|
| 1 | +#!/usr/bin/env bash |
| 2 | +# Validate Project Context Memory Hook |
| 3 | +# |
| 4 | +# This script validates that the project-context-scanner agent properly saved |
| 5 | +# project context to memory. If validation fails, it re-enqueues the scanner task. |
| 6 | +# |
| 7 | +# Usage: validate_project_context_memory.sh <task_id> |
| 8 | + |
| 9 | +set -euo pipefail |
| 10 | + |
| 11 | +TASK_ID="${1:-}" |
| 12 | + |
| 13 | +# Colors for output |
| 14 | +RED='\033[0;31m' |
| 15 | +GREEN='\033[0;32m' |
| 16 | +YELLOW='\033[1;33m' |
| 17 | +NC='\033[0m' # No Color |
| 18 | + |
| 19 | +log_info() { |
| 20 | + echo -e "${GREEN}[INFO]${NC} $*" |
| 21 | +} |
| 22 | + |
| 23 | +log_warn() { |
| 24 | + echo -e "${YELLOW}[WARN]${NC} $*" |
| 25 | +} |
| 26 | + |
| 27 | +log_error() { |
| 28 | + echo -e "${RED}[ERROR]${NC} $*" |
| 29 | +} |
| 30 | + |
| 31 | +# Validate inputs |
| 32 | +if [[ -z "$TASK_ID" ]]; then |
| 33 | + log_error "Task ID is required" |
| 34 | + exit 1 |
| 35 | +fi |
| 36 | + |
| 37 | +log_info "Validating project context memory for task: $TASK_ID" |
| 38 | + |
| 39 | +# Check if abathur CLI is available |
| 40 | +if ! command -v abathur &> /dev/null; then |
| 41 | + log_error "abathur CLI not found in PATH" |
| 42 | + exit 1 |
| 43 | +fi |
| 44 | + |
| 45 | +# Query memory to check if project context was saved |
| 46 | +log_info "Checking for project context in memory..." |
| 47 | + |
| 48 | +# Try to get the memory entry for project:context/metadata |
| 49 | +if ! MEMORY_OUTPUT=$(abathur memory get project:context metadata 2>&1); then |
| 50 | + log_error "Failed to retrieve project context from memory" |
| 51 | + log_error "Output: $MEMORY_OUTPUT" |
| 52 | + |
| 53 | + # Re-enqueue the project-context-scanner task |
| 54 | + log_warn "Re-enqueueing project-context-scanner task..." |
| 55 | + |
| 56 | + if abathur task enqueue \ |
| 57 | + --summary "Scan project context" \ |
| 58 | + --description "Initial project scan to detect language, framework, and conventions (retry after validation failure)." \ |
| 59 | + --agent-type "project-context-scanner" \ |
| 60 | + --priority 10; then |
| 61 | + log_info "Successfully re-enqueued project-context-scanner task" |
| 62 | + else |
| 63 | + log_error "Failed to re-enqueue project-context-scanner task" |
| 64 | + exit 1 |
| 65 | + fi |
| 66 | + |
| 67 | + exit 1 |
| 68 | +fi |
| 69 | + |
| 70 | +# Validate that the memory contains required fields |
| 71 | +log_info "Validating project context structure..." |
| 72 | + |
| 73 | +# Check if the output contains expected JSON structure |
| 74 | +if echo "$MEMORY_OUTPUT" | grep -q '"language"' && \ |
| 75 | + echo "$MEMORY_OUTPUT" | grep -q '"frameworks"' && \ |
| 76 | + echo "$MEMORY_OUTPUT" | grep -q '"tooling"' && \ |
| 77 | + echo "$MEMORY_OUTPUT" | grep -q '"validation_requirements"'; then |
| 78 | + log_info "✓ Project context contains all required fields" |
| 79 | +else |
| 80 | + log_error "Project context is missing required fields" |
| 81 | + log_error "Memory output: $MEMORY_OUTPUT" |
| 82 | + |
| 83 | + # Re-enqueue the project-context-scanner task |
| 84 | + log_warn "Re-enqueueing project-context-scanner task due to incomplete data..." |
| 85 | + |
| 86 | + if abathur task enqueue \ |
| 87 | + --summary "Scan project context" \ |
| 88 | + --description "Initial project scan to detect language, framework, and conventions (retry after incomplete data)." \ |
| 89 | + --agent-type "project-context-scanner" \ |
| 90 | + --priority 10; then |
| 91 | + log_info "Successfully re-enqueued project-context-scanner task" |
| 92 | + else |
| 93 | + log_error "Failed to re-enqueue project-context-scanner task" |
| 94 | + exit 1 |
| 95 | + fi |
| 96 | + |
| 97 | + exit 1 |
| 98 | +fi |
| 99 | + |
| 100 | +# Validate primary language is set |
| 101 | +if echo "$MEMORY_OUTPUT" | grep -q '"primary"'; then |
| 102 | + log_info "✓ Primary language detected" |
| 103 | +else |
| 104 | + log_warn "Primary language not detected in project context" |
| 105 | +fi |
| 106 | + |
| 107 | +# Validate validation_agent is set |
| 108 | +if echo "$MEMORY_OUTPUT" | grep -q '"validation_agent"'; then |
| 109 | + log_info "✓ Validation agent specified" |
| 110 | +else |
| 111 | + log_warn "Validation agent not specified in project context" |
| 112 | +fi |
| 113 | + |
| 114 | +log_info "✓ Project context memory validation passed for task $TASK_ID" |
| 115 | +exit 0 |
0 commit comments