Skip to content

Add --staged-only flag to fix unstaged changes attribution (Issue #16)#19

Closed
jalalatlassian wants to merge 2 commits intogit-ai-project:mainfrom
jalalatlassian:fix/staged-only-attribution
Closed

Add --staged-only flag to fix unstaged changes attribution (Issue #16)#19
jalalatlassian wants to merge 2 commits intogit-ai-project:mainfrom
jalalatlassian:fix/staged-only-attribution

Conversation

@jalalatlassian
Copy link
Copy Markdown

Pull Request: Add --staged-only flag to fix unstaged changes attribution (Issue #16)

Summary

This PR implements a solution for Issue #16 where unstaged changes were incorrectly attributed to AI agents, corrupting attribution accuracy. The solution adds a --staged-only flag that ensures only explicitly staged changes are tracked for attribution.

Problem

Git-ai was attributing ALL working directory changes (staged, unstaged, and untracked) to the checkpoint author. This caused:

  • Unstaged human changes incorrectly attributed to AI agents
  • Corrupted human vs AI contribution metrics
  • Loss of trust in attribution accuracy

Solution

Added --staged-only flag that:

  • ✅ Only tracks staged changes (git add) for attribution
  • ✅ Ignores unstaged changes with helpful warnings
  • ✅ Preserves backward compatibility (flag is opt-in)
  • ✅ Provides clear guidance to users

Changes Made

1. Core Implementation (src/commands/checkpoint.rs)

  • Added staged_only parameter to run() function
  • Implemented get_staged_files_only() - filters only staged changes using git2::Status flags
  • Implemented get_unstaged_files() - detects unstaged changes for warnings
  • Added warning system to inform users about ignored unstaged files

2. Command-Line Interface (src/main.rs)

  • Added --staged-only flag parsing
  • Updated help text with flag documentation
  • Passed flag through to checkpoint function

3. Comprehensive Testing (tests/staged_only_attribution.rs)

  • Test staged-only flag ignores unstaged changes
  • Test without flag includes all changes (backward compatibility)
  • Test warning messages for unstaged files
  • Test mixed staged/unstaged scenarios

4. Manual Testing (test_staged_only.sh)

  • Interactive test script for manual verification
  • Demonstrates flag behavior with real git scenarios

Usage Examples

Before (Problematic)

# Human makes changes but doesn't stage
echo "human code" > file.txt

# AI agent runs checkpoint - INCORRECTLY attributes human code to AI
git-ai checkpoint --author "Claude"

After (Fixed)

# Human makes changes but doesn't stage  
echo "human code" > file.txt

# AI agent runs checkpoint with --staged-only - ignores unstaged human code
git-ai checkpoint --author "Claude" --staged-only
# Warning: Found 1 unstaged file(s) that will be ignored:
#   file.txt
# Use 'git add' to stage changes or remove --staged-only to include unstaged changes

# Proper workflow: AI stages its changes
git add ai_generated_file.txt
git-ai checkpoint --author "Claude" --staged-only  # Only tracks AI's staged changes

Backward Compatibility

  • No breaking changes - flag is opt-in
  • Existing workflows continue working without the flag
  • Authorship log format unchanged - no migration needed
  • Clear migration path for users who want accurate attribution

Testing

Run the comprehensive test suite:

cargo test staged_only_attribution

Run manual verification:

./test_staged_only.sh

Future Considerations

This implementation provides a foundation for making staged-only the default behavior in future releases:

  1. Phase 1 (This PR): Add --staged-only flag (opt-in)
  2. Phase 2 (Future): Make staged-only default with --include-unstaged for backward compatibility
  3. Phase 3 (Future): Remove backward compatibility flag

Files Changed

  • src/commands/checkpoint.rs (+74 lines) - Core implementation
  • src/main.rs (+6 lines) - CLI interface
  • tests/staged_only_attribution.rs (+251 lines) - Comprehensive tests
  • test_staged_only.sh (+73 lines) - Manual test script

Total: 407 additions, 3 deletions

Fixes

Closes #16

Review Notes

  • Implementation is minimal and focused - only adds filtering logic
  • No changes to existing data structures or formats
  • Clear error messages guide users to correct usage
  • Comprehensive test coverage for edge cases
  • Ready for immediate deployment with low risk

…-ai-project#16)

This commit implements a solution for Issue git-ai-project#16 where unstaged changes were
incorrectly attributed to AI agents, corrupting attribution accuracy.

Changes:
- Add --staged-only flag to checkpoint command
- Implement get_staged_files_only() to filter only staged changes
- Add get_unstaged_files() to detect and warn about ignored files
- Update command-line interface and help text
- Add comprehensive tests for the new functionality

When --staged-only is used:
- Only staged changes (git add) are tracked for attribution
- Unstaged changes are ignored with helpful warnings
- Users are guided to stage changes or remove the flag

This preserves backward compatibility while providing a path to accurate
attribution. The flag can be made default in future releases.

Fixes git-ai-project#16
@jalalatlassian jalalatlassian force-pushed the fix/staged-only-attribution branch from 71de20c to f483933 Compare September 3, 2025 01:43
- Update pre_commit.rs and tmp_repo.rs to pass staged_only=false
- Fix test to check stderr instead of stdout for output
- All tests now pass including comprehensive staged-only test suite
@acunniffe
Copy link
Copy Markdown
Collaborator

Thanks for this @jalaatlassian, but I don't think this is a bug.

The guidance in the ReadMe is to run git-ai checkpoint right before your agent starts writing to the filesystem. This is meant to mop up and attribute any human changes that were made since the last checkpoint.

Screenshot 2025-09-03 at 9 57 12 AM

@acunniffe acunniffe closed this Sep 3, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Unstaged changes incorrectly attributed as human changes

2 participants