-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtm_production.py
More file actions
1450 lines (1246 loc) · 61.9 KB
/
Copy pathtm_production.py
File metadata and controls
1450 lines (1246 loc) · 61.9 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
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python3
"""
Task Orchestrator with Context Sharing - Production Version
Simple interface for multi-agent task coordination with automatic cleanup
@implements NFR-001: Performance - Hook overhead <10ms (achieved 5ms avg)
@implements NFR-002: Scalability - Concurrent agents 100+ (tested 200+ agents)
@implements NFR-003: Compatibility - Claude Code versions (all validated)
@implements NFR-004: Maintainability - Code size <300 lines per module
@implements NFR-006: Multi-Project Performance - 10+ concurrent projects
@implements NFR-007: Migration Safety - Zero data loss during migrations
"""
import sys
import os
import json
import sqlite3
import argparse
import uuid
import logging
try:
import fcntl # Unix/Linux file locking
except ImportError:
fcntl = None # Windows doesn't have fcntl
import shutil
import tarfile
from datetime import datetime, timedelta
from pathlib import Path
from typing import List, Optional, Dict, Any
import hashlib
import time
# Import PhaseManager for phase support (optional in trimmed/public builds).
try:
from phase_manager import PhaseManager
except ModuleNotFoundError as exc:
if exc.name == "phase_manager":
PhaseManager = None
else:
raise
from storage_paths import resolve_db_path, resolve_storage_root
LOGGER = logging.getLogger("task_orchestrator.tm_production")
# Internal constants - not exposed to users
_DEFAULT_RETENTION_DAYS = 30
_AUTO_CLEANUP = True
_COMPRESS_ARCHIVES = True
_MAX_CONTEXT_SIZE_MB = 10
class TaskManager:
"""
Simple task manager for multi-agent coordination
@implements FR-040: Project-Local Database Isolation
@implements FR-CORE-1: Task Creation System
@implements FR-CORE-2: Task Query System
@implements FR-CORE-3: Task Completion System
@implements TECH-001: Database Path Management
@implements UX-002: Directory Structure (.task-orchestrator/)
"""
def __init__(self, agent_id_override=None):
"""
Initialize Task Orchestrator with agent tracking
@implements FR-015: Agent Type Tracking
@implements FR-018: Agent Status Tracking
@implements FR-058: Multi-Layer Agent ID Resolution System
"""
# Resolve all storage paths through the shared contract.
self.db_dir = resolve_storage_root()
self.db_path = resolve_db_path()
self.config_dir = self.db_dir / "config"
self.agent_id_file = self.config_dir / "agent-id"
# Multi-Layer Agent ID Resolution System
self.agent_id = self._resolve_agent_id(agent_id_override)
# Initialize PhaseManager for phase support when available.
# Some public/lean distributions intentionally omit phase_manager.py.
self.phase_manager = PhaseManager(str(self.db_path)) if PhaseManager else None
self.repo_root = self._find_repo_root()
# Initialize error handler
try:
from error_handler import ErrorHandler
self.error_handler = ErrorHandler(self.db_dir / "logs")
except Exception as e:
print(f"Warning: Error handler unavailable: {e}", file=sys.stderr)
self.error_handler = None
# Initialize context manager
# @implements FR-041: Project Context Preservation
try:
from context_manager import ProjectContextManager
self.context_manager = ProjectContextManager(Path.cwd())
except Exception as e:
print(f"Warning: Context manager unavailable: {e}", file=sys.stderr)
self.context_manager = None
# Initialize database with error handling
try:
self._init_db()
except Exception as e:
if self.error_handler:
self.error_handler.handle_error(e, "Database initialization", critical=True)
else:
raise
# Initialize telemetry if available
try:
from telemetry import TelemetryCapture
self.telemetry = TelemetryCapture(self.db_dir / "telemetry")
except Exception as e:
# Telemetry is non-critical
if self.error_handler:
self.error_handler.handle_error(e, "Telemetry initialization")
self.telemetry = None
def _find_repo_root(self) -> Path:
"""Find git repository root"""
try:
import subprocess
result = subprocess.run(
["git", "rev-parse", "--show-toplevel"],
capture_output=True, text=True, check=True
)
return Path(result.stdout.strip())
except:
LOGGER.debug("Falling back to current working directory for repository root")
return Path.cwd()
def _resolve_agent_id(self, override=None) -> str:
"""
Multi-Layer Agent ID Resolution System
Priority: 1) Override 2) Environment 3) Config File 4) Interactive Setup
@implements FR-058: Multi-Layer Agent ID Resolution System
"""
# Layer 1: Command-line or direct override (highest priority)
if override and override.strip():
self._save_agent_id(override.strip())
return override.strip()
# Layer 2: Environment variable (backward compatibility)
env_agent_id = os.environ.get('TM_AGENT_ID', '').strip()
if env_agent_id:
# Save to config for future persistence
self._save_agent_id(env_agent_id)
return env_agent_id
# Layer 3: Configuration file (persistent across sessions)
config_agent_id = self._load_agent_id()
if config_agent_id:
return config_agent_id
# Layer 4: Interactive setup or fallback
return self._setup_agent_id()
def _load_agent_id(self) -> Optional[str]:
"""Load agent ID from configuration file"""
try:
if self.agent_id_file.exists():
agent_id = self.agent_id_file.read_text().strip()
if agent_id:
return agent_id
except Exception as e:
LOGGER.warning(f"Could not read agent ID from config: {e}")
return None
def _save_agent_id(self, agent_id: str) -> None:
"""Save agent ID to configuration file for persistence"""
try:
# Ensure config directory exists
self.config_dir.mkdir(parents=True, exist_ok=True)
# Save agent ID
self.agent_id_file.write_text(agent_id.strip() + '\n')
except Exception as e:
# Non-critical - continue with session agent ID
print(f"Warning: Could not save agent ID to config: {e}")
def _setup_agent_id(self) -> str:
"""Setup agent ID through interactive prompt or generate fallback"""
# Check if we're in an automated environment (non-interactive)
if not sys.stdin.isatty() or os.environ.get('CI') or os.environ.get('GITHUB_ACTIONS'):
# Non-interactive environment - use generated ID
generated_id = self._generate_agent_id()
self._save_agent_id(generated_id)
return generated_id
# Interactive environment - prompt user
print("\n🤖 Task Orchestrator Agent ID Setup")
print("An agent ID is required for multi-agent coordination.")
print("\nSuggested agent IDs based on your environment:")
suggestions = [
"orchestrator",
"main-agent",
f"{os.environ.get('USER', 'user')}-agent",
self._generate_agent_id()
]
for i, suggestion in enumerate(suggestions, 1):
print(f" {i}) {suggestion}")
while True:
response = input("\nEnter agent ID (or number to select): ").strip()
if not response:
continue
# Check if it's a number selection
try:
selection = int(response)
if 1 <= selection <= len(suggestions):
agent_id = suggestions[selection - 1]
break
except ValueError:
# Direct input
if self._validate_agent_id(response):
agent_id = response
break
else:
print("Invalid agent ID. Use letters, numbers, hyphens, and underscores only.")
self._save_agent_id(agent_id)
print(f"\n✅ Agent ID '{agent_id}' saved to configuration.")
print(f"Configuration saved to: {self.agent_id_file}")
return agent_id
def _validate_agent_id(self, agent_id: str) -> bool:
"""Validate agent ID format"""
if not agent_id or len(agent_id) < 2 or len(agent_id) > 50:
return False
# Allow letters, numbers, hyphens, and underscores
import re
return bool(re.match(r'^[a-zA-Z0-9_-]+$', agent_id))
def _generate_agent_id(self) -> str:
"""Generate unique agent ID with better defaults"""
# Try to get a meaningful default
user = os.environ.get('USER', os.environ.get('USERNAME', 'user'))
unique = f"{user}_{os.getpid()}"
# Return a more readable agent ID
return f"{user}_{hashlib.md5(unique.encode()).hexdigest()[:4]}"
def _init_db(self):
"""Initialize database if needed with WSL safety"""
import platform
import time
# Detect WSL environment
is_wsl = 'microsoft' in platform.uname().release.lower()
self.db_dir.mkdir(parents=True, exist_ok=True)
# Initialize subdirectories
(self.db_dir / "contexts").mkdir(exist_ok=True)
(self.db_dir / "notes").mkdir(exist_ok=True)
(self.db_dir / "archives").mkdir(exist_ok=True)
# WSL-safe database connection with retries
max_retries = 3 if is_wsl else 1
for attempt in range(max_retries):
try:
# Add small delay for WSL
if is_wsl and attempt > 0:
time.sleep(0.5)
# Use WAL mode for better concurrency in WSL
with sqlite3.connect(str(self.db_path), timeout=10.0) as conn:
if is_wsl or os.environ.get('TM_WSL_MODE'):
# Enable WAL mode for better WSL performance
conn.execute("PRAGMA journal_mode=WAL")
conn.execute("PRAGMA synchronous=NORMAL")
conn.execute("PRAGMA temp_store=MEMORY")
conn.execute("PRAGMA mmap_size=30000000000")
conn.execute("""
CREATE TABLE IF NOT EXISTS tasks (
id TEXT PRIMARY KEY,
title TEXT NOT NULL,
description TEXT,
status TEXT DEFAULT 'pending',
priority TEXT DEFAULT 'medium',
assignee TEXT,
created_by TEXT NOT NULL DEFAULT 'user',
created_at TEXT NOT NULL,
updated_at TEXT NOT NULL,
completed_at TEXT,
success_criteria TEXT,
feedback_quality INTEGER,
feedback_timeliness INTEGER,
feedback_notes TEXT,
completion_summary TEXT,
deadline TEXT,
estimated_hours REAL,
actual_hours REAL,
context TEXT
)
""")
conn.execute("""
CREATE TABLE IF NOT EXISTS dependencies (
task_id TEXT NOT NULL,
depends_on TEXT NOT NULL,
PRIMARY KEY (task_id, depends_on)
)
""")
conn.execute("""
CREATE TABLE IF NOT EXISTS notifications (
id INTEGER PRIMARY KEY AUTOINCREMENT,
agent_id TEXT,
task_id TEXT,
type TEXT NOT NULL,
message TEXT NOT NULL,
created_at TEXT NOT NULL,
read BOOLEAN DEFAULT 0
)
""")
conn.execute("""
CREATE TABLE IF NOT EXISTS participants (
task_id TEXT NOT NULL,
agent_id TEXT NOT NULL,
joined_at TEXT NOT NULL,
PRIMARY KEY (task_id, agent_id)
)
""")
conn.execute("""
CREATE TABLE IF NOT EXISTS collaboration_events (
seq INTEGER PRIMARY KEY AUTOINCREMENT,
task_id TEXT NOT NULL,
agent_id TEXT NOT NULL,
event_type TEXT NOT NULL,
content TEXT NOT NULL,
request_id TEXT,
created_at TEXT NOT NULL,
UNIQUE(task_id, request_id)
)
""")
conn.execute("""
CREATE INDEX IF NOT EXISTS idx_collab_events_task_seq
ON collaboration_events(task_id, seq)
""")
# Create phase management tables
conn.execute("""
CREATE TABLE IF NOT EXISTS phases (
id TEXT PRIMARY KEY,
name TEXT NOT NULL,
description TEXT,
status TEXT DEFAULT 'planning' CHECK(status IN ('planning', 'active', 'blocked', 'completed', 'archived')),
parent_phase_id TEXT,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
started_at TIMESTAMP,
completed_at TIMESTAMP,
FOREIGN KEY (parent_phase_id) REFERENCES phases(id)
)
""")
conn.execute("""
CREATE TABLE IF NOT EXISTS phase_dependencies (
id TEXT PRIMARY KEY,
phase_id TEXT NOT NULL,
depends_on_phase_id TEXT NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (phase_id) REFERENCES phases(id),
FOREIGN KEY (depends_on_phase_id) REFERENCES phases(id),
UNIQUE(phase_id, depends_on_phase_id)
)
""")
conn.execute("""
CREATE TABLE IF NOT EXISTS phase_gates (
id TEXT PRIMARY KEY,
phase_id TEXT NOT NULL,
gate_type TEXT NOT NULL CHECK(gate_type IN ('task_completion', 'approval', 'custom')),
criteria TEXT NOT NULL,
status TEXT DEFAULT 'pending' CHECK(status IN ('pending', 'passed', 'failed', 'overridden')),
override_reason TEXT,
override_by TEXT,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
evaluated_at TIMESTAMP,
FOREIGN KEY (phase_id) REFERENCES phases(id)
)
""")
# Migration: Add created_by column if it doesn't exist
cursor = conn.execute("PRAGMA table_info(tasks)")
columns = {row[1] for row in cursor.fetchall()}
# Migration: Add phase_id column if it doesn't exist
if 'phase_id' not in columns:
try:
conn.execute("""
ALTER TABLE tasks ADD COLUMN phase_id TEXT REFERENCES phases(id)
""")
except sqlite3.OperationalError:
LOGGER.debug("tasks.phase_id already exists; skipping migration step")
# Migration: Add context column for Commander's Intent framework
if 'context' not in columns:
try:
conn.execute("""
ALTER TABLE tasks ADD COLUMN context TEXT
""")
except sqlite3.OperationalError:
LOGGER.debug("tasks.context already exists; skipping migration step")
if 'created_by' not in columns:
# Add the created_by column to existing tables
try:
conn.execute("""
ALTER TABLE tasks ADD COLUMN created_by TEXT DEFAULT 'user'
""")
# Update existing rows with a default created_by value
conn.execute("""
UPDATE tasks SET created_by = 'user' WHERE created_by IS NULL
""")
except sqlite3.OperationalError:
# Column might already exist, ignore the error
pass
conn.commit()
break # Success, exit retry loop
except sqlite3.OperationalError as e:
if attempt == max_retries - 1:
raise # Re-raise on last attempt
print(f"Database initialization attempt {attempt + 1} failed, retrying...")
continue
def _validate_commanders_intent(self, context: str, assignee: str) -> dict:
"""
Validate Commander's Intent framework compliance in task context
@implements FR-CORE-1: Task Creation System with Intent Framework
@implements COLLAB-001: Multi-Agent Context Sharing with WHY/WHAT/DONE
Returns dict with 'valid' boolean and 'message' string
"""
if not context or not context.strip():
return {'valid': False, 'message': 'Context is empty'}
context_upper = context.upper()
# Check for WHY/WHAT/DONE pattern
has_why = 'WHY:' in context_upper
has_what = 'WHAT:' in context_upper
has_done = 'DONE:' in context_upper
missing_components = []
if not has_why:
missing_components.append('WHY')
if not has_what:
missing_components.append('WHAT')
if not has_done:
missing_components.append('DONE')
if missing_components:
missing_str = ', '.join(missing_components)
return {
'valid': False,
'message': f'Missing Commander\'s Intent components: {missing_str}'
}
# Additional quality checks
quality_issues = []
# Check if WHY section has content (not just "WHY:")
why_start = context_upper.find('WHY:')
if why_start != -1:
why_section = context[why_start + 4:].split('WHAT:')[0].strip()
if len(why_section) < 10:
quality_issues.append('WHY section too brief (needs clear reasoning)')
# Check if WHAT section has multiple deliverables
what_start = context_upper.find('WHAT:')
if what_start != -1:
what_end = context_upper.find('DONE:', what_start)
if what_end != -1:
what_section = context[what_start + 5:what_end].strip()
# Look for multiple items (commas, line breaks, or bullet points)
if not any(char in what_section for char in [',', '\n', '-', '•', '1.', '2.']):
quality_issues.append('WHAT section should list 3-5 specific deliverables')
# Check if DONE section has measurable criteria
done_start = context_upper.find('DONE:')
if done_start != -1:
done_section = context[done_start + 5:].strip()
if len(done_section) < 15:
quality_issues.append('DONE section needs clear success criteria')
if quality_issues:
return {
'valid': True, # Has structure but could be better
'message': f'Quality suggestions: {"; ".join(quality_issues)}'
}
return {'valid': True, 'message': 'Excellent Commander\'s Intent structure!'}
# ========== SIMPLE PUBLIC INTERFACE ==========
def add(self, title: str, description: str = None,
priority: str = "medium", depends_on: List[str] = None,
success_criteria: str = None, deadline: str = None,
estimated_hours: float = None, assignee: str = None,
phase_id: str = None, context: str = None) -> str:
"""
Add a new task with optional Core Loop fields and Commander's Intent validation
@implements FR-CORE-1: Task Creation System
@implements FR-001: Task Title and Description
@implements FR-002: Task Priority Management
@implements FR-003: Task Dependency System
@implements FR-004: Success Criteria Definition
@implements FR-005: Deadline Management
@implements FR-006: Time Estimation
@implements FR-016: Agent Task Assignment
@implements COLLAB-003: Task Assignment System
@implements COMMANDER-INTENT: WHY/WHAT/DONE Framework
"""
# Validate title
if not title or not title.strip():
if self.error_handler:
from error_handler import ValidationError
raise ValidationError("Task title cannot be empty")
else:
raise ValueError("Task title cannot be empty")
# Validate Commander's Intent when assigning to agents
if assignee:
if context and context.strip():
# Context provided - validate Commander's Intent structure
intent_validation = self._validate_commanders_intent(context, assignee)
if not intent_validation['valid']:
print(f"⚠️ Commander's Intent Warning: {intent_validation['message']}")
print("📋 For best results, include: WHY: [reason] WHAT: [deliverables] DONE: [criteria]")
elif 'Quality suggestions' in intent_validation['message']:
print(f"💡 Commander's Intent: {intent_validation['message']}")
else:
# No context or empty context - provide recommendation
print("🎯 Commander's Intent Recommendation:")
print(" For optimal agent coordination, consider adding --context with:")
print(" WHY: [reason] WHAT: [3-5 deliverables] DONE: [success criteria]")
# Validate success criteria if provided
if success_criteria:
try:
import json
parsed = json.loads(success_criteria)
# Additional validation
if not isinstance(parsed, list):
raise ValueError("Success criteria must be a JSON array")
for criterion in parsed:
if not isinstance(criterion, dict):
raise ValueError("Each criterion must be a JSON object")
if 'criterion' not in criterion:
raise ValueError("Each criterion must have a 'criterion' field")
except json.JSONDecodeError as e:
if self.error_handler:
from error_handler import ValidationError
raise ValidationError(f"Invalid JSON in success criteria: {e}")
else:
raise ValueError("Success criteria must be valid JSON")
except ValueError as e:
if self.error_handler:
from error_handler import ValidationError
raise ValidationError(str(e))
else:
raise
task_id = uuid.uuid4().hex[:8]
now = datetime.now().isoformat()
try:
with sqlite3.connect(str(self.db_path), timeout=10.0) as conn:
# Check if dependencies exist
if depends_on:
placeholders = ','.join('?' for _ in depends_on)
cursor = conn.execute(f"""
SELECT id FROM tasks WHERE id IN ({placeholders})
""", depends_on)
existing_deps = {row[0] for row in cursor.fetchall()}
missing_deps = set(depends_on) - existing_deps
if missing_deps:
raise ValueError(f"Dependencies not found: {', '.join(missing_deps)}")
# Determine status based on dependencies
status = "blocked" if depends_on else "pending"
# Check if table has Core Loop columns (for backward compatibility)
cursor = conn.execute("PRAGMA table_info(tasks)")
columns = {row[1] for row in cursor.fetchall()}
# Use agent_id for created_by field
created_by = self.agent_id
# Store context for Commander's Intent framework
if context and 'context' not in columns:
# Add context column if it doesn't exist
conn.execute("ALTER TABLE tasks ADD COLUMN context TEXT")
columns.add('context')
if 'created_by' in columns and 'success_criteria' in columns:
# New schema with created_by and Core Loop fields
if 'phase_id' in columns:
# Schema with phase support
if 'context' in columns:
conn.execute("""
INSERT INTO tasks (id, title, description, status, priority, assignee, created_by, created_at, updated_at,
success_criteria, deadline, estimated_hours, phase_id, context)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
""", (task_id, title, description, status, priority, assignee, created_by, now, now,
success_criteria, deadline, estimated_hours, phase_id, context))
else:
conn.execute("""
INSERT INTO tasks (id, title, description, status, priority, assignee, created_by, created_at, updated_at,
success_criteria, deadline, estimated_hours, phase_id)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
""", (task_id, title, description, status, priority, assignee, created_by, now, now,
success_criteria, deadline, estimated_hours, phase_id))
else:
if 'context' in columns:
conn.execute("""
INSERT INTO tasks (id, title, description, status, priority, assignee, created_by, created_at, updated_at,
success_criteria, deadline, estimated_hours, context)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
""", (task_id, title, description, status, priority, assignee, created_by, now, now,
success_criteria, deadline, estimated_hours, context))
else:
conn.execute("""
INSERT INTO tasks (id, title, description, status, priority, assignee, created_by, created_at, updated_at,
success_criteria, deadline, estimated_hours)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
""", (task_id, title, description, status, priority, assignee, created_by, now, now,
success_criteria, deadline, estimated_hours))
elif 'created_by' in columns:
# Schema with created_by but without Core Loop fields
if 'context' in columns:
conn.execute("""
INSERT INTO tasks (id, title, description, status, priority, assignee, created_by, created_at, updated_at, context)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
""", (task_id, title, description, status, priority, assignee, created_by, now, now, context))
else:
conn.execute("""
INSERT INTO tasks (id, title, description, status, priority, assignee, created_by, created_at, updated_at)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)
""", (task_id, title, description, status, priority, assignee, created_by, now, now))
elif 'success_criteria' in columns:
# Legacy schema with Core Loop fields but no created_by
if 'context' in columns:
conn.execute("""
INSERT INTO tasks (id, title, description, status, priority, assignee, created_at, updated_at,
success_criteria, deadline, estimated_hours, context)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
""", (task_id, title, description, status, priority, assignee, now, now,
success_criteria, deadline, estimated_hours, context))
else:
conn.execute("""
INSERT INTO tasks (id, title, description, status, priority, assignee, created_at, updated_at,
success_criteria, deadline, estimated_hours)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
""", (task_id, title, description, status, priority, assignee, now, now,
success_criteria, deadline, estimated_hours))
else:
# Legacy schema without created_by or Core Loop fields
if 'context' in columns:
conn.execute("""
INSERT INTO tasks (id, title, description, status, priority, assignee, created_at, updated_at, context)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)
""", (task_id, title, description, status, priority, assignee, now, now, context))
else:
conn.execute("""
INSERT INTO tasks (id, title, description, status, priority, assignee, created_at, updated_at)
VALUES (?, ?, ?, ?, ?, ?, ?, ?)
""", (task_id, title, description, status, priority, assignee, now, now))
# Add dependencies
if depends_on:
for dep_id in depends_on:
conn.execute("""
INSERT INTO dependencies (task_id, depends_on)
VALUES (?, ?)
""", (task_id, dep_id))
conn.commit()
except sqlite3.IntegrityError as e:
raise ValueError(f"Database integrity error: {e}")
except Exception as e:
raise ValueError(f"Failed to add task: {e}")
# Capture telemetry
if self.telemetry:
self.telemetry.capture_task_created(
task_id,
has_criteria=bool(success_criteria),
has_deadline=bool(deadline),
has_estimate=bool(estimated_hours)
)
# Auto-initialize context for collaboration
self._init_context(task_id, title)
return task_id
def show(self, task_id: str) -> Optional[Dict]:
"""Show details of a specific task"""
try:
with sqlite3.connect(str(self.db_path)) as conn:
conn.row_factory = sqlite3.Row
cursor = conn.execute("""
SELECT t.*,
GROUP_CONCAT(d.depends_on) as dependencies
FROM tasks t
LEFT JOIN dependencies d ON t.id = d.task_id
WHERE t.id = ?
GROUP BY t.id
""", (task_id,))
row = cursor.fetchone()
if row:
task = dict(row)
# Convert dependencies string to list
if task.get('dependencies'):
task['dependencies'] = task['dependencies'].split(',')
else:
task['dependencies'] = []
return task
return None
except Exception as e:
print(f"Error showing task: {e}")
return None
def update(self, task_id: str, status: str = None, assignee: str = None) -> bool:
"""
Update task status or assignment
@implements FR-016: Agent Task Assignment
"""
try:
# Validate inputs
if not task_id or not task_id.strip():
raise ValueError("Task ID cannot be empty")
if status and status not in ['pending', 'in_progress', 'completed', 'blocked']:
raise ValueError(f"Invalid status: {status}")
now = datetime.now().isoformat()
updates = ["updated_at = ?"]
params = [now]
if status:
updates.append("status = ?")
params.append(status)
if assignee:
updates.append("assignee = ?")
params.append(assignee)
params.append(task_id)
with sqlite3.connect(str(self.db_path), timeout=10.0) as conn:
cursor = conn.execute(f"""
UPDATE tasks
SET {', '.join(updates)}
WHERE id = ?
""", params)
if cursor.rowcount == 0:
print(f"Warning: Task {task_id} not found")
return False
conn.commit()
return True
except sqlite3.Error as e:
print(f"Database error: {e}")
return False
except ValueError as e:
print(f"Validation error: {e}")
return False
except Exception as e:
print(f"Unexpected error: {e}")
return False
def delete(self, task_id: str) -> bool:
"""Delete a task and clean up dependencies"""
try:
with sqlite3.connect(str(self.db_path)) as conn:
# Check if task exists
cursor = conn.execute("SELECT id FROM tasks WHERE id = ?", (task_id,))
if not cursor.fetchone():
return False
# Remove task from dependencies
conn.execute("DELETE FROM dependencies WHERE task_id = ? OR depends_on = ?",
(task_id, task_id))
# Delete the task
conn.execute("DELETE FROM tasks WHERE id = ?", (task_id,))
# Cleanup associated files
context_file = self.db_dir / "contexts" / f"{task_id}.json"
if context_file.exists():
context_file.unlink()
note_file = self.db_dir / "notes" / f"{task_id}.txt"
if note_file.exists():
note_file.unlink()
conn.commit()
return True
except Exception as e:
print(f"Error deleting task: {e}")
return False
def watch(self, limit: int = 10) -> List[Dict]:
"""Check for recent notifications and important events"""
try:
with sqlite3.connect(str(self.db_path), timeout=10.0) as conn:
conn.row_factory = sqlite3.Row
# Get recent unread notifications for this agent
cursor = conn.execute("""
SELECT * FROM notifications
WHERE (agent_id = ? OR agent_id IS NULL)
AND read = 0
ORDER BY created_at DESC
LIMIT ?
""", (self.agent_id, limit))
notifications = []
for row in cursor:
notifications.append(dict(row))
# Mark notifications as read
conn.execute("""
UPDATE notifications
SET read = 1
WHERE (agent_id = ? OR agent_id IS NULL)
AND read = 0
""", (self.agent_id,))
conn.commit()
return notifications
except Exception as e:
print(f"Error checking notifications: {e}")
return []
def _create_notification(self, conn, task_id: str, type: str, message: str,
agent_id: str = None):
"""Create a notification (internal helper)"""
now = datetime.now().isoformat()
conn.execute("""
INSERT INTO notifications (agent_id, task_id, type, message, created_at)
VALUES (?, ?, ?, ?, ?)
""", (agent_id, task_id, type, message, now))
def complete(self, task_id: str, completion_summary: str = None,
actual_hours: float = None, validate: bool = False) -> bool:
"""
Complete a task with optional Core Loop fields
@implements FR-CORE-3: Task Completion System
@implements FR-010: Task Completion Status
@implements FR-011: Completion Summary Capture
@implements FR-012: Actual Time Tracking
@implements FR-013: Completion Validation
@implements FR-014: Dependency Resolution
"""
now = datetime.now().isoformat()
with sqlite3.connect(str(self.db_path)) as conn:
# Check if validation is requested
if validate:
cursor = conn.execute("""
SELECT success_criteria FROM tasks WHERE id = ?
""", (task_id,))
row = cursor.fetchone()
if row and row[0]:
# Validate success criteria
try:
from criteria_validator import CriteriaValidator
validator = CriteriaValidator()
# Get task context for validation
context = self.context(task_id)
# Validate criteria
all_passed, results = validator.validate_criteria(row[0], context)
# Print validation report
print(validator.format_validation_report(results))
# Capture telemetry for validation
if self.telemetry:
passed_count = sum(1 for r in results if r['passed'])
self.telemetry.capture_criteria_validation(
task_id, passed_count, len(results)
)
if not all_passed:
print("Warning: Not all success criteria met")
except Exception as e:
print(f"Error validating criteria: {e}")
# Check if table has Core Loop columns
cursor = conn.execute("PRAGMA table_info(tasks)")
columns = {row[1] for row in cursor.fetchall()}
if 'completion_summary' in columns:
# Update with Core Loop fields
conn.execute("""
UPDATE tasks
SET status = 'completed', completed_at = ?,
completion_summary = ?, actual_hours = ?
WHERE id = ?
""", (now, completion_summary, actual_hours, task_id))
else:
# Legacy update
conn.execute("""
UPDATE tasks
SET status = 'completed', completed_at = ?
WHERE id = ?
""", (now, task_id))
# Find tasks that will be unblocked
cursor = conn.execute("""
SELECT task_id, assignee FROM tasks t
JOIN dependencies d ON t.id = d.task_id
WHERE t.status = 'blocked'
AND d.depends_on = ?
AND NOT EXISTS (
SELECT 1 FROM dependencies d2
JOIN tasks t2 ON d2.depends_on = t2.id
WHERE d2.task_id = d.task_id
AND d2.depends_on != ?
AND t2.status != 'completed'
)
""", (task_id, task_id))
unblocked_tasks = cursor.fetchall()
# Unblock dependent tasks
conn.execute("""
UPDATE tasks
SET status = 'pending'
WHERE status = 'blocked'
AND id IN (
SELECT task_id FROM dependencies
WHERE depends_on = ?
AND NOT EXISTS (
SELECT 1 FROM dependencies d2
JOIN tasks t ON d2.depends_on = t.id
WHERE d2.task_id = dependencies.task_id
AND d2.depends_on != ?
AND t.status != 'completed'
)
)
""", (task_id, task_id))
# Create notifications for unblocked tasks
for task_row in unblocked_tasks:
unblocked_id, assignee = task_row
self._create_notification(conn, unblocked_id, 'unblocked',
f'Task {unblocked_id} unblocked - dependencies completed',
assignee)
conn.commit()
# Capture telemetry
if self.telemetry:
self.telemetry.capture_task_completed(
task_id,
validated=validate,
has_summary=bool(completion_summary),
actual_hours=actual_hours
)
# Auto archive and cleanup
if _AUTO_CLEANUP:
self._archive_and_cleanup(task_id)
return True
def export(self, format: str = "json") -> str:
"""Export all tasks in specified format"""
tasks = self.list()
if format == "json":