Files
claudetools/.claude/hooks/sync-contexts
Mike Swanson 25f3759ecc [Config] Add coding guidelines and code-fixer agent
Major additions:
- Add CODING_GUIDELINES.md with "NO EMOJIS" rule
- Create code-fixer agent for automated violation fixes
- Add offline mode v2 hooks with local caching/queue
- Add periodic context save with invisible Task Scheduler setup
- Add agent coordination rules and database connection docs

Infrastructure:
- Update hooks: task-complete-v2, user-prompt-submit-v2
- Add periodic_save_check.py for auto-save every 5min
- Add PowerShell scripts: setup_periodic_save.ps1, update_to_invisible.ps1
- Add sync-contexts script for queue synchronization

Documentation:
- OFFLINE_MODE.md, PERIODIC_SAVE_INVISIBLE_SETUP.md
- Migration procedures and verification docs
- Fix flashing window guide

Updates:
- Update agent configs (backup, code-review, coding, database, gitea, testing)
- Update claude.md with coding guidelines reference
- Update .gitignore for new cache/queue directories

Status: Pre-automated-fixer baseline commit

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
2026-01-17 12:51:43 -07:00

111 lines
3.0 KiB
Bash

#!/bin/bash
#
# Sync Queued Contexts to Database
# Uploads any locally queued contexts to the central API
# Can be run manually or called automatically by hooks
#
# Usage: bash .claude/hooks/sync-contexts
#
# Load configuration
CLAUDE_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
CONFIG_FILE="$CLAUDE_DIR/context-recall-config.env"
if [ -f "$CONFIG_FILE" ]; then
source "$CONFIG_FILE"
fi
# Default values
API_URL="${CLAUDE_API_URL:-http://172.16.3.30:8001}"
QUEUE_DIR="$CLAUDE_DIR/context-queue"
PENDING_DIR="$QUEUE_DIR/pending"
UPLOADED_DIR="$QUEUE_DIR/uploaded"
FAILED_DIR="$QUEUE_DIR/failed"
# Exit if no JWT token
if [ -z "$JWT_TOKEN" ]; then
echo "ERROR: No JWT token available" >&2
exit 1
fi
# Create directories if they don't exist
mkdir -p "$PENDING_DIR" "$UPLOADED_DIR" "$FAILED_DIR" 2>/dev/null
# Check if there are any pending files
PENDING_COUNT=$(find "$PENDING_DIR" -type f -name "*.json" 2>/dev/null | wc -l)
if [ "$PENDING_COUNT" -eq 0 ]; then
# No pending contexts to sync
exit 0
fi
echo "==================================="
echo "Syncing Queued Contexts"
echo "==================================="
echo "Found $PENDING_COUNT pending context(s)"
echo ""
# Process each pending file
SUCCESS_COUNT=0
FAIL_COUNT=0
for QUEUE_FILE in "$PENDING_DIR"/*.json; do
# Skip if no files match
[ -e "$QUEUE_FILE" ] || continue
FILENAME=$(basename "$QUEUE_FILE")
echo "Processing: $FILENAME"
# Read the payload
PAYLOAD=$(cat "$QUEUE_FILE")
# Determine endpoint based on filename
if [[ "$FILENAME" == *"_state.json" ]]; then
ENDPOINT="${API_URL}/api/project-states"
else
ENDPOINT="${API_URL}/api/conversation-contexts"
fi
# Try to POST to API
RESPONSE=$(curl -s --max-time 10 -w "\n%{http_code}" \
-X POST "$ENDPOINT" \
-H "Authorization: Bearer ${JWT_TOKEN}" \
-H "Content-Type: application/json" \
-d "$PAYLOAD" 2>/dev/null)
HTTP_CODE=$(echo "$RESPONSE" | tail -n1)
if [ "$HTTP_CODE" = "200" ] || [ "$HTTP_CODE" = "201" ]; then
# Success - move to uploaded directory
mv "$QUEUE_FILE" "$UPLOADED_DIR/"
echo " ✓ Uploaded successfully"
((SUCCESS_COUNT++))
else
# Failed - move to failed directory for manual review
mv "$QUEUE_FILE" "$FAILED_DIR/"
echo " ✗ Upload failed (HTTP $HTTP_CODE) - moved to failed/"
((FAIL_COUNT++))
fi
done
echo ""
echo "==================================="
echo "Sync Complete"
echo "==================================="
echo "Successful: $SUCCESS_COUNT"
echo "Failed: $FAIL_COUNT"
echo ""
# Clean up old uploaded files (keep last 100)
UPLOADED_COUNT=$(find "$UPLOADED_DIR" -type f -name "*.json" 2>/dev/null | wc -l)
if [ "$UPLOADED_COUNT" -gt 100 ]; then
echo "Cleaning up old uploaded contexts (keeping last 100)..."
find "$UPLOADED_DIR" -type f -name "*.json" -printf '%T@ %p\n' | \
sort -n | \
head -n -100 | \
cut -d' ' -f2- | \
xargs rm -f
fi
exit 0