-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathschema_manager.py
More file actions
1010 lines (913 loc) · 38.6 KB
/
Copy pathschema_manager.py
File metadata and controls
1010 lines (913 loc) · 38.6 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
"""Database schema management for CodeFRAME.
Handles schema creation, migrations, and database initialization.
Extracted from the monolithic Database class for better maintainability.
"""
import sqlite3
import logging
logger = logging.getLogger(__name__)
class SchemaManager:
"""Manages database schema creation and migrations.
Responsible for creating all database tables, indexes, and ensuring
schema consistency across the application.
"""
def __init__(self, conn: sqlite3.Connection):
"""Initialize schema manager with database connection.
Args:
conn: Active sqlite3.Connection
"""
self.conn = conn
def create_schema(self) -> None:
"""Create all database tables and indexes.
Creates the complete v1.0 flattened schema with all migrations applied.
This method is idempotent - safe to call multiple times.
"""
cursor = self.conn.cursor()
# Authentication tables
self._create_auth_tables(cursor)
# Core project tables
self._create_project_tables(cursor)
# Issue and task tables
self._create_issue_task_tables(cursor)
# Agent management tables
self._create_agent_tables(cursor)
# Blocker management tables
self._create_blocker_tables(cursor)
# Quality and testing tables
self._create_quality_tables(cursor)
# Memory and context tables
self._create_memory_context_tables(cursor)
# Checkpoint and git tracking tables
self._create_checkpoint_git_tables(cursor)
# Metrics and audit tables
self._create_metrics_audit_tables(cursor)
# Apply schema migrations for existing databases BEFORE creating indexes
# (indexes may reference columns added by migrations)
self._apply_migrations(cursor)
# Create all indexes (after migrations so all columns exist)
self._create_indexes(cursor)
self.conn.commit()
# Ensure default admin user exists
self._ensure_default_admin_user()
def _apply_migrations(self, cursor: sqlite3.Cursor) -> None:
"""Apply schema migrations for existing databases.
Handles adding new columns to existing tables.
These are idempotent - safe to run multiple times.
"""
# Migration: Add depends_on column to issues table (cf-207)
self._add_column_if_not_exists(
cursor, "issues", "depends_on", "TEXT"
)
# Migration: Add missing columns to tasks table for older databases
# Core columns that may be missing
self._add_column_if_not_exists(
cursor, "tasks", "project_id", "INTEGER"
)
self._add_column_if_not_exists(
cursor, "tasks", "issue_id", "INTEGER"
)
self._add_column_if_not_exists(
cursor, "tasks", "parent_issue_number", "TEXT"
)
self._add_column_if_not_exists(
cursor, "tasks", "task_number", "TEXT"
)
self._add_column_if_not_exists(
cursor, "tasks", "description", "TEXT"
)
self._add_column_if_not_exists(
cursor, "tasks", "status", "TEXT", "'pending'"
)
self._add_column_if_not_exists(
cursor, "tasks", "priority", "INTEGER", "0"
)
self._add_column_if_not_exists(
cursor, "tasks", "workflow_step", "INTEGER"
)
self._add_column_if_not_exists(
cursor, "tasks", "depends_on", "TEXT"
)
self._add_column_if_not_exists(
cursor, "tasks", "created_at", "TIMESTAMP", "CURRENT_TIMESTAMP"
)
self._add_column_if_not_exists(
cursor, "tasks", "completed_at", "TIMESTAMP"
)
self._add_column_if_not_exists(
cursor, "tasks", "assigned_to", "TEXT"
)
self._add_column_if_not_exists(
cursor, "tasks", "can_parallelize", "BOOLEAN", "FALSE"
)
self._add_column_if_not_exists(
cursor, "tasks", "requires_mcp", "BOOLEAN", "FALSE"
)
self._add_column_if_not_exists(
cursor, "tasks", "estimated_tokens", "INTEGER"
)
self._add_column_if_not_exists(
cursor, "tasks", "actual_tokens", "INTEGER"
)
self._add_column_if_not_exists(
cursor, "tasks", "commit_sha", "TEXT"
)
self._add_column_if_not_exists(
cursor, "tasks", "quality_gate_status", "TEXT", "'pending'"
)
self._add_column_if_not_exists(
cursor, "tasks", "quality_gate_failures", "JSON"
)
self._add_column_if_not_exists(
cursor, "tasks", "requires_human_approval", "BOOLEAN", "FALSE"
)
# Migration: Add effort estimation columns to tasks table (Phase 1)
self._add_column_if_not_exists(
cursor, "tasks", "estimated_hours", "REAL"
)
self._add_column_if_not_exists(
cursor, "tasks", "complexity_score", "INTEGER"
)
self._add_column_if_not_exists(
cursor, "tasks", "uncertainty_level", "TEXT"
)
self._add_column_if_not_exists(
cursor, "tasks", "resource_requirements", "TEXT"
)
# Migration: Add supervisor intervention context to tasks table
self._add_column_if_not_exists(
cursor, "tasks", "intervention_context", "JSON"
)
def _add_column_if_not_exists(
self,
cursor: sqlite3.Cursor,
table_name: str,
column_name: str,
column_type: str,
default_value: str = None,
) -> None:
"""Add a column to a table if it doesn't exist.
Args:
cursor: SQLite cursor
table_name: Table to modify
column_name: Column to add
column_type: SQLite column type
default_value: Optional default value for the column
Raises:
ValueError: If table_name, column_name, or column_type contain invalid characters
"""
# SECURITY: Validate identifiers to prevent SQL injection.
# Only alphanumeric + underscore allowed (standard SQL identifier rules).
import re
identifier_pattern = re.compile(r'^[a-zA-Z_][a-zA-Z0-9_]*$')
for name, value in [("table_name", table_name), ("column_name", column_name), ("column_type", column_type)]:
if not identifier_pattern.match(value):
raise ValueError(f"Invalid SQL identifier for {name}: {value}")
# SECURITY: Validate default_value to only allow safe SQL literals.
# Allowed: NULL, TRUE, FALSE, CURRENT_TIMESTAMP, integers, floats,
# or single-quoted strings (with no embedded quotes).
if default_value is not None:
safe_literal_pattern = re.compile(
r"^(NULL|TRUE|FALSE|CURRENT_TIMESTAMP|" # SQL keywords
r"-?\d+|" # Integers (including negative)
r"-?\d+\.\d+|" # Floats
r"'[^']*')$", # Single-quoted strings (no embedded quotes)
re.IGNORECASE
)
if not safe_literal_pattern.match(default_value):
raise ValueError(
f"Invalid SQL literal for default_value: {default_value}. "
"Only NULL, TRUE, FALSE, CURRENT_TIMESTAMP, numbers, or "
"single-quoted strings (without embedded quotes) are allowed."
)
# Check if column exists
cursor.execute(f"PRAGMA table_info({table_name})")
columns = {row[1] for row in cursor.fetchall()}
if column_name not in columns:
# Add the column
if default_value is not None:
cursor.execute(
f"ALTER TABLE {table_name} ADD COLUMN {column_name} {column_type} DEFAULT {default_value}"
)
else:
cursor.execute(
f"ALTER TABLE {table_name} ADD COLUMN {column_name} {column_type}"
)
logger.info(f"Added column {column_name} to {table_name}")
def _create_auth_tables(self, cursor: sqlite3.Cursor) -> None:
"""Create authentication tables (fastapi-users compatible)."""
cursor.execute(
"""
CREATE TABLE IF NOT EXISTS users (
id INTEGER PRIMARY KEY AUTOINCREMENT,
email TEXT UNIQUE NOT NULL,
name TEXT,
hashed_password TEXT NOT NULL,
is_active INTEGER DEFAULT 1,
is_superuser INTEGER DEFAULT 0,
is_verified INTEGER DEFAULT 0,
email_verified INTEGER DEFAULT 0,
image TEXT,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
)
"""
)
# Accounts table (BetterAuth compatible - stores passwords and OAuth)
cursor.execute(
"""
CREATE TABLE IF NOT EXISTS accounts (
id TEXT PRIMARY KEY,
user_id INTEGER NOT NULL REFERENCES users(id) ON DELETE CASCADE,
account_id TEXT NOT NULL,
provider_id TEXT NOT NULL,
password TEXT,
access_token TEXT,
refresh_token TEXT,
id_token TEXT,
access_token_expires_at TIMESTAMP,
refresh_token_expires_at TIMESTAMP,
scope TEXT,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
UNIQUE(user_id, provider_id)
)
"""
)
# Create index on user_id for faster login lookups
cursor.execute(
"""
CREATE INDEX IF NOT EXISTS idx_accounts_user_id ON accounts(user_id)
"""
)
# Sessions table (BetterAuth compatible)
cursor.execute(
"""
CREATE TABLE IF NOT EXISTS sessions (
id TEXT PRIMARY KEY,
token TEXT UNIQUE NOT NULL,
user_id INTEGER NOT NULL REFERENCES users(id) ON DELETE CASCADE,
expires_at TIMESTAMP NOT NULL,
ip_address TEXT,
user_agent TEXT,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
)
"""
)
# Verification table (BetterAuth compatible)
# Used for email verification tokens when requireEmailVerification is enabled
cursor.execute(
"""
CREATE TABLE IF NOT EXISTS verification (
id TEXT PRIMARY KEY,
identifier TEXT NOT NULL,
value TEXT NOT NULL,
expires_at TIMESTAMP NOT NULL
)
"""
)
# API Keys table for programmatic access
cursor.execute(
"""
CREATE TABLE IF NOT EXISTS api_keys (
id TEXT PRIMARY KEY,
user_id INTEGER NOT NULL REFERENCES users(id) ON DELETE CASCADE,
name TEXT NOT NULL,
key_hash TEXT NOT NULL,
prefix TEXT NOT NULL,
scopes TEXT NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
last_used_at TIMESTAMP,
expires_at TIMESTAMP,
is_active INTEGER DEFAULT 1
)
"""
)
# Indexes for api_keys table
cursor.execute(
"""
CREATE INDEX IF NOT EXISTS idx_api_keys_prefix ON api_keys(prefix)
"""
)
cursor.execute(
"""
CREATE INDEX IF NOT EXISTS idx_api_keys_user_id ON api_keys(user_id)
"""
)
def _create_project_tables(self, cursor: sqlite3.Cursor) -> None:
"""Create project and project_users tables."""
# Projects table
cursor.execute(
"""
CREATE TABLE IF NOT EXISTS projects (
id INTEGER PRIMARY KEY,
name TEXT NOT NULL,
description TEXT NOT NULL,
user_id INTEGER REFERENCES users(id) ON DELETE SET NULL,
source_type TEXT CHECK(source_type IN ('git_remote', 'local_path', 'upload', 'empty')) DEFAULT 'empty',
source_location TEXT,
source_branch TEXT DEFAULT 'main',
workspace_path TEXT NOT NULL,
git_initialized BOOLEAN DEFAULT FALSE,
current_commit TEXT,
status TEXT CHECK(status IN ('init', 'planning', 'running', 'active', 'paused', 'completed')),
phase TEXT CHECK(phase IN ('discovery', 'planning', 'active', 'review', 'complete')) DEFAULT 'discovery',
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
paused_at TIMESTAMP NULL,
config JSON
)
"""
)
# Project users table (authorization)
cursor.execute(
"""
CREATE TABLE IF NOT EXISTS project_users (
project_id INTEGER NOT NULL REFERENCES projects(id) ON DELETE CASCADE,
user_id INTEGER NOT NULL REFERENCES users(id) ON DELETE CASCADE,
role TEXT NOT NULL CHECK(role IN ('owner', 'collaborator', 'viewer')),
granted_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (project_id, user_id)
)
"""
)
def _create_issue_task_tables(self, cursor: sqlite3.Cursor) -> None:
"""Create issues and tasks tables."""
# Issues table
cursor.execute(
"""
CREATE TABLE IF NOT EXISTS issues (
id INTEGER PRIMARY KEY,
project_id INTEGER REFERENCES projects(id),
issue_number TEXT NOT NULL,
title TEXT NOT NULL,
description TEXT,
status TEXT CHECK(status IN ('pending', 'in_progress', 'completed', 'failed')),
priority INTEGER CHECK(priority BETWEEN 0 AND 4),
workflow_step INTEGER,
depends_on TEXT,
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
completed_at TIMESTAMP,
UNIQUE(project_id, issue_number)
)
"""
)
# Tasks table
cursor.execute(
"""
CREATE TABLE IF NOT EXISTS tasks (
id INTEGER PRIMARY KEY,
project_id INTEGER REFERENCES projects(id),
issue_id INTEGER REFERENCES issues(id),
task_number TEXT,
parent_issue_number TEXT,
title TEXT NOT NULL,
description TEXT,
status TEXT CHECK(status IN ('pending', 'assigned', 'in_progress', 'blocked', 'completed', 'failed')),
assigned_to TEXT,
depends_on TEXT,
can_parallelize BOOLEAN DEFAULT FALSE,
priority INTEGER CHECK(priority BETWEEN 0 AND 4),
workflow_step INTEGER,
requires_mcp BOOLEAN DEFAULT FALSE,
estimated_tokens INTEGER,
actual_tokens INTEGER,
commit_sha TEXT,
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
completed_at TIMESTAMP,
quality_gate_status TEXT CHECK(quality_gate_status IN ('pending', 'running', 'passed', 'failed')) DEFAULT 'pending',
quality_gate_failures JSON,
requires_human_approval BOOLEAN DEFAULT FALSE,
-- Effort estimation fields (Phase 1)
estimated_hours REAL,
complexity_score INTEGER CHECK(complexity_score BETWEEN 1 AND 5),
uncertainty_level TEXT CHECK(uncertainty_level IN ('low', 'medium', 'high')),
resource_requirements TEXT,
-- Supervisor intervention context (JSON for flexible structure)
intervention_context JSON
)
"""
)
# Task dependencies junction table
cursor.execute(
"""
CREATE TABLE IF NOT EXISTS task_dependencies (
id INTEGER PRIMARY KEY,
task_id INTEGER NOT NULL,
depends_on_task_id INTEGER NOT NULL,
FOREIGN KEY (task_id) REFERENCES tasks(id),
FOREIGN KEY (depends_on_task_id) REFERENCES tasks(id),
UNIQUE(task_id, depends_on_task_id)
)
"""
)
def _create_agent_tables(self, cursor: sqlite3.Cursor) -> None:
"""Create agent and project_agents tables."""
# Agents table
cursor.execute(
"""
CREATE TABLE IF NOT EXISTS agents (
id TEXT PRIMARY KEY,
type TEXT NOT NULL,
project_id INTEGER REFERENCES projects(id) ON DELETE CASCADE,
provider TEXT,
maturity_level TEXT CHECK(maturity_level IN ('directive', 'coaching', 'supporting', 'delegating')),
status TEXT CHECK(status IN ('idle', 'working', 'blocked', 'offline')),
current_task_id INTEGER REFERENCES tasks(id),
last_heartbeat TIMESTAMP,
metrics JSON,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
)
"""
)
# Project-Agent junction table (many-to-many)
cursor.execute(
"""
CREATE TABLE IF NOT EXISTS project_agents (
id INTEGER PRIMARY KEY AUTOINCREMENT,
project_id INTEGER NOT NULL REFERENCES projects(id) ON DELETE CASCADE,
agent_id TEXT NOT NULL REFERENCES agents(id) ON DELETE CASCADE,
role TEXT NOT NULL,
assigned_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
unassigned_at TIMESTAMP,
is_active BOOLEAN DEFAULT TRUE,
CHECK(unassigned_at IS NULL OR unassigned_at >= assigned_at)
)
"""
)
def _create_blocker_tables(self, cursor: sqlite3.Cursor) -> None:
"""Create blockers table."""
cursor.execute(
"""
CREATE TABLE IF NOT EXISTS blockers (
id INTEGER PRIMARY KEY AUTOINCREMENT,
agent_id TEXT NOT NULL,
project_id INTEGER NOT NULL,
task_id INTEGER,
blocker_type TEXT NOT NULL CHECK(blocker_type IN ('SYNC', 'ASYNC')),
question TEXT NOT NULL,
answer TEXT,
status TEXT NOT NULL DEFAULT 'PENDING' CHECK(status IN ('PENDING', 'RESOLVED', 'EXPIRED')),
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
resolved_at TIMESTAMP,
FOREIGN KEY (project_id) REFERENCES projects(id) ON DELETE CASCADE,
FOREIGN KEY (task_id) REFERENCES tasks(id) ON DELETE CASCADE
)
"""
)
def _create_quality_tables(self, cursor: sqlite3.Cursor) -> None:
"""Create quality, testing, and code review tables."""
# Lint results table
cursor.execute(
"""
CREATE TABLE IF NOT EXISTS lint_results (
id INTEGER PRIMARY KEY AUTOINCREMENT,
task_id INTEGER NOT NULL,
linter TEXT NOT NULL CHECK(linter IN ('ruff', 'eslint', 'other')),
error_count INTEGER NOT NULL DEFAULT 0,
warning_count INTEGER NOT NULL DEFAULT 0,
files_linted INTEGER NOT NULL DEFAULT 0,
output TEXT,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (task_id) REFERENCES tasks(id) ON DELETE CASCADE
)
"""
)
# Test results table
cursor.execute(
"""
CREATE TABLE IF NOT EXISTS test_results (
id INTEGER PRIMARY KEY,
task_id INTEGER NOT NULL REFERENCES tasks(id),
status TEXT NOT NULL CHECK(status IN ('passed', 'failed', 'error', 'timeout', 'no_tests')),
passed INTEGER DEFAULT 0,
failed INTEGER DEFAULT 0,
errors INTEGER DEFAULT 0,
skipped INTEGER DEFAULT 0,
duration REAL DEFAULT 0.0,
output TEXT,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
)
"""
)
# Correction attempts table
cursor.execute(
"""
CREATE TABLE IF NOT EXISTS correction_attempts (
id INTEGER PRIMARY KEY,
task_id INTEGER NOT NULL REFERENCES tasks(id),
attempt_number INTEGER NOT NULL CHECK(attempt_number BETWEEN 1 AND 3),
error_analysis TEXT NOT NULL,
fix_description TEXT NOT NULL,
code_changes TEXT DEFAULT '',
test_result_id INTEGER REFERENCES test_results(id),
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
)
"""
)
# Code reviews table
cursor.execute(
"""
CREATE TABLE IF NOT EXISTS code_reviews (
id INTEGER PRIMARY KEY AUTOINCREMENT,
task_id INTEGER NOT NULL REFERENCES tasks(id) ON DELETE CASCADE,
agent_id TEXT NOT NULL,
project_id INTEGER NOT NULL REFERENCES projects(id) ON DELETE CASCADE,
file_path TEXT NOT NULL,
line_number INTEGER,
severity TEXT NOT NULL CHECK(severity IN ('critical', 'high', 'medium', 'low', 'info')),
category TEXT NOT NULL CHECK(category IN ('security', 'performance', 'quality', 'maintainability', 'style')),
message TEXT NOT NULL,
recommendation TEXT,
code_snippet TEXT,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
)
"""
)
# Task evidence table (for evidence-based quality enforcement)
cursor.execute(
"""
CREATE TABLE IF NOT EXISTS task_evidence (
id INTEGER PRIMARY KEY AUTOINCREMENT,
task_id INTEGER NOT NULL REFERENCES tasks(id) ON DELETE CASCADE,
agent_id TEXT NOT NULL,
language TEXT NOT NULL,
framework TEXT,
-- Test results
total_tests INTEGER NOT NULL,
passed_tests INTEGER NOT NULL,
failed_tests INTEGER NOT NULL,
skipped_tests INTEGER NOT NULL,
pass_rate REAL NOT NULL,
coverage REAL,
test_output TEXT NOT NULL,
-- Skip violations
skip_violations_count INTEGER NOT NULL DEFAULT 0,
skip_violations_json TEXT,
skip_check_passed BOOLEAN NOT NULL,
-- Quality metrics
quality_metrics_json TEXT NOT NULL,
-- Verification status
verified BOOLEAN NOT NULL,
verification_errors TEXT,
-- Metadata
timestamp TEXT NOT NULL,
task_description TEXT NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
)
"""
)
def _create_memory_context_tables(self, cursor: sqlite3.Cursor) -> None:
"""Create memory and context management tables."""
# Memory table
cursor.execute(
"""
CREATE TABLE IF NOT EXISTS memory (
id INTEGER PRIMARY KEY,
project_id INTEGER REFERENCES projects(id),
category TEXT CHECK(category IN ('pattern', 'decision', 'gotcha', 'preference', 'conversation', 'discovery_state', 'discovery_answers', 'prd')),
key TEXT,
value TEXT,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
)
"""
)
# Add unique constraint on memory table to prevent duplicate keys
# First, check if the index already exists
cursor.execute(
"SELECT name FROM sqlite_master WHERE type='index' AND name='idx_memory_unique_key'"
)
index_exists = cursor.fetchone() is not None
if not index_exists:
# Migration: Clean up duplicate entries before creating unique index
# Keep the most recent entry (highest id) for each (project_id, category, key)
cursor.execute(
"""
DELETE FROM memory
WHERE id NOT IN (
SELECT MAX(id)
FROM memory
GROUP BY project_id, category, key
)
"""
)
deleted_count = cursor.rowcount
if deleted_count > 0:
logger.info(
f"Migration: Removed {deleted_count} duplicate memory entries"
)
cursor.execute(
"""
CREATE UNIQUE INDEX IF NOT EXISTS idx_memory_unique_key
ON memory(project_id, category, key)
"""
)
# Context items table
cursor.execute(
"""
CREATE TABLE IF NOT EXISTS context_items (
id TEXT PRIMARY KEY,
project_id INTEGER REFERENCES projects(id),
agent_id TEXT NOT NULL,
item_type TEXT,
content TEXT,
importance_score FLOAT,
importance_reasoning TEXT,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
last_accessed TIMESTAMP,
access_count INTEGER DEFAULT 0,
current_tier TEXT CHECK(current_tier IN ('hot', 'warm', 'cold')),
manual_pin BOOLEAN DEFAULT FALSE
)
"""
)
def _create_checkpoint_git_tables(self, cursor: sqlite3.Cursor) -> None:
"""Create checkpoint, git tracking, and deployment tables."""
# Checkpoints table
cursor.execute(
"""
CREATE TABLE IF NOT EXISTS checkpoints (
id INTEGER PRIMARY KEY,
project_id INTEGER REFERENCES projects(id),
trigger TEXT,
state_snapshot JSON,
git_commit TEXT,
db_backup_path TEXT,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
name TEXT,
description TEXT,
database_backup_path TEXT,
context_snapshot_path TEXT,
metadata JSON
)
"""
)
# Context checkpoints table (for flash save)
cursor.execute(
"""
CREATE TABLE IF NOT EXISTS context_checkpoints (
id INTEGER PRIMARY KEY AUTOINCREMENT,
agent_id TEXT NOT NULL,
checkpoint_data TEXT NOT NULL,
items_count INTEGER NOT NULL,
items_archived INTEGER NOT NULL,
hot_items_retained INTEGER NOT NULL,
token_count INTEGER NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
)
"""
)
# Changelog table
cursor.execute(
"""
CREATE TABLE IF NOT EXISTS changelog (
id INTEGER PRIMARY KEY,
project_id INTEGER REFERENCES projects(id),
agent_id TEXT,
task_id INTEGER,
action TEXT,
details JSON,
timestamp TIMESTAMP DEFAULT CURRENT_TIMESTAMP
)
"""
)
# Git branches table
cursor.execute(
"""
CREATE TABLE IF NOT EXISTS git_branches (
id INTEGER PRIMARY KEY,
issue_id INTEGER REFERENCES issues(id),
branch_name TEXT NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
merged_at TIMESTAMP,
merge_commit TEXT,
status TEXT CHECK(status IN ('active', 'merged', 'abandoned')) DEFAULT 'active'
)
"""
)
# Deployments table
cursor.execute(
"""
CREATE TABLE IF NOT EXISTS deployments (
id INTEGER PRIMARY KEY,
commit_hash TEXT NOT NULL,
environment TEXT CHECK(environment IN ('staging', 'production')),
status TEXT CHECK(status IN ('success', 'failed')),
output TEXT,
duration_seconds REAL,
triggered_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
)
"""
)
# Pull requests table (Sprint 11 - GitHub PR integration)
cursor.execute(
"""
CREATE TABLE IF NOT EXISTS pull_requests (
id INTEGER PRIMARY KEY AUTOINCREMENT,
project_id INTEGER NOT NULL REFERENCES projects(id) ON DELETE CASCADE,
issue_id INTEGER REFERENCES issues(id) ON DELETE SET NULL,
branch_name TEXT NOT NULL,
pr_number INTEGER,
pr_url TEXT,
title TEXT NOT NULL,
body TEXT,
base_branch TEXT DEFAULT 'main',
head_branch TEXT NOT NULL,
status TEXT CHECK(status IN ('draft', 'open', 'merged', 'closed')) DEFAULT 'open',
merge_commit_sha TEXT,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
merged_at TIMESTAMP,
closed_at TIMESTAMP,
github_created_at TIMESTAMP,
github_updated_at TIMESTAMP
)
"""
)
def _create_metrics_audit_tables(self, cursor: sqlite3.Cursor) -> None:
"""Create metrics, token usage, and audit log tables."""
# Token usage table
cursor.execute(
"""
CREATE TABLE IF NOT EXISTS token_usage (
id INTEGER PRIMARY KEY AUTOINCREMENT,
task_id INTEGER REFERENCES tasks(id) ON DELETE SET NULL,
agent_id TEXT NOT NULL,
project_id INTEGER NOT NULL REFERENCES projects(id) ON DELETE CASCADE,
model_name TEXT NOT NULL,
input_tokens INTEGER NOT NULL CHECK(input_tokens >= 0),
output_tokens INTEGER NOT NULL CHECK(output_tokens >= 0),
estimated_cost_usd REAL NOT NULL CHECK(estimated_cost_usd >= 0),
actual_cost_usd REAL CHECK(actual_cost_usd >= 0),
call_type TEXT CHECK(call_type IN ('task_execution', 'code_review', 'coordination', 'other')),
timestamp TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
session_id TEXT DEFAULT NULL
)
"""
)
# Audit logs table
cursor.execute(
"""
CREATE TABLE IF NOT EXISTS audit_logs (
id INTEGER PRIMARY KEY AUTOINCREMENT,
event_type TEXT NOT NULL,
user_id INTEGER REFERENCES users(id) ON DELETE SET NULL,
resource_type TEXT NOT NULL,
resource_id INTEGER,
ip_address TEXT,
metadata TEXT,
timestamp TIMESTAMP DEFAULT CURRENT_TIMESTAMP
)
"""
)
def _create_indexes(self, cursor: sqlite3.Cursor) -> None:
"""Create all database indexes for performance."""
# Issues indexes
cursor.execute(
"CREATE INDEX IF NOT EXISTS idx_issues_number ON issues(project_id, issue_number)"
)
# Tasks indexes
cursor.execute(
"CREATE INDEX IF NOT EXISTS idx_tasks_issue_number ON tasks(parent_issue_number)"
)
cursor.execute(
"CREATE INDEX IF NOT EXISTS idx_tasks_pending_priority ON tasks(project_id, status, priority, created_at)"
)
# Index for agent maturity queries (get_tasks_by_agent)
cursor.execute(
"CREATE INDEX IF NOT EXISTS idx_tasks_assigned_to ON tasks(assigned_to, project_id, created_at)"
)
# Project-Agent indexes
cursor.execute(
"CREATE INDEX IF NOT EXISTS idx_project_agents_project_active ON project_agents(project_id, is_active) WHERE is_active = TRUE"
)
cursor.execute(
"CREATE INDEX IF NOT EXISTS idx_project_agents_agent_active ON project_agents(agent_id, is_active) WHERE is_active = TRUE"
)
cursor.execute(
"CREATE INDEX IF NOT EXISTS idx_project_agents_assigned_at ON project_agents(assigned_at)"
)
cursor.execute(
"CREATE INDEX IF NOT EXISTS idx_project_agents_unassigned ON project_agents(unassigned_at) WHERE unassigned_at IS NOT NULL"
)
cursor.execute(
"CREATE UNIQUE INDEX IF NOT EXISTS idx_project_agents_unique_active ON project_agents(project_id, agent_id, is_active) WHERE is_active = TRUE"
)
# Blocker indexes
cursor.execute(
"CREATE INDEX IF NOT EXISTS idx_blockers_status_created ON blockers(status, created_at)"
)
cursor.execute(
"CREATE INDEX IF NOT EXISTS idx_blockers_agent_status ON blockers(agent_id, status)"
)
cursor.execute("CREATE INDEX IF NOT EXISTS idx_blockers_task_id ON blockers(task_id)")
# Lint results indexes
cursor.execute("CREATE INDEX IF NOT EXISTS idx_lint_results_task ON lint_results(task_id)")
cursor.execute(
"CREATE INDEX IF NOT EXISTS idx_lint_results_created ON lint_results(created_at DESC)"
)
# Context items indexes
cursor.execute(
"CREATE INDEX IF NOT EXISTS idx_context_project_agent ON context_items(project_id, agent_id, current_tier)"
)
# Context checkpoints indexes
cursor.execute(
"CREATE INDEX IF NOT EXISTS idx_checkpoints_agent_created ON context_checkpoints(agent_id, created_at DESC)"
)
# Test results indexes
cursor.execute("CREATE INDEX IF NOT EXISTS idx_test_results_task ON test_results(task_id)")
# Correction attempts indexes
cursor.execute(
"CREATE INDEX IF NOT EXISTS idx_correction_attempts_task ON correction_attempts(task_id)"
)
# Task dependencies indexes
cursor.execute(
"CREATE INDEX IF NOT EXISTS idx_task_dependencies_task ON task_dependencies(task_id)"
)
cursor.execute(
"CREATE INDEX IF NOT EXISTS idx_task_dependencies_depends_on ON task_dependencies(depends_on_task_id)"
)
# Code reviews indexes
cursor.execute("CREATE INDEX IF NOT EXISTS idx_reviews_task ON code_reviews(task_id)")
cursor.execute(
"CREATE INDEX IF NOT EXISTS idx_reviews_severity ON code_reviews(severity, created_at)"
)
cursor.execute(
"CREATE INDEX IF NOT EXISTS idx_reviews_project ON code_reviews(project_id, created_at)"
)
# Task evidence indexes
cursor.execute(
"CREATE INDEX IF NOT EXISTS idx_task_evidence_task ON task_evidence(task_id)"
)
cursor.execute(
"CREATE INDEX IF NOT EXISTS idx_task_evidence_verified ON task_evidence(verified, created_at DESC)"
)
# Token usage indexes
cursor.execute(
"CREATE INDEX IF NOT EXISTS idx_token_usage_agent ON token_usage(agent_id, timestamp)"
)
cursor.execute(
"CREATE INDEX IF NOT EXISTS idx_token_usage_project ON token_usage(project_id, timestamp)"
)
cursor.execute("CREATE INDEX IF NOT EXISTS idx_token_usage_task ON token_usage(task_id)")
# Checkpoints indexes
cursor.execute(
"CREATE INDEX IF NOT EXISTS idx_checkpoints_project ON checkpoints(project_id, created_at DESC)"
)
# Pull requests indexes (Sprint 11 - GitHub PR integration)
cursor.execute(
"CREATE INDEX IF NOT EXISTS idx_pull_requests_project ON pull_requests(project_id, status)"
)
cursor.execute(
"CREATE INDEX IF NOT EXISTS idx_pull_requests_issue ON pull_requests(issue_id)"
)
cursor.execute(
"CREATE INDEX IF NOT EXISTS idx_pull_requests_branch ON pull_requests(project_id, branch_name)"
)
# Audit logs indexes
cursor.execute(
"CREATE INDEX IF NOT EXISTS idx_audit_logs_user_id ON audit_logs(user_id, timestamp DESC)"
)
cursor.execute(
"CREATE INDEX IF NOT EXISTS idx_audit_logs_event_type ON audit_logs(event_type, timestamp DESC)"
)
cursor.execute(
"CREATE INDEX IF NOT EXISTS idx_audit_logs_resource ON audit_logs(resource_type, resource_id, timestamp DESC)"
)
# Authentication indexes
cursor.execute("CREATE INDEX IF NOT EXISTS idx_users_email ON users(email)")
cursor.execute("CREATE INDEX IF NOT EXISTS idx_sessions_user_id ON sessions(user_id)")
cursor.execute("CREATE INDEX IF NOT EXISTS idx_sessions_expires_at ON sessions(expires_at)")
cursor.execute(
"CREATE INDEX IF NOT EXISTS idx_project_users_user_id ON project_users(user_id)"
)
cursor.execute(
"CREATE INDEX IF NOT EXISTS idx_project_users_user_project ON project_users(user_id, project_id)"
)
cursor.execute("CREATE INDEX IF NOT EXISTS idx_projects_user_id ON projects(user_id)")
def _ensure_default_admin_user(self) -> None:
"""Ensure default admin user exists in database for initial setup.
Creates admin user with id=1 if it doesn't exist. This provides
a bootstrap user for test fixtures and initial database setup.
SECURITY: The admin user has a disabled password placeholder
that cannot match any bcrypt hash, so it cannot be used for
direct login. Users must register through the auth system.
Uses INSERT OR IGNORE to avoid conflicts with test fixtures.
"""
cursor = self.conn.cursor()
# Create user record (FastAPI Users compatible)
# hashed_password uses a placeholder that cannot match any bcrypt hash
cursor.execute(
"""
INSERT OR IGNORE INTO users (
id, email, name, hashed_password,
is_active, is_superuser, is_verified, email_verified
)
VALUES (1, 'admin@localhost', 'Admin User', '!DISABLED!', 1, 1, 1, 1)
"""