feat: enhance task execution with intelligent grouping and dependency analysis

This commit is contained in:
catlog22
2025-11-22 20:57:16 +08:00
parent a98b26b111
commit 530c348e95

View File

@@ -185,89 +185,276 @@ Execution Complete
previousExecutionResults = [] previousExecutionResults = []
``` ```
### Step 2: Create TodoWrite Execution List ### Step 2: Intelligent Task Grouping & Execution Planning
**Operations**: **Operations**:
- Create execution tracking from task list - Analyze task dependencies and file targets
- Typically single execution call for all tasks - Group tasks based on context similarity and dependencies
- Split into multiple calls if task list very large (>10 tasks) - Support parallel execution for independent tasks
- Respect task limits per execution (Agent: 7 tasks, CLI: 4 tasks)
**Execution Call Creation**: **Task Analysis & Grouping**:
```javascript ```javascript
function createExecutionCalls(tasks) { // Extract file path from task for dependency analysis
const taskTitles = tasks.map(t => t.title || t) function getTaskFile(task) {
return task.file || task.title.match(/in\s+([^\s:]+)/)?.[1] || null
}
// Single call for ≤10 tasks (most common) // Infer dependencies from task descriptions and file paths
if (tasks.length <= 10) { function inferDependencies(tasks) {
return [{ return tasks.map((task, index) => {
method: executionMethod === "Codex" ? "Codex" : "Agent", const dependencies = []
taskSummary: taskTitles.length <= 3 const taskFile = getTaskFile(task)
? taskTitles.join(', ') const keywords = task.description?.toLowerCase() || task.title.toLowerCase()
: `${taskTitles.slice(0, 2).join(', ')}, and ${taskTitles.length - 2} more`,
tasks: tasks // Check previous tasks for dependencies
}] for (let i = 0; i < index; i++) {
const prevTask = tasks[i]
const prevFile = getTaskFile(prevTask)
// Same file modification → sequential dependency
if (taskFile && prevFile === taskFile) {
dependencies.push(i)
continue
}
// Keyword-based dependency detection
if (keywords.includes('use') || keywords.includes('integrate') ||
keywords.includes('call') || keywords.includes('import')) {
const prevTitle = prevTask.title.toLowerCase()
if (keywords.includes(prevTitle.split(' ')[0])) {
dependencies.push(i)
}
}
}
return { ...task, taskIndex: index, dependencies }
})
}
// Group tasks into execution batches
function createExecutionCalls(tasks, executionMethod) {
const tasksWithDeps = inferDependencies(tasks)
const maxTasksPerCall = executionMethod === "Codex" ? 4 : 7
const calls = []
const processed = new Set()
// Phase 1: Group independent tasks for parallel execution
const parallelGroups = []
tasksWithDeps.forEach(task => {
if (task.dependencies.length === 0 && !processed.has(task.taskIndex)) {
const group = [task]
processed.add(task.taskIndex)
// Find other independent tasks with different files for parallel batch
const taskFile = getTaskFile(task)
tasksWithDeps.forEach(other => {
if (other.dependencies.length === 0 &&
!processed.has(other.taskIndex) &&
group.length < maxTasksPerCall) {
const otherFile = getTaskFile(other)
// Only group if different files (avoid conflicts)
if (!taskFile || !otherFile || taskFile !== otherFile) {
group.push(other)
processed.add(other.taskIndex)
}
}
})
parallelGroups.push(group)
}
})
// Phase 2: Group dependent tasks sequentially
const dependentTasks = tasksWithDeps.filter(t => !processed.has(t.taskIndex))
const sequentialBatches = []
while (dependentTasks.length > 0) {
const batch = []
const batchIndices = new Set()
for (const task of dependentTasks) {
// Can add to batch if dependencies are already processed
const depsProcessed = task.dependencies.every(dep =>
processed.has(dep) || batchIndices.has(dep)
)
if (depsProcessed && batch.length < maxTasksPerCall) {
batch.push(task)
batchIndices.add(task.taskIndex)
processed.add(task.taskIndex)
}
}
if (batch.length === 0) break // Prevent infinite loop
sequentialBatches.push(batch)
dependentTasks.splice(0, dependentTasks.length,
...dependentTasks.filter(t => !processed.has(t.taskIndex))
)
} }
// Split into multiple calls for >10 tasks // Combine parallel and sequential batches
const callSize = 5 parallelGroups.forEach((group, i) => {
const calls = []
for (let i = 0; i < tasks.length; i += callSize) {
const batchTasks = tasks.slice(i, i + callSize)
const batchTitles = batchTasks.map(t => t.title || t)
calls.push({ calls.push({
method: executionMethod === "Codex" ? "Codex" : "Agent", method: executionMethod === "Codex" ? "Codex" : "Agent",
taskSummary: `Tasks ${i + 1}-${Math.min(i + callSize, tasks.length)}: ${batchTitles[0]}...`, executionType: "parallel",
tasks: batchTasks groupId: `P${i + 1}`,
taskSummary: group.map(t => t.title).join(' | '),
tasks: group
}) })
} })
sequentialBatches.forEach((batch, i) => {
calls.push({
method: executionMethod === "Codex" ? "Codex" : "Agent",
executionType: "sequential",
groupId: `S${i + 1}`,
taskSummary: batch.map(t => t.title).join(' → '),
tasks: batch
})
})
return calls return calls
} }
// Create execution calls with IDs // Create execution calls with IDs and execution method
executionCalls = createExecutionCalls(planObject.tasks).map((call, index) => ({ executionCalls = createExecutionCalls(planObject.tasks, executionMethod).map((call, index) => ({
...call, ...call,
id: `[${call.method}-${index+1}]` id: `[${call.groupId}]`
})) }))
// Create TodoWrite list // Create TodoWrite list with execution type indicators
TodoWrite({ TodoWrite({
todos: executionCalls.map(call => ({ todos: executionCalls.map(call => {
content: `${call.id} (${call.taskSummary})`, const typeIcon = call.executionType === "parallel" ? "⚡" : "→"
status: "pending", const typeLabel = call.executionType === "parallel" ? "Parallel" : "Sequential"
activeForm: `Executing ${call.id} (${call.taskSummary})` return {
})) content: `${typeIcon} ${call.id} [${typeLabel}] (${call.tasks.length} tasks: ${call.taskSummary})`,
status: "pending",
activeForm: `Executing ${call.id} - ${typeLabel} batch (${call.tasks.length} tasks)`
}
})
}) })
``` ```
**Example Execution Lists**: **Grouping Strategy Examples**:
```javascript
// Example 1: Independent tasks, different files → Parallel groups
Tasks: [
{ title: "Create auth.ts", file: "src/auth.ts" },
{ title: "Create utils.ts", file: "src/utils.ts" },
{ title: "Create types.ts", file: "src/types.ts" }
]
Result: [P1] Parallel (3 tasks)
// Example 2: Same file modifications → Sequential batch
Tasks: [
{ title: "Add function in auth.ts", file: "src/auth.ts" },
{ title: "Update function in auth.ts", file: "src/auth.ts" }
]
Result: [S1] Sequential (2 tasks)
// Example 3: Mixed dependencies → Multiple batches
Tasks: [
{ title: "Create base.ts", file: "src/base.ts" }, // No deps
{ title: "Create helper.ts", file: "src/helper.ts" }, // No deps
{ title: "Use base in main.ts", file: "src/main.ts" }, // Depends on base.ts
{ title: "Use helper in app.ts", file: "src/app.ts" } // Depends on helper.ts
]
Result:
[P1] Parallel (2 tasks: base.ts | helper.ts)
[P2] Parallel (2 tasks: main.ts | app.ts)
// Example 4: Exceeds batch limit → Multiple batches
Agent (7 tasks max): 10 tasks [P1: 7 tasks] + [P2: 3 tasks]
Codex (4 tasks max): 10 tasks [P1: 4 tasks] + [P2: 4 tasks] + [P3: 2 tasks]
``` ```
Single call (typical):
[ ] [Agent-1] (Create AuthService, Add JWT utilities, Implement middleware)
Few tasks: **TodoWrite Display Examples**:
[ ] [Codex-1] (Create AuthService, Add JWT utilities, and 3 more) ```
Parallel execution (independent files):
[ ] ⚡ [P1] [Parallel] (3 tasks: Create auth.ts | Create utils.ts | Create types.ts)
Large task sets (>10): Sequential execution (same file or dependencies):
[ ] [Agent-1] (Tasks 1-5: Create AuthService, Add JWT utilities, ...) [ ] → [S1] [Sequential] (2 tasks: Add function in auth.ts → Update function in auth.ts)
[ ] [Agent-2] (Tasks 6-10: Create tests, Update docs, ...)
Mixed execution (multiple batches):
[ ] ⚡ [P1] [Parallel] (4 tasks: Create base.ts | Create helper.ts | Create config.ts | Create types.ts)
[ ] ⚡ [P2] [Parallel] (3 tasks: Use base in main.ts | Use helper in app.ts | Use config in index.ts)
[ ] → [S1] [Sequential] (2 tasks: Integrate components → Add tests)
``` ```
### Step 3: Launch Execution ### Step 3: Launch Execution
**IMPORTANT**: CLI execution MUST run in foreground (no background execution) **Execution Strategy**:
- **Parallel batches**: Launch all parallel batches simultaneously (CLI has no concurrency limit)
- **Sequential batches**: Execute in order, waiting for previous completion
- **Mixed workflow**: Process parallel groups first, then sequential groups
**Execution Loop**: **Execution Loop**:
```javascript ```javascript
for (currentIndex = 0; currentIndex < executionCalls.length; currentIndex++) { // Phase 1: Launch all parallel batches concurrently
const currentCall = executionCalls[currentIndex] const parallelCalls = executionCalls.filter(c => c.executionType === "parallel")
const sequentialCalls = executionCalls.filter(c => c.executionType === "sequential")
// Update TodoWrite: mark current call in_progress if (parallelCalls.length > 0) {
// Launch execution with previousExecutionResults context // Mark all parallel calls as in_progress
// After completion: collect result, add to previousExecutionResults TodoWrite({
// Update TodoWrite: mark current call completed todos: executionCalls.map(call => ({
content: `${call.executionType === "parallel" ? "⚡" : "→"} ${call.id} [${call.executionType}] (${call.tasks.length} tasks)`,
status: call.executionType === "parallel" ? "in_progress" : "pending",
activeForm: `Executing ${call.id}`
}))
})
// Launch all parallel batches using single message with multiple tool calls
// Use Task tool or Bash tool to launch each parallel execution
// Collect results as they complete
parallelResults = await Promise.all(parallelCalls.map(call => executeBatch(call)))
previousExecutionResults.push(...parallelResults)
// Mark parallel calls as completed
TodoWrite({
todos: executionCalls.map(call => ({
content: `${call.executionType === "parallel" ? "⚡" : "→"} ${call.id} [${call.executionType}] (${call.tasks.length} tasks)`,
status: parallelCalls.includes(call) ? "completed" : "pending",
activeForm: `Executing ${call.id}`
}))
})
}
// Phase 2: Execute sequential batches in order
for (const call of sequentialCalls) {
// Update TodoWrite: mark current sequential call in_progress
TodoWrite({
todos: executionCalls.map(c => ({
content: `${c.executionType === "parallel" ? "⚡" : "→"} ${c.id} [${c.executionType}] (${c.tasks.length} tasks)`,
status: c === call ? "in_progress" : (parallelCalls.includes(c) ? "completed" : "pending"),
activeForm: `Executing ${c.id}`
}))
})
// Launch sequential execution with previousExecutionResults context
result = await executeBatch(call)
previousExecutionResults.push(result)
// Update TodoWrite: mark completed
TodoWrite({
todos: executionCalls.map(c => ({
content: `${c.executionType === "parallel" ? "⚡" : "→"} ${c.id} [${c.executionType}] (${c.tasks.length} tasks)`,
status: c.id <= call.id ? "completed" : "pending",
activeForm: `Executing ${c.id}`
}))
})
} }
``` ```
**Important Notes**:
- Parallel batches use single Claude message with multiple Bash/Task tool calls
- CLI tools (Codex/Gemini/Qwen) execute in foreground (NOT background)
- Each batch receives `previousExecutionResults` for context continuity
**Option A: Agent Execution** **Option A: Agent Execution**
When to use: When to use:
@@ -331,13 +518,9 @@ ${result.notes ? `Notes: ${result.notes}` : ''}
Read these files for detailed architecture, patterns, and constraints.` : ''} Read these files for detailed architecture, patterns, and constraints.` : ''}
## Instructions ## Requirements
- Reference original request to ensure alignment MUST complete ALL ${planObject.tasks.length} tasks listed above in this single execution.
- Review previous results to understand completed work Return only after all tasks are fully implemented and tested.
- Build on previous work, avoid duplication
- Test functionality as you implement
- Complete all assigned tasks
- Read planning artifact files for detailed context when needed
` `
) )
``` ```
@@ -413,13 +596,9 @@ ${executionContext.session.artifacts.exploration ? `- Exploration: ${executionCo
Read these files for complete architecture details, code patterns, and integration constraints. Read these files for complete architecture details, code patterns, and integration constraints.
` : ''} ` : ''}
## Execution Instructions ## Requirements
- Reference original request to ensure alignment MUST complete ALL ${planObject.tasks.length} tasks listed above in this single execution.
- Review previous results for context continuity Return only after all tasks are fully implemented and tested.
- Build on previous work, don't duplicate completed tasks
- Complete all assigned tasks in single execution
- Test functionality as you implement
${executionContext?.session?.artifacts ? `- Read planning artifact files for detailed architecture and pattern guidance` : ''}
Complexity: ${planObject.complexity} Complexity: ${planObject.complexity}
" --skip-git-repo-check -s danger-full-access " --skip-git-repo-check -s danger-full-access
@@ -440,110 +619,113 @@ bash_result = Bash(
### Step 4: Track Execution Progress ### Step 4: Track Execution Progress
**Real-time TodoWrite Updates** at execution call level: **Real-time TodoWrite Updates** at batch level:
```javascript ```javascript
// When call starts // Parallel batches executing concurrently
TodoWrite({ TodoWrite({
todos: [ todos: [
{ content: "[Agent-1] (Implement auth + Create JWT utils)", status: "in_progress", activeForm: "..." }, { content: "⚡ [P1] [Parallel] (4 tasks)", status: "in_progress", activeForm: "Executing P1" },
{ content: "[Agent-2] (Add middleware + Update routes)", status: "pending", activeForm: "..." } { content: "⚡ [P2] [Parallel] (3 tasks)", status: "in_progress", activeForm: "Executing P2" },
{ content: "→ [S1] [Sequential] (2 tasks)", status: "pending", activeForm: "..." }
] ]
}) })
// When call completes // After parallel completion, sequential execution
TodoWrite({ TodoWrite({
todos: [ todos: [
{ content: "[Agent-1] (Implement auth + Create JWT utils)", status: "completed", activeForm: "..." }, { content: "⚡ [P1] [Parallel] (4 tasks)", status: "completed", activeForm: "..." },
{ content: "[Agent-2] (Add middleware + Update routes)", status: "in_progress", activeForm: "..." } { content: "⚡ [P2] [Parallel] (3 tasks)", status: "completed", activeForm: "..." },
{ content: "→ [S1] [Sequential] (2 tasks)", status: "in_progress", activeForm: "Executing S1" }
] ]
}) })
``` ```
**User Visibility**: **User Visibility**:
- User sees execution call progress (not individual task progress) - Parallel batches show simultaneous execution (multiple "in_progress" at once)
- Current execution highlighted as "in_progress" - Sequential batches execute one at a time
- Completed executions marked with checkmark - Icons distinguish parallel (⚡) from sequential (→)
- Each execution shows task summary for context - Task count shows batch size for each execution group
### Step 5: Code Review (Optional) ### Step 5: Code Review (Optional)
**Skip Condition**: Only run if `codeReviewTool ≠ "Skip"` **Skip Condition**: Only run if `codeReviewTool ≠ "Skip"`
**Operations**: **Review Focus**: Verify implementation against task.json acceptance criteria
- Agent Review: Current agent performs direct review - Read task.json from session artifacts for acceptance criteria
- Gemini Review: Execute gemini CLI with review prompt - Check each acceptance criterion is fulfilled
- Custom tool: Execute specified CLI tool (qwen, codex, etc.) - Validate code quality and identify issues
- Ensure alignment with planned approach
**Command Formats**: **Operations**:
- Agent Review: Current agent performs direct review (read task.json for acceptance criteria)
- Gemini Review: Execute gemini CLI with review prompt (task.json in CONTEXT)
- Custom tool: Execute specified CLI tool (qwen, codex, etc.) with task.json reference
**Unified Review Template** (All tools use same standard):
**Review Criteria**:
- **Acceptance Criteria**: Verify each criterion from task.json `context.acceptance`
- **Code Quality**: Analyze quality, identify issues, suggest improvements
- **Plan Alignment**: Validate implementation matches planned approach
**Shared Prompt Template** (used by all CLI tools):
```
PURPOSE: Code review for implemented changes against task.json acceptance criteria
TASK: • Verify task.json acceptance criteria fulfillment • Analyze code quality • Identify issues • Suggest improvements • Validate plan adherence
MODE: analysis
CONTEXT: @**/* @{task.json} @{plan.json} [@{exploration.json}] | Memory: Review lite-execute changes against task.json requirements
EXPECTED: Quality assessment with acceptance criteria verification, issue identification, and recommendations. Explicitly check each acceptance criterion from task.json.
RULES: $(cat ~/.claude/workflows/cli-templates/prompts/analysis/02-review-code-quality.txt) | Focus on task.json acceptance criteria and plan adherence | analysis=READ-ONLY
```
**Tool-Specific Execution** (Apply shared prompt template above):
```bash ```bash
# Agent Review: Direct agent review (no CLI) # Method 1: Agent Review (current agent)
# Uses analysis prompt and TodoWrite tools directly # - Read task.json: ${executionContext.session.artifacts.task}
# - Apply unified review criteria (see Shared Prompt Template)
# - Report findings directly
# Gemini Review: # Method 2: Gemini Review (recommended)
# Include artifact paths for planning context reference gemini -p "[Shared Prompt Template with artifacts]"
${executionContext?.session?.artifacts ? ` # CONTEXT includes: @**/* @${task.json} @${plan.json} [@${exploration.json}]
gemini -p "
PURPOSE: Code review for implemented changes against planned approach
TASK: • Analyze quality • Identify issues • Suggest improvements • Verify alignment with plan
MODE: analysis
CONTEXT: @**/* @${executionContext.session.artifacts.plan}${executionContext.session.artifacts.exploration ? ` @${executionContext.session.artifacts.exploration}` : ''} | Memory: Review lite-execute changes with planning context
EXPECTED: Quality assessment with recommendations and plan alignment check
RULES: $(cat ~/.claude/workflows/cli-templates/prompts/analysis/02-review-code-quality.txt) | Focus on recent changes and plan adherence | analysis=READ-ONLY
"
` : `
gemini -p "
PURPOSE: Code review for implemented changes
TASK: • Analyze quality • Identify issues • Suggest improvements
MODE: analysis
CONTEXT: @**/* | Memory: Review lite-execute changes
EXPECTED: Quality assessment with recommendations
RULES: $(cat ~/.claude/workflows/cli-templates/prompts/analysis/02-review-code-quality.txt) | Focus on recent changes | analysis=READ-ONLY
"
`}
# Qwen Review (custom tool via "Other"): # Method 3: Qwen Review (alternative)
# Include artifact paths for planning context reference qwen -p "[Shared Prompt Template with artifacts]"
${executionContext?.session?.artifacts ? ` # Same prompt as Gemini, different execution engine
qwen -p "
PURPOSE: Code review for implemented changes against planned approach
TASK: • Analyze quality • Identify issues • Suggest improvements • Verify alignment with plan
MODE: analysis
CONTEXT: @**/* @${executionContext.session.artifacts.plan}${executionContext.session.artifacts.exploration ? ` @${executionContext.session.artifacts.exploration}` : ''} | Memory: Review lite-execute changes with planning context
EXPECTED: Quality assessment with recommendations and plan alignment check
RULES: $(cat ~/.claude/workflows/cli-templates/prompts/analysis/02-review-code-quality.txt) | Focus on recent changes and plan adherence | analysis=READ-ONLY
"
` : `
qwen -p "
PURPOSE: Code review for implemented changes
TASK: • Analyze quality • Identify issues • Suggest improvements
MODE: analysis
CONTEXT: @**/* | Memory: Review lite-execute changes
EXPECTED: Quality assessment with recommendations
RULES: $(cat ~/.claude/workflows/cli-templates/prompts/analysis/02-review-code-quality.txt) | Focus on recent changes | analysis=READ-ONLY
"
`}
# Codex Review (custom tool via "Other"): # Method 4: Codex Review (autonomous)
codex --full-auto exec "Review recent code changes for quality, potential issues, and improvements" --skip-git-repo-check -s danger-full-access codex --full-auto exec "[Verify task.json acceptance criteria at ${task.json}]" --skip-git-repo-check -s danger-full-access
``` ```
**Implementation Note**: Replace `[Shared Prompt Template with artifacts]` placeholder with actual template content, substituting:
- `@{task.json}``@${executionContext.session.artifacts.task}`
- `@{plan.json}``@${executionContext.session.artifacts.plan}`
- `[@{exploration.json}]``@${executionContext.session.artifacts.exploration}` (if exists)
## Best Practices ## Best Practices
### Execution Intelligence ### Execution Intelligence
1. **Context Continuity**: Each execution call receives previous results 1. **Intelligent Task Grouping**: Dependency-aware task allocation
- Prevents duplication across multiple executions - Same file modifications → Sequential batch
- Different files + no dependencies → Parallel batch
- Respects batch limits: Agent (7 tasks), CLI (4 tasks)
- Infers dependencies from file paths and keywords
2. **Parallel Execution**: Maximize throughput for independent tasks
- All parallel batches launch simultaneously
- No concurrency limit for CLI tools
- Reduces total execution time for independent work
- Uses single Claude message with multiple tool calls
3. **Context Continuity**: Each batch receives previous results
- Prevents duplication across batches
- Maintains coherent implementation flow - Maintains coherent implementation flow
- Builds on completed work - Builds on completed work from parallel/sequential batches
2. **Execution Call Tracking**: Progress at call level, not task level 4. **Flexible Execution**: Multiple input modes supported
- Each call handles all or subset of tasks
- Clear visibility of current execution
- Simple progress updates
3. **Flexible Execution**: Multiple input modes supported
- In-memory: Seamless lite-plan integration - In-memory: Seamless lite-plan integration
- Prompt: Quick standalone execution - Prompt: Quick standalone execution
- File: Intelligent format detection - File: Intelligent format detection
@@ -552,15 +734,22 @@ codex --full-auto exec "Review recent code changes for quality, potential issues
### Task Management ### Task Management
1. **Live Progress Updates**: Real-time TodoWrite tracking 1. **Smart Batch Creation**: Automatic dependency analysis
- Execution calls created before execution starts - File path extraction and comparison
- Updated as executions progress - Keyword-based dependency inference
- Clear completion status - Context signature grouping (same files = sequential)
- Batch size optimization per execution method
2. **Simple Execution**: Straightforward task handling 2. **Live Progress Tracking**: Real-time batch-level updates
- All tasks in single call (typical) - Visual distinction: ⚡ (parallel) vs → (sequential)
- Split only for very large task sets (>10) - Shows concurrent execution for parallel batches
- Agent/Codex determines optimal execution order - Clear batch completion tracking
- Task count per batch for progress visibility
3. **Execution Strategies**:
- Parallel-first: All independent batches execute concurrently
- Sequential-after: Dependent batches execute in order
- Context passing: Each batch receives all previous results
## Error Handling ## Error Handling