-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdependency_management.py
More file actions
634 lines (523 loc) · 20.1 KB
/
Copy pathdependency_management.py
File metadata and controls
634 lines (523 loc) · 20.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
#!/usr/bin/env python3
"""
Task Orchestrator - Advanced Dependency Management Examples
This script demonstrates sophisticated dependency management patterns,
including complex workflows, parallel execution paths, and dependency
optimization strategies.
Use cases covered:
- Feature development workflows
- Release preparation processes
- Parallel development coordination
- Dependency optimization
- Complex project structures
"""
import sys
import os
import time
from pathlib import Path
from datetime import datetime, timedelta
# Add the project root to the Python path
project_root = Path(__file__).parent.parent.parent
sys.path.insert(0, str(project_root))
try:
from src.tm_production import TaskManager
from src.error_handler import ValidationError
from src.dependency_graph import CircularDependencyError
except ImportError as e:
print(f"Error importing TaskManager: {e}")
print("Make sure you're running this from the task-orchestrator directory")
sys.exit(1)
def print_separator(title):
"""Print a formatted section separator."""
print(f"\n{'='*70}")
print(f" {title}")
print('='*70)
def print_dependency_graph(tm, title="Dependency Graph"):
"""Print tasks showing dependency relationships."""
print(f"\n{title}:")
print("-" * 50)
tasks = tm.list_tasks()
if not tasks:
print("No tasks found.")
return
# Group by status for better visualization
status_groups = {
'pending': [],
'in_progress': [],
'completed': [],
'blocked': []
}
for task in tasks:
status_groups.setdefault(task['status'], []).append(task)
status_symbols = {
'pending': '○',
'in_progress': '◐',
'completed': '●',
'blocked': '⊘',
'cancelled': '✗'
}
for status, task_list in status_groups.items():
if task_list:
print(f"\n{status.upper()}:")
for task in task_list:
symbol = status_symbols.get(status, '?')
deps = f" ← depends on: {','.join(task['dependencies'])}" if task['dependencies'] else ""
blocks = f" → blocks: {','.join(task['blocks'])}" if task['blocks'] else ""
priority_marker = "!" if task['priority'] in ['high', 'critical'] else ""
print(f" {symbol} [{task['id']}] {priority_marker}{task['title']}")
if deps:
print(f" {deps}")
if blocks:
print(f" {blocks}")
def create_feature_development_workflow():
"""Create a realistic feature development workflow with dependencies."""
print_separator("Feature Development Workflow")
tm = TaskManager()
print("Creating a complete feature development workflow...")
print("This simulates developing a 'User Profile Management' feature.")
# 1. Planning and Design Phase
epic_id = tm.add_task(
"User Profile Management Feature Epic",
description="Complete user profile management system with editing, privacy controls, and image upload",
priority="high",
tags=["epic", "user-profile"]
)
print(f"✓ Epic created: {epic_id}")
# Design tasks
requirements_id = tm.add_task(
"Gather user profile requirements",
depends_on=[epic_id],
priority="critical",
tags=["planning", "requirements"]
)
ui_design_id = tm.add_task(
"Design user profile UI mockups",
depends_on=[requirements_id],
priority="high",
tags=["design", "ui"]
)
api_design_id = tm.add_task(
"Design profile API specification",
depends_on=[requirements_id],
priority="high",
tags=["design", "api"]
)
# 2. Backend Development
db_schema_id = tm.add_task(
"Create user profile database schema",
depends_on=[api_design_id],
priority="high",
tags=["backend", "database"]
)
api_impl_id = tm.add_task(
"Implement profile API endpoints",
depends_on=[api_design_id, db_schema_id],
priority="high",
tags=["backend", "api"]
)
image_upload_id = tm.add_task(
"Implement image upload service",
depends_on=[api_design_id],
priority="medium",
tags=["backend", "images"]
)
# 3. Frontend Development (can start after UI design)
components_id = tm.add_task(
"Build profile UI components",
depends_on=[ui_design_id],
priority="medium",
tags=["frontend", "components"]
)
forms_id = tm.add_task(
"Create profile edit forms",
depends_on=[components_id],
priority="medium",
tags=["frontend", "forms"]
)
integration_id = tm.add_task(
"Integrate frontend with API",
depends_on=[forms_id, api_impl_id],
priority="high",
tags=["frontend", "integration"]
)
# 4. Testing Phase
backend_tests_id = tm.add_task(
"Write backend unit tests",
depends_on=[api_impl_id, image_upload_id],
priority="high",
tags=["testing", "backend"]
)
frontend_tests_id = tm.add_task(
"Write frontend component tests",
depends_on=[integration_id],
priority="high",
tags=["testing", "frontend"]
)
e2e_tests_id = tm.add_task(
"Create end-to-end tests",
depends_on=[integration_id, backend_tests_id],
priority="medium",
tags=["testing", "e2e"]
)
# 5. Deployment and Documentation
docs_id = tm.add_task(
"Update user documentation",
depends_on=[integration_id],
priority="medium",
tags=["documentation"]
)
deployment_id = tm.add_task(
"Deploy to staging environment",
depends_on=[e2e_tests_id, frontend_tests_id, docs_id],
priority="critical",
tags=["deployment", "staging"]
)
print_dependency_graph(tm, "Initial Feature Workflow")
return {
'epic_id': epic_id,
'requirements_id': requirements_id,
'ui_design_id': ui_design_id,
'api_design_id': api_design_id,
'deployment_id': deployment_id
}
def simulate_parallel_development():
"""Demonstrate parallel development streams with smart dependencies."""
print_separator("Parallel Development Coordination")
tm = TaskManager()
print("Creating parallel development streams that can work independently...")
# Core infrastructure (blocking for others)
auth_service_id = tm.add_task(
"Implement authentication service",
priority="critical",
tags=["infrastructure", "auth"]
)
database_id = tm.add_task(
"Set up production database",
priority="critical",
tags=["infrastructure", "database"]
)
# Team A: User Management
team_a_lead_id = tm.add_task(
"User management API design",
depends_on=[auth_service_id],
priority="high",
tags=["team-a", "api"]
)
user_crud_id = tm.add_task(
"Implement user CRUD operations",
depends_on=[team_a_lead_id, database_id],
priority="high",
tags=["team-a", "backend"]
)
user_ui_id = tm.add_task(
"Build user management UI",
depends_on=[team_a_lead_id], # Can start with just API design
priority="medium",
tags=["team-a", "frontend"]
)
# Team B: Content Management (independent)
content_api_id = tm.add_task(
"Content management API design",
depends_on=[database_id], # Only needs database
priority="high",
tags=["team-b", "api"]
)
content_crud_id = tm.add_task(
"Implement content CRUD operations",
depends_on=[content_api_id],
priority="high",
tags=["team-b", "backend"]
)
content_ui_id = tm.add_task(
"Build content management UI",
depends_on=[content_api_id],
priority="medium",
tags=["team-b", "frontend"]
)
# Team C: Analytics (depends on both teams)
analytics_design_id = tm.add_task(
"Design analytics data collection",
depends_on=[user_crud_id, content_crud_id],
priority="medium",
tags=["team-c", "analytics"]
)
analytics_impl_id = tm.add_task(
"Implement analytics tracking",
depends_on=[analytics_design_id],
priority="low",
tags=["team-c", "analytics"]
)
# Integration tasks (require multiple teams)
integration_tests_id = tm.add_task(
"Cross-team integration tests",
depends_on=[user_ui_id, content_ui_id, analytics_impl_id],
priority="high",
tags=["integration", "testing"]
)
print_dependency_graph(tm, "Parallel Development Streams")
# Simulate completing infrastructure first
print("\n1. Completing infrastructure tasks...")
tm.complete_task(auth_service_id)
tm.complete_task(database_id)
print_dependency_graph(tm, "After Infrastructure Completion")
# Show how teams can now work in parallel
ready_tasks = tm.list_tasks(status="pending")
print(f"\nTasks ready for parallel development: {len(ready_tasks)}")
for task in ready_tasks:
team = [tag for tag in task.get('tags', []) if tag.startswith('team-')]
team_name = team[0] if team else "shared"
print(f" - {team_name}: [{task['id']}] {task['title']}")
def demonstrate_release_workflow():
"""Create a complex release preparation workflow."""
print_separator("Release Workflow Management")
tm = TaskManager()
print("Creating release preparation workflow for v2.7.2...")
# Release planning
release_planning_id = tm.add_task(
"Plan v2.7.2 release scope",
priority="critical",
tags=["release", "planning"]
)
# Feature completion (parallel)
feature1_id = tm.add_task(
"Complete authentication redesign",
depends_on=[release_planning_id],
priority="critical",
tags=["release", "feature"]
)
feature2_id = tm.add_task(
"Complete dashboard improvements",
depends_on=[release_planning_id],
priority="high",
tags=["release", "feature"]
)
feature3_id = tm.add_task(
"Complete mobile responsiveness",
depends_on=[release_planning_id],
priority="medium",
tags=["release", "feature"]
)
# Code quality tasks
security_audit_id = tm.add_task(
"Perform security audit",
depends_on=[feature1_id], # Auth changes need security review
priority="critical",
tags=["release", "security"]
)
performance_testing_id = tm.add_task(
"Run performance tests",
depends_on=[feature1_id, feature2_id, feature3_id],
priority="high",
tags=["release", "performance"]
)
# Documentation tasks
changelog_id = tm.add_task(
"Update changelog",
depends_on=[feature1_id, feature2_id, feature3_id],
priority="medium",
tags=["release", "documentation"]
)
api_docs_id = tm.add_task(
"Update API documentation",
depends_on=[feature1_id, feature2_id], # Only features affecting API
priority="medium",
tags=["release", "documentation"]
)
user_guide_id = tm.add_task(
"Update user guide",
depends_on=[feature2_id, feature3_id], # UI-affecting features
priority="low",
tags=["release", "documentation"]
)
# Pre-release validation
staging_deployment_id = tm.add_task(
"Deploy to staging",
depends_on=[security_audit_id, performance_testing_id],
priority="critical",
tags=["release", "deployment"]
)
qa_testing_id = tm.add_task(
"QA acceptance testing",
depends_on=[staging_deployment_id],
priority="critical",
tags=["release", "qa"]
)
# Release tasks
version_bump_id = tm.add_task(
"Version bump and tagging",
depends_on=[qa_testing_id, changelog_id],
priority="critical",
tags=["release", "versioning"]
)
production_deployment_id = tm.add_task(
"Deploy to production",
depends_on=[version_bump_id],
priority="critical",
tags=["release", "deployment"]
)
# Post-release
monitoring_id = tm.add_task(
"Monitor production deployment",
depends_on=[production_deployment_id],
priority="high",
tags=["release", "monitoring"]
)
announcement_id = tm.add_task(
"Release announcement",
depends_on=[production_deployment_id, api_docs_id, user_guide_id],
priority="medium",
tags=["release", "communication"]
)
print_dependency_graph(tm, "Release Workflow")
# Show critical path
critical_tasks = tm.list_tasks()
critical_path = [t for t in critical_tasks if t['priority'] == 'critical']
print(f"\nCritical path tasks ({len(critical_path)} tasks):")
for task in critical_path:
deps = f" (depends on {len(task['dependencies'])} tasks)" if task['dependencies'] else ""
print(f" - [{task['id']}] {task['title']}{deps}")
def analyze_workflow_efficiency():
"""Analyze and optimize workflow dependencies."""
print_separator("Workflow Optimization Analysis")
tm = TaskManager()
# Create a suboptimal workflow first
print("Creating a workflow with optimization opportunities...")
# Sequential workflow (suboptimal)
task1 = tm.add_task("Research requirements", priority="high")
task2 = tm.add_task("Write specification", depends_on=[task1], priority="high")
task3 = tm.add_task("Design database schema", depends_on=[task2], priority="medium")
task4 = tm.add_task("Design API", depends_on=[task2], priority="medium")
task5 = tm.add_task("Design UI mockups", depends_on=[task2], priority="low")
task6 = tm.add_task("Implement database", depends_on=[task3], priority="medium")
task7 = tm.add_task("Implement API", depends_on=[task4, task6], priority="high")
task8 = tm.add_task("Implement UI", depends_on=[task5, task7], priority="medium")
task9 = tm.add_task("Integration testing", depends_on=[task8], priority="high")
task10 = tm.add_task("Deploy", depends_on=[task9], priority="critical")
print_dependency_graph(tm, "Original Workflow")
# Analyze bottlenecks
print("\nWorkflow Analysis:")
all_tasks = tm.list_tasks()
# Find tasks with no dependencies (can start immediately)
ready_tasks = [t for t in all_tasks if not t['dependencies']]
print(f"✓ Tasks that can start immediately: {len(ready_tasks)}")
# Find tasks that block many others
blocking_tasks = [(t, len(t['blocks'])) for t in all_tasks if t['blocks']]
blocking_tasks.sort(key=lambda x: x[1], reverse=True)
print("✓ Tasks blocking the most others:")
for task, block_count in blocking_tasks[:3]:
print(f" [{task['id']}] {task['title']} (blocks {block_count} tasks)")
# Find longest dependency chains
def get_dependency_depth(task_id, tasks_dict, visited=None):
if visited is None:
visited = set()
if task_id in visited:
return 0
visited.add(task_id)
task = tasks_dict.get(task_id)
if not task or not task['dependencies']:
return 1
max_depth = 0
for dep_id in task['dependencies']:
depth = get_dependency_depth(dep_id, tasks_dict, visited.copy())
max_depth = max(max_depth, depth)
return max_depth + 1
tasks_dict = {t['id']: t for t in all_tasks}
depths = [(t, get_dependency_depth(t['id'], tasks_dict)) for t in all_tasks]
depths.sort(key=lambda x: x[1], reverse=True)
print("✓ Longest dependency chains:")
for task, depth in depths[:3]:
print(f" [{task['id']}] {task['title']} (depth: {depth})")
print("\nOptimization Recommendations:")
print("1. Tasks 3, 4, 5 could be parallelized after task 2")
print("2. Consider if UI design really needs to wait for full specification")
print("3. Some testing could happen in parallel with development")
print("4. Documentation tasks could start earlier in the process")
def demonstrate_advanced_patterns():
"""Show advanced dependency patterns and edge cases."""
print_separator("Advanced Dependency Patterns")
tm = TaskManager()
print("1. Diamond Dependency Pattern:")
print(" A common pattern where multiple tasks depend on a common ancestor")
# A
# / \
# B C
# \ /
# D
a_id = tm.add_task("Define project requirements", priority="critical")
b_id = tm.add_task("Backend development", depends_on=[a_id], priority="high")
c_id = tm.add_task("Frontend development", depends_on=[a_id], priority="high")
d_id = tm.add_task("Integration testing", depends_on=[b_id, c_id], priority="high")
print(f" A (requirements): {a_id}")
print(f" B (backend): {b_id}")
print(f" C (frontend): {c_id}")
print(f" D (integration): {d_id}")
print("\n2. Fan-out Pattern:")
print(" One task enables many parallel tasks")
foundation_id = tm.add_task("Set up development environment", priority="critical")
parallel_tasks = []
for i, task_name in enumerate([
"Set up linting rules",
"Configure testing framework",
"Set up CI/CD pipeline",
"Create project documentation template",
"Set up monitoring"
]):
task_id = tm.add_task(task_name, depends_on=[foundation_id], priority="medium")
parallel_tasks.append(task_id)
print(f" Foundation: {foundation_id}")
print(f" Parallel tasks: {len(parallel_tasks)} tasks can run after foundation")
print("\n3. Convergent Pattern:")
print(" Many tasks converge to a single milestone")
milestone_deps = []
for task_name in [
"Complete user authentication",
"Complete data validation",
"Complete error handling",
"Complete logging",
"Complete security review"
]:
task_id = tm.add_task(task_name, priority="high")
milestone_deps.append(task_id)
milestone_id = tm.add_task(
"Release candidate ready",
depends_on=milestone_deps,
priority="critical"
)
print(f" Milestone: {milestone_id} (depends on {len(milestone_deps)} tasks)")
print_dependency_graph(tm, "Advanced Dependency Patterns")
def main():
"""Run all dependency management demonstrations."""
print("Task Orchestrator - Advanced Dependency Management")
print("==================================================")
print("This script demonstrates sophisticated dependency management patterns")
print("used in real-world development workflows.")
try:
# Initialize clean database for examples
tm = TaskManager()
tm.init_db()
# Run demonstrations
feature_workflow = create_feature_development_workflow()
time.sleep(1)
simulate_parallel_development()
time.sleep(1)
demonstrate_release_workflow()
time.sleep(1)
analyze_workflow_efficiency()
time.sleep(1)
demonstrate_advanced_patterns()
print_separator("Dependency Management Examples Complete")
print("All advanced dependency patterns have been demonstrated!")
print("\nKey Takeaways:")
print("1. Smart dependencies enable parallel work")
print("2. Critical path identification helps prioritize work")
print("3. Dependency analysis can reveal optimization opportunities")
print("4. Different patterns suit different project structures")
print(f"\nDatabase: {tm.db_path}")
print("Run './tm list --has-deps' to see all tasks with dependencies")
except Exception as e:
print(f"\nError during demonstration: {e}")
import traceback
traceback.print_exc()
sys.exit(1)
if __name__ == "__main__":
main()