Skip to content

Commit dc68261

Browse files
committed
add project context hook
1 parent 61e8c48 commit dc68261

File tree

6 files changed

+237
-2
lines changed

6 files changed

+237
-2
lines changed

.abathur/hooks.yaml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -349,6 +349,26 @@ hooks:
349349
priority: 5
350350
enabled: true
351351

352+
# ============================================================================
353+
# PROJECT CONTEXT SCANNER HOOKS
354+
# ============================================================================
355+
356+
- id: validate-project-context-memory
357+
description: "Validate that project-context-scanner stored memory correctly and reinitialize if failed"
358+
event: post_complete
359+
conditions:
360+
- agent_type: project-context-scanner
361+
task_status: completed
362+
actions:
363+
- type: run_script
364+
script_path: ./.abathur/hooks/validate_project_context_memory.sh
365+
args: ["${task_id}"]
366+
- type: log_message
367+
level: info
368+
message: "Validating project context memory for task ${task_id}"
369+
priority: 10
370+
enabled: true
371+
352372
# ============================================================================
353373
# CHILD SPAWNING HOOKS
354374
# ============================================================================

src/application/swarm_orchestrator.rs

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -198,8 +198,9 @@ impl SwarmOrchestrator {
198198
///
199199
/// Initializes:
200200
/// 1. Resource monitoring background task
201-
/// 2. Task polling and distribution loop
202-
/// 3. Agent event processing loop
201+
/// 2. Process all pending tasks (transition to Ready/Blocked)
202+
/// 3. Task polling and distribution loop
203+
/// 4. Agent event processing loop
203204
pub async fn start(&mut self) -> Result<()> {
204205
let mut state = self.state.write().await;
205206
if *state != SwarmState::Stopped {
@@ -220,6 +221,18 @@ impl SwarmOrchestrator {
220221

221222
*self.resource_monitor_handle.write().await = Some(resource_handle);
222223

224+
// Process all pending tasks to transition them to Ready or Blocked
225+
info!("Processing pending tasks on startup");
226+
match self.task_coordinator.process_pending_tasks().await {
227+
Ok(count) => {
228+
info!("Processed {} pending tasks", count);
229+
}
230+
Err(e) => {
231+
warn!("Error processing pending tasks: {:?}", e);
232+
// Continue anyway - this is not a fatal error
233+
}
234+
}
235+
223236
// Start main task processing loop
224237
let task_loop_handle = self.spawn_task_processing_loop().await?;
225238
*self.task_loop_handle.write().await = Some(task_loop_handle);

src/application/task_coordinator.rs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -480,6 +480,43 @@ impl TaskCoordinator {
480480
Ok(task_id)
481481
}
482482

483+
/// Process all pending tasks on startup
484+
///
485+
/// This method should be called when the swarm orchestrator starts to transition
486+
/// all pending tasks to either Ready (if dependencies are met) or Blocked (if not).
487+
///
488+
/// # Returns
489+
///
490+
/// * `Ok(usize)` - Number of tasks processed
491+
/// * `Err` - If database error or coordination fails
492+
#[instrument(skip(self))]
493+
pub async fn process_pending_tasks(&self) -> Result<usize> {
494+
info!("Processing all pending tasks on startup");
495+
496+
// Get all pending tasks
497+
let pending_tasks = self
498+
.task_queue
499+
.get_tasks_by_status(TaskStatus::Pending)
500+
.await
501+
.context("Failed to get pending tasks")?;
502+
503+
let task_count = pending_tasks.len();
504+
info!("Found {} pending tasks to process", task_count);
505+
506+
// Process each pending task
507+
for task in pending_tasks {
508+
if let Err(e) = self.coordinate_task_lifecycle(task.id).await {
509+
warn!(
510+
"Failed to coordinate pending task {}: {:?}",
511+
task.id, e
512+
);
513+
}
514+
}
515+
516+
info!("Finished processing {} pending tasks", task_count);
517+
Ok(task_count)
518+
}
519+
483520
// Private helper methods
484521

485522
/// Check if all dependencies for a task are met

template/.abathur/hooks.yaml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -349,6 +349,26 @@ hooks:
349349
priority: 5
350350
enabled: true
351351

352+
# ============================================================================
353+
# PROJECT CONTEXT SCANNER HOOKS
354+
# ============================================================================
355+
356+
- id: validate-project-context-memory
357+
description: "Validate that project-context-scanner stored memory correctly and reinitialize if failed"
358+
event: post_complete
359+
conditions:
360+
- agent_type: project-context-scanner
361+
task_status: completed
362+
actions:
363+
- type: run_script
364+
script_path: ./.abathur/hooks/validate_project_context_memory.sh
365+
args: ["${task_id}"]
366+
- type: log_message
367+
level: info
368+
message: "Validating project context memory for task ${task_id}"
369+
priority: 10
370+
enabled: true
371+
352372
# ============================================================================
353373
# CHILD SPAWNING HOOKS
354374
# ============================================================================

template/.abathur/hooks/README.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,36 @@ Validates technical requirements before allowing a technical-requirements-specia
7676
args: ["${task_id}", "${parent_task_id}"]
7777
```
7878

79+
### `validate_project_context_memory.sh`
80+
81+
Validates that the project-context-scanner agent successfully saved project context to memory. If validation fails, automatically re-enqueues the scanner task for retry.
82+
83+
**Usage:**
84+
```bash
85+
./validate_project_context_memory.sh <task_id>
86+
```
87+
88+
**What it validates:**
89+
- Memory entry exists at `project:context/metadata`
90+
- Contains required fields: language, frameworks, tooling, validation_requirements
91+
- Primary language is detected
92+
- Validation agent is specified
93+
94+
**Hook Configuration:**
95+
```yaml
96+
- event: post_complete
97+
conditions:
98+
- agent_type: project-context-scanner
99+
task_status: completed
100+
actions:
101+
- type: run_script
102+
script_path: ./.abathur/hooks/validate_project_context_memory.sh
103+
args: ["${task_id}"]
104+
```
105+
106+
**Retry Logic:**
107+
If validation fails, the hook automatically re-enqueues a new project-context-scanner task with priority 10 to retry the scan.
108+
79109
### `integration_test.sh`
80110

81111
Runs integration tests when a feature branch completes all its tasks.
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
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

Comments
 (0)