A comprehensive caching and change tracking system for Square catalog items using MongoDB.
This system provides:
- Local MongoDB cache of all Square catalog items
- Change detection with before/after snapshots
- Audit trail of all modifications
- AI-powered image processing with automated background removal
- CLI tools for easy management
- Photo integration from Photos Library to Square
- Gemini AI chat for image analysis and assistance
~/Workspace/square-tools/
βββ bin/
β βββ square_cache.sh # CLI wrapper for cache operations
β βββ photos_to_square.sh # Photos Library β Square uploader
β βββ process_and_upload.sh # AI-powered photo processing + upload
β βββ gemini_chat.sh # Interactive Gemini chat interface
β βββ browse_photos.sh # Photos Library browser
βββ cache-system/
β βββ square_cache_manager.py # Core MongoDB cache manager
β βββ bg_removal_service.py # Background removal service (Remove.bg, Gemini, Banana)
β βββ test_bg_removal.py # System health checker
β βββ demo_change_tracking.sh # Demonstration script
βββ data/
β βββ photo_exports/ # Exported photos directory
β βββ logs/ # Application logs
βββ config.sh # Configuration file
βββ IMAGE_PROCESSING.md # Image processing documentation
βββ QUICKSTART_BG_REMOVAL.md # Quick start guide
βββ DEMO.md # Quick demos and examples
βββ README.md # This documentation
-
catalog_items- Cached Square catalog items{ "_id": "ObjectId", "id": "ANE5SXKQR4JZ6AYEZDO26IMX", "type": "ITEM", "version": 1759194139169, "item_data": { "name": "Trading Places - Rare Video 8 Format (1983)", "image_ids": ["ZDHVCXFYL7MNUQV6TAQTXH4G", "..."], ... }, "content_hash": "620e6650a2732e5a...", "cached_at": "2025-09-30T02:31:13.439Z" } -
change_snapshots- Before/after change tracking{ "_id": "ObjectId", "item_id": "ANE5SXKQR4JZ6AYEZDO26IMX", "item_name": "Trading Places - Rare Video 8 Format (1983)", "change_type": "update", "timestamp": "2025-09-30T02:35:00.000Z", "before_data": { /* complete item before change */ }, "after_data": { /* complete item after change */ }, "differences": { "item_data.image_ids": { "before": null, "after": ["ZDHVCXFYL7MNUQV6TAQTXH4G", "JUOA5BTTKGJOXWHJ62RR4I62"] } }, "square_version_before": 1759194139169, "square_version_after": 1759194200000 } -
sync_log- Sync operation history{ "_id": "ObjectId", "timestamp": "2025-09-30T02:31:12.920Z", "total_items": 31, "created_count": 31, "updated_count": 0, "changes_detected": 31, "duration_seconds": 0.532 } -
bg_removal_cache- Processed image cache (AI background removal){ "_id": "ObjectId", "image_hash": "sha256_hash_of_original", "processed_path": "/path/to/processed_image.png", "metadata": { "provider": "remove_bg", "status": "success", "cost": 0.002, "timestamp": "2025-12-20T03:22:00Z", "original_size": 1024000, "processed_size": 512000 }, "cached_at": "2025-12-20T03:22:00Z" }
Privileged operations are gated by runtime policy:
runtime/capability_matrix.jsonruntime/operation_policy.jsonbin/agent_preflight.sh
Example:
bin/agent_preflight.sh --operation square_cache_sync --runtime "${SQUARE_RUNTIME_ID:-local_cli}"# Install MongoDB
brew tap mongodb/brew
brew install mongodb-community@8.0
brew services start mongodb-community@8.0
# Install Python dependencies
pip3 install pymongo requests Pillow# Required for Square API
export SQUARE_TOKEN="YOUR_SQUARE_TOKEN_HERE"
# Optional: For AI background removal (at least one recommended)
export REMOVEBG_API_KEY="your_remove_bg_api_key" # Recommended
export GEMINI_API_KEY="your_gemini_api_key" # For chat & image analysisRun secret scanning before commit/release:
./bin/secret_scan.sh# Load configuration (creates directories)
source ~/Workspace/square-tools/config.sh
# Check system status
square_cache.sh status
# Perform initial sync (creates baseline cache)
square_cache.sh sync
# View all changes
square_cache.sh changes# Sync all items from Square to MongoDB cache
square_cache.sh sync
# Show recent changes
square_cache.sh changes
square_cache.sh changes --since 2025-09-30
# Generate detailed change report
square_cache.sh report
square_cache.sh report --output json
# Search cached items
square_cache.sh search "Trading Places"
# Get specific item details
square_cache.sh item ANE5SXKQR4JZ6AYEZDO26IMX
# Check system status
square_cache.sh status# Browse Photos Library
browse_photos.sh recent 10
browse_photos.sh search "IMG_7232"
browse_photos.sh random 5
browse_photos.sh export 27569
# Upload photos to Square (basic)
photos_to_square.sh 27569 "YOUR_SQUARE_TOKEN"
# Process with AI background removal and upload
process_and_upload.sh 27569 --remove-bg --preview
process_and_upload.sh 27569 --remove-bg --skip-upload
process_and_upload.sh 27569 --remove-bg --provider remove_bg# Remove background from image (Python interface)
python3 cache-system/bg_removal_service.py image.jpg remove_bg
# Chat with Gemini about text
gemini_chat.sh flash
# Chat with Gemini about an image
gemini_chat.sh flash image.jpg
# Test system health
python3 cache-system/test_bg_removal.py# Run change tracking demonstration
~/Workspace/square-tools/cache-system/demo_change_tracking.sh- Each item gets a SHA256 hash of its content (excluding volatile fields like
updated_at,version) - Changes detected by comparing hashes between Square API and cached versions
- BEFORE: Complete item data from MongoDB cache
- AFTER: Complete item data from Square API
- DIFFERENCES: Field-by-field comparison showing exactly what changed
create: New item added to Square catalogupdate: Existing item modified (with detailed diff)delete: Item removed from Square catalog (planned feature)
{
"name": "Trading Places - Rare Video 8 Format (1983)",
"version": 1759194139169,
"item_data": {
// NO image_ids field - item has no images
}
}{
"name": "Trading Places - Rare Video 8 Format (1983)",
"version": 1759194200000,
"item_data": {
"image_ids": [
"ZDHVCXFYL7MNUQV6TAQTXH4G", // 1A2E186B-A015-4A86-84B6-C1F76EC9810D.jpeg
"JUOA5BTTKGJOXWHJ62RR4I62" // 5748D4B2-F1DC-4BB6-84E4-90B56DCA4059.jpeg
]
}
}π Trading Places - Rare Video 8 Format (1983)
Type: update
Changes: item_data.image_ids, version, updated_at
Version: 1759194139169 β 1759194200000
BEFORE: No images attached
AFTER: 2 images attached
- Track all changes to your Square catalog
- See who changed what and when
- Compliance and business intelligence
- Backup/restore item configurations
- Bulk operations planning
- A/B testing tracking
- Detect unauthorized changes
- Monitor third-party integrations
- Quality assurance workflows
- Track catalog growth over time
- Monitor pricing changes
- Image attachment patterns
- β 31 items cached from Square catalog
- β All items have baseline snapshots (before state)
- β Trading Places item cached with NO images (perfect test case)
- β 2 images uploaded to Square catalog ready for attachment
- β Change detection ready to track image attachment
- Baseline established: All items cached with current state
- Make changes: Attach images via Square Dashboard or API
- Sync & detect:
./square_cache.sh syncwill detect changes - View changes:
./square_cache.sh changesshows before/after
def _detect_changes(self, square_item: Dict) -> Optional[ChangeSnapshot]:
item_id = square_item['id']
cached_item = self.items_collection.find_one({"id": item_id})
if not cached_item:
return ChangeSnapshot(change_type='create', ...)
square_hash = self._calculate_hash(square_item)
cached_hash = cached_item.get('content_hash', '')
if square_hash != cached_hash:
differences = self._find_differences(cached_item, square_item)
return ChangeSnapshot(change_type='update', differences=differences, ...)
return None- MongoDB indexes on key fields (id, timestamp, item_name)
- Content hashing for efficient change detection
- Cursor-based pagination for large catalogs
- Batch operations for bulk changes
- Connection retry logic for MongoDB and Square API
- Partial sync recovery if operations fail
- Comprehensive logging of all operations
- Data validation before cache updates
The system successfully provides:
- Complete catalog cache: All 31 Square items cached locally in MongoDB
- Change tracking: Before/after snapshots for all modifications
- Audit trail: Complete history of when and what changed
- CLI tools: Easy-to-use command-line interface
- Photo integration: Direct upload from macOS Photos Library
- Real-time sync: Detect changes on-demand or scheduled
- Detailed reporting: JSON and human-readable change reports
This creates a robust foundation for Square catalog management, change tracking, and business intelligence.
To see the change tracking in action:
- Attach the uploaded images to Trading Places item in Square Dashboard
- Run sync:
./square_cache.sh sync - View changes:
./square_cache.sh changes --since 2025-09-30 - Generate report:
./square_cache.sh report
The system will detect and document the exact changes made, creating a complete before/after snapshot for audit and analysis purposes.