Comparing AI Agent File Scope with Git Changes
In this article
This guide walks you through comparing the files an AI agent is allowed to modify (its file scope) with the actual changes tracked in your Git repository. This is useful for code review, debugging, and ensuring the agent didn't touch files outside its intended scope. You'll learn to identify scope violations, verify agent behavior, and keep your workflow clean.
What You'll Need
- A Git repository with at least one commit.
- The AI agent's configuration file (e.g.,
agent.yaml,.agentconfig) that defines its file scope (include/exclude patterns). - Access to a terminal or command line.
- Basic familiarity with Git commands (
git diff,git status,git log).
Step-by-Step
- Identify the agent's file scope. Look for a configuration file in the repo root or a dedicated config directory. It typically contains patterns like
include: ["src/**"]orexclude: ["docs/*"]. Note these patterns exactly. - Determine the Git changes you want to compare. Decide whether you want to compare against the last commit (HEAD), a specific branch, or a range of commits. For a simple check, use
git diff --name-only HEADto list all files changed since the last commit. - Get the list of changed files. Run
git diff --name-only HEAD(orgit diff --name-only main...your-branchfor a branch comparison). This outputs one file path per line. Save this to a temporary file if you want to process it later. - Convert the agent's scope patterns to a comparable format. If the agent uses glob patterns (like
src/**/*.js), you can use a tool likegit check-ignoreor a glob library. For simplicity, you can manually expand the patterns or use a shell command likefind src -type fto list all files that match the include pattern. - Compare the lists. Use a diff tool or a script to compare the list of changed files against the list of files allowed by the agent's scope. In bash, you can use
comm -23 <(git diff --name-only HEAD | sort) <(list_of_allowed_files | sort)to find files that are changed but NOT in the allowed list. This shows scope violations. - Check for files that are in scope but unchanged. The reverse can also be useful:
comm -13will show files in scope that were not changed, which might indicate the agent missed something. - Automate the comparison. For regular checks, write a small script (e.g., in Python or bash) that reads the agent config, extracts the patterns, and compares them against
git diff --name-only. This can be integrated into a CI pipeline to automatically flag scope violations. - Review the results. If you find files outside the scope that were changed, investigate why. It could be a misconfigured agent, a bug, or a deliberate override. If files in scope were not changed, verify the agent actually attempted to work on them.
Common Mistakes
- Ignoring nested patterns. A pattern like
src/**includes all subdirectories, butsrc/*only includes direct children. Always test your patterns. - Forgetting about untracked files.
git diffonly shows tracked files. If the agent created new files, they appear ingit statusas untracked. Usegit status --porcelainto see them. - Using the wrong base commit. If you compare against the wrong commit, you'll get misleading results. Always specify the correct base (e.g.,
HEAD~1for the previous commit). - Not accounting for file renames. Git can detect renames, but
--name-onlyshows the new path. If the agent renamed a file, it might appear as a delete and add. Use--name-statusto see the rename flag.
Tips
- Use
git diff --name-statusto see whether a file was added, modified, or deleted. This helps understand the agent's actions. - Leverage
git check-ignoreto test if a specific file is excluded by the agent's scope. For example,git check-ignore docs/readme.mdwill output the file if it matches an exclude pattern. - Create a baseline. Before running the agent, capture the current state with
git rev-parse HEAD. After the agent runs, compare against that exact commit. - Use a GUI tool. Tools like GitKraken or VS Code's GitLens can visually show changed files, making it easier to spot out-of-scope changes.
- Write a test. If you use a script, add a test that asserts no files outside the scope are changed. This prevents regressions.
Troubleshooting
- No changes appear. If
git diffshows nothing, but you know the agent made changes, check if the files are untracked or if the agent committed them. Usegit statusto see all changes. - Patterns don't match as expected. Glob syntax varies. Test your patterns with a tool like
globstarin bash or use a library likeglobin Python to expand them and see what matches. - Script errors due to spaces in file paths. When processing file lists, always use null-delimited output (
git diff -z --name-only) and read withwhile IFS= read -r -d '' fileto handle spaces correctly. - Agent config is not in the repo. If the config is stored elsewhere (e.g., in a CI variable), you'll need to fetch it manually. You can still compare by manually listing the allowed files.
- Large repositories slow down the comparison. For huge repos, use
git diff --name-only --diff-filter=ACMRTto only include added, copied, modified, renamed, or type-changed files, excluding deletions.
Frequently Asked Questions
How do I see the exact files an AI agent changed?
Run git diff --name-only HEAD after the agent finishes. This lists all tracked files that differ from the last commit. For untracked files, use git status --porcelain.
What if the agent committed its changes?
If the agent made a commit, compare against the commit before it. Use git diff HEAD~1 HEAD --name-only to see what that commit changed.
Can I compare against a specific branch?
Yes, use git diff main...feature-branch --name-only to see changes on the feature branch relative to main. This is useful for pull request reviews.
How do I handle symlinks or submodules?
Git treats symlinks as files, so they appear in diffs. Submodules show as a single commit reference. You may need to handle these separately, as the agent's scope might not cover them.
Is there a built-in Git command for this?
No, Git doesn't know about your agent's scope. You need to write a script or use a third-party tool to compare the scope patterns with the diff output.
Can I automate this check in CI?
Yes, you can add a CI step that runs your comparison script. If it finds out-of-scope files, the build fails, alerting you to the issue.