Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

Comparing AI Agent File Scope with Git Changes

· 5 min read · Home Tech

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

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

  1. 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/**"] or exclude: ["docs/*"]. Note these patterns exactly.
  2. 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 HEAD to list all files changed since the last commit.
  3. Get the list of changed files. Run git diff --name-only HEAD (or git diff --name-only main...your-branch for a branch comparison). This outputs one file path per line. Save this to a temporary file if you want to process it later.
  4. Convert the agent's scope patterns to a comparable format. If the agent uses glob patterns (like src/**/*.js), you can use a tool like git check-ignore or a glob library. For simplicity, you can manually expand the patterns or use a shell command like find src -type f to list all files that match the include pattern.
  5. 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.
  6. Check for files that are in scope but unchanged. The reverse can also be useful: comm -13 will show files in scope that were not changed, which might indicate the agent missed something.
  7. 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.
  8. 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, but src/* only includes direct children. Always test your patterns.
  • Forgetting about untracked files. git diff only shows tracked files. If the agent created new files, they appear in git status as untracked. Use git status --porcelain to 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~1 for the previous commit).
  • Not accounting for file renames. Git can detect renames, but --name-only shows the new path. If the agent renamed a file, it might appear as a delete and add. Use --name-status to see the rename flag.

Tips

  • Use git diff --name-status to see whether a file was added, modified, or deleted. This helps understand the agent's actions.
  • Leverage git check-ignore to test if a specific file is excluded by the agent's scope. For example, git check-ignore docs/readme.md will 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 diff shows nothing, but you know the agent made changes, check if the files are untracked or if the agent committed them. Use git status to see all changes.
  • Patterns don't match as expected. Glob syntax varies. Test your patterns with a tool like globstar in bash or use a library like glob in 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 with while IFS= read -r -d '' file to 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=ACMRT to 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.