Add subtask generation decision

Change-Id: If43efa882de08bc262fe6117af7307e97c4dfeda
diff --git a/server/agent/manager.go b/server/agent/manager.go
index 3d8277f..ab625bf 100644
--- a/server/agent/manager.go
+++ b/server/agent/manager.go
@@ -261,16 +261,16 @@
 		return fmt.Errorf("failed to update task status: %w", err)
 	}
 
-	// Check if this task should generate subtasks
+	// Check if this task should generate subtasks (with LLM decision)
 	if m.shouldGenerateSubtasks(task) {
-		log.Printf("Analyzing task %s for subtask generation", task.ID)
+		log.Printf("LLM determined task %s should generate subtasks", task.ID)
 		if err := m.generateSubtasksForTask(ctx, task); err != nil {
 			log.Printf("Warning: Failed to generate subtasks for task %s: %v", task.ID, err)
 			// Continue with normal processing if subtask generation fails
 		} else {
 			// Task has been converted to subtask management, mark as completed
 			task.Status = tm.StatusCompleted
-			task.Solution = "Task broken down into subtasks. See subtasks PR for details."
+			task.Solution = "Task analyzed by LLM and broken down into subtasks with potential new agent creation. See subtasks PR for details."
 			completedAt := time.Now()
 			task.CompletedAt = &completedAt
 			agent.CurrentTask = nil
@@ -279,7 +279,7 @@
 				return fmt.Errorf("failed to update task with subtasks: %w", err)
 			}
 
-			log.Printf("Task %s converted to subtasks by agent %s", task.ID, agent.Name)
+			log.Printf("Task %s converted to subtasks by agent %s using LLM analysis", task.ID, agent.Name)
 			return nil
 		}
 	}
@@ -611,24 +611,37 @@
 	return status
 }
 
-// shouldGenerateSubtasks determines if a task should be broken down into subtasks
+// shouldGenerateSubtasks determines if a task should be broken down into subtasks using LLM
 func (m *Manager) shouldGenerateSubtasks(task *tm.Task) bool {
 	// Don't generate subtasks for subtasks
 	if task.ParentTaskID != "" {
 		return false
 	}
 
-	// Don't generate if already generated
-	if task.SubtasksGenerated {
+	// Don't generate if already evaluated
+	if task.SubtasksEvaluated {
 		return false
 	}
 
-	// Only generate for high priority tasks or complex descriptions
-	if task.Priority == tm.PriorityHigh || len(task.Description) > 200 {
-		return true
+	// Ask LLM to decide
+	ctx := context.Background()
+	decision, err := m.subtaskService.ShouldGenerateSubtasks(ctx, task)
+	if err != nil {
+		log.Printf("Warning: Failed to get LLM subtask decision for task %s: %v", task.ID, err)
+		// Fallback to simple heuristics
+		return task.Priority == tm.PriorityHigh || len(task.Description) > 200
 	}
 
-	return false
+	// Update task to mark as evaluated
+	task.SubtasksEvaluated = true
+	if err := m.taskManager.UpdateTask(task); err != nil {
+		log.Printf("Warning: Failed to update task evaluation status: %v", err)
+	}
+
+	log.Printf("LLM subtask decision for task %s: needs_subtasks=%v, complexity=%d, reasoning=%s", 
+		task.ID, decision.NeedsSubtasks, decision.ComplexityScore, decision.Reasoning)
+
+	return decision.NeedsSubtasks
 }
 
 // generateSubtasksForTask analyzes a task and creates a PR with proposed subtasks
@@ -654,7 +667,14 @@
 	task.SubtasksGenerated = true
 
 	log.Printf("Generated subtask PR for task %s: %s", task.ID, prURL)
-	log.Printf("Proposed %d subtasks for task %s", len(analysis.Subtasks), task.ID)
+	log.Printf("Proposed %d subtasks and %d new agents for task %s", len(analysis.Subtasks), len(analysis.AgentCreations), task.ID)
+	
+	// Log proposed new agents if any
+	if len(analysis.AgentCreations) > 0 {
+		for _, agent := range analysis.AgentCreations {
+			log.Printf("Proposed new agent: %s with skills: %v", agent.Role, agent.Skills)
+		}
+	}
 
 	return nil
 }