Skip to content

feat: Prevent agents from committing unintended files (git staging hygiene) #42

@diberry

Description

@diberry

Problem

When Squad agents (EECOM, CONTROL, etc.) make code changes via the task tool, they frequently include unintended files in their commits — dirty template files, .squad/ state changes, or working-tree artifacts from other branches. This happens because agents use git add . or git commit -a instead of staging specific files.

Evidence from This Session

  1. PR fix(nap): archive undated decisions and add count-based fallback #38 (nap.ts bug fix): EECOM's commit included 8 extra template files (squad.agent.md, casting-reference.md, orchestration-log.md, squad-heartbeat.yml) that were dirty in the working tree but unrelated to the fix. The diff showed 3,365 insertions / 3,166 deletions when the actual change was +199 lines across 2 files.

  2. PR feat(ci): add test count guard to prevent AI agents from deleting tests #41 (test count guard): Same pattern — EECOM committed 13 files when only 5 were deliverables. Template files from the working tree leaked into the commit. Required manual cleanup: git reset --soft HEAD~1, selective re-staging of only the 5 intended files, and recommit.

  3. P0: @copilot accidentally deleted 361 source files on dev (commit 1ab2f5c) bradygaster/squad#631 (@copilot mass deletion): The GitHub @copilot coding agent committed 361 file deletions alongside a 6-file docs fix because it used git add . on an incomplete working tree. This is the same root cause at extreme scale.

Root Cause

Agents default to broad staging commands (git add ., git add -A, git commit -a) because:

  1. It's the simplest way to "commit everything I changed"
  2. The agent doesn't check git status before committing to verify only intended files are staged
  3. The working tree often has dirty files from other branches, template syncs, or build artifacts
  4. No pre-commit validation exists to catch over-broad commits

Impact

Proposed Solutions

1. Agent spawn prompt: explicit staging instructions

Add to the standard agent spawn template in squad.agent.md:

## Git Hygiene
- NEVER use `git add .`, `git add -A`, or `git commit -a`
- ALWAYS stage specific files: `git add path/to/file1 path/to/file2`
- Before committing, run `git status` and verify ONLY intended files are staged
- If you see unexpected files in `git status`, do NOT stage them

2. copilot-instructions.md: same rule for @copilot

Already partially addressed in PR #41 (test integrity section). Add explicit git staging rules.

3. Pre-commit hook or CI check

A lightweight check that warns when a commit touches >N files or includes files outside the expected scope. Could be:

  • A git hook (.githooks/pre-commit) that warns on >20 staged files
  • A CI step that compares PR file count against issue scope
  • A Squad coordinator post-work check that verifies agent commits are scoped

4. Agent git add wrapper

Teach agents to use a scoped staging pattern:

# Instead of: git add .
# Use: git add only the files you created/modified
git add packages/squad-cli/src/cli/core/nap.ts test/nap.test.ts

Or provide a helper that stages only files matching a pattern:

git diff --name-only | grep -E "nap\.ts|test/nap" | xargs git add

5. Coordinator post-work validation

After an agent commits, the coordinator should verify:

  • git diff --stat HEAD~1 shows only expected files
  • No template files, .squad/ state, or build artifacts leaked in
  • File count is reasonable for the task scope

If validation fails, auto-remediate: git reset --soft HEAD~1, selective re-stage, recommit.

Success Criteria

  • Agent spawn prompts include explicit "never use git add ." rule
  • No PR in this session's work requires manual commit cleanup
  • copilot-instructions.md has git staging rules
  • Pre-commit or post-work validation catches over-broad commits

Related

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or requestgo:needs-researchNeeds investigationsquadSquad triage inbox — Lead will assign to a membersquad:archiveResolved by upstream or no longer applicablesquad:fidoAssigned to FIDO (Quality Owner)

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions