-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathregistry.py
More file actions
1450 lines (1176 loc) · 45.4 KB
/
registry.py
File metadata and controls
1450 lines (1176 loc) · 45.4 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
"""
Project Registry Module
=======================
Cross-platform project registry for storing project name to git URL mappings.
Uses SQLite database stored at ~/.zerocoder/registry.db.
Local clones are stored at:
- ~/.zerocoder/projects/{name}/ - Full clone for all operations (wizard, edit, beads)
Note: The separate beads-sync branch clone (~/.zerocoder/beads-sync/) is deprecated.
All beads operations now use the project directory directly.
"""
import logging
import os
import re
from contextlib import contextmanager
from datetime import datetime
from pathlib import Path
from typing import Any
from sqlalchemy import Boolean, CheckConstraint, Column, DateTime, Float, ForeignKey, Integer, String, Text, UniqueConstraint, create_engine
from sqlalchemy.exc import IntegrityError
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker
# Module logger
logger = logging.getLogger(__name__)
# =============================================================================
# Exceptions
# =============================================================================
class RegistryError(Exception):
"""Base registry exception."""
pass
class RegistryNotFound(RegistryError):
"""Registry file doesn't exist."""
pass
class RegistryCorrupted(RegistryError):
"""Registry database is corrupted."""
pass
class RegistryPermissionDenied(RegistryError):
"""Can't read/write registry file."""
pass
# =============================================================================
# SQLAlchemy Model
# =============================================================================
Base = declarative_base()
class Project(Base):
"""SQLAlchemy model for registered projects."""
__tablename__ = "projects"
name = Column(String(50), primary_key=True, index=True)
git_url = Column(String, nullable=False) # git@github.com:user/repo.git or https://...
target_container_count = Column(Integer, default=1) # 1-10 parallel agents
created_at = Column(DateTime, nullable=False)
__table_args__ = (
CheckConstraint(
'target_container_count >= 1 AND target_container_count <= 10',
name='valid_target_container_count'
),
)
@property
def local_path(self) -> Path:
"""Get the local clone path for this project."""
return get_projects_dir() / self.name
class Container(Base):
"""SQLAlchemy model for container instances."""
__tablename__ = "containers"
id = Column(Integer, primary_key=True, autoincrement=True)
project_name = Column(String(50), ForeignKey("projects.name", ondelete="CASCADE"), nullable=False, index=True)
container_number = Column(Integer, nullable=False)
container_type = Column(String(20), default='coding') # 'init' or 'coding'
docker_container_id = Column(String(100), nullable=True)
status = Column(String(20), default='created') # 'created', 'running', 'stopping', 'stopped'
current_feature = Column(String(50), nullable=True) # beads-42
created_at = Column(DateTime, nullable=False, default=datetime.now)
# Session-scoped state (cleared on server restart)
user_started_at = Column(DateTime, nullable=True) # Non-null = user started this container
graceful_stop_requested = Column(Boolean, default=False)
restarting = Column(Boolean, default=False)
last_agent_was_overseer = Column(Boolean, default=False)
is_milestone_overseer = Column(Boolean, default=False)
last_activity_at = Column(DateTime, nullable=True)
last_closed_feature = Column(String(50), nullable=True) # For reviewer agent
__table_args__ = (
UniqueConstraint('project_name', 'container_number', 'container_type', name='uq_container_identity'),
CheckConstraint("container_type IN ('init', 'coding')", name='valid_container_type'),
CheckConstraint("status IN ('created', 'running', 'stopping', 'stopped')", name='valid_container_status'),
)
class FeatureCache(Base):
"""Cached feature data from container polling."""
__tablename__ = "feature_cache"
project_name = Column(
String(50),
ForeignKey("projects.name", ondelete="CASCADE"),
primary_key=True,
index=True
)
feature_id = Column(String(50), primary_key=True)
priority = Column(Integer, default=999)
category = Column(String(100), default="")
name = Column(String(255), nullable=False)
description = Column(Text, default="")
steps_json = Column(Text, default="[]") # JSON array of steps
status = Column(String(20), nullable=False) # open, in_progress, closed
updated_at = Column(DateTime, nullable=False)
__table_args__ = (
CheckConstraint("status IN ('open', 'in_progress', 'closed')", name='valid_feature_status'),
)
class FeatureStatsCache(Base):
"""Cached aggregate feature stats for quick access."""
__tablename__ = "feature_stats_cache"
project_name = Column(
String(50),
ForeignKey("projects.name", ondelete="CASCADE"),
primary_key=True,
index=True
)
pending_count = Column(Integer, default=0)
in_progress_count = Column(Integer, default=0)
done_count = Column(Integer, default=0)
total_count = Column(Integer, default=0)
percentage = Column(Float, default=0.0)
last_polled_at = Column(DateTime, nullable=False)
poll_error = Column(String(500), nullable=True)
# Track last overseer milestone (0, 10, 20, ..., 90) for 10% periodic runs
last_overseer_milestone = Column(Integer, default=0)
class ProjectVerificationState(Base):
"""Session-scoped verification state (cleared on server restart)."""
__tablename__ = "project_verification_state"
project_name = Column(String(50), primary_key=True)
verification_running = Column(Boolean, default=False)
started_at = Column(DateTime, nullable=True)
# =============================================================================
# Database Connection
# =============================================================================
# Module-level singleton for database engine
_engine = None
_SessionLocal = None
def get_config_dir() -> Path:
"""
Get the config directory.
Uses ZEROCODER_DATA_DIR environment variable if set (for Docker),
otherwise defaults to ~/.zerocoder/
Returns:
Path to config directory (created if it doesn't exist)
"""
data_dir = os.getenv("ZEROCODER_DATA_DIR")
if data_dir:
config_dir = Path(data_dir) / "zerocoder"
else:
config_dir = Path.home() / ".zerocoder"
config_dir.mkdir(parents=True, exist_ok=True)
return config_dir
def get_projects_dir() -> Path:
"""
Get the projects directory for local clones.
Projects are cloned to ~/.zerocoder/projects/{name}/ for:
- Spec creation wizard (new projects)
- Task editing (edit mode)
Returns:
Path to ~/.zerocoder/projects/ (created if it doesn't exist)
"""
projects_dir = get_config_dir() / "projects"
projects_dir.mkdir(parents=True, exist_ok=True)
return projects_dir
def get_beads_sync_dir() -> Path:
"""
DEPRECATED: Get the beads-sync directory for beads-sync branch clones.
This function is deprecated. All beads operations now use the project
directory directly via get_projects_dir(). This function is kept for
backwards compatibility but should not be used for new code.
Returns:
Path to ~/.zerocoder/beads-sync/ (created if it doesn't exist)
"""
import warnings
warnings.warn(
"get_beads_sync_dir() is deprecated. Use get_projects_dir() instead.",
DeprecationWarning,
stacklevel=2
)
beads_sync_dir = get_config_dir() / "beads-sync"
beads_sync_dir.mkdir(parents=True, exist_ok=True)
return beads_sync_dir
def get_registry_path() -> Path:
"""Get the path to the registry database."""
return get_config_dir() / "registry.db"
def _migrate_schema(engine) -> None:
"""
Migrate database schema to add new columns.
This handles upgrading existing databases with new columns.
SQLite doesn't support adding columns with constraints inline,
so we add them with defaults.
"""
from sqlalchemy import text, inspect
inspector = inspect(engine)
# Check if containers table exists and needs migration
if 'containers' in inspector.get_table_names():
columns = {col['name'] for col in inspector.get_columns('containers')}
new_columns = [
('user_started_at', 'DATETIME'),
('graceful_stop_requested', 'BOOLEAN DEFAULT 0'),
('restarting', 'BOOLEAN DEFAULT 0'),
('last_agent_was_overseer', 'BOOLEAN DEFAULT 0'),
('is_milestone_overseer', 'BOOLEAN DEFAULT 0'),
('last_activity_at', 'DATETIME'),
('last_closed_feature', 'VARCHAR(50)'),
]
with engine.connect() as conn:
for col_name, col_type in new_columns:
if col_name not in columns:
try:
conn.execute(text(f'ALTER TABLE containers ADD COLUMN {col_name} {col_type}'))
conn.commit()
logger.info(f"Added column {col_name} to containers table")
except Exception as e:
logger.debug(f"Column {col_name} may already exist: {e}")
# Migrate feature_stats_cache table
if 'feature_stats_cache' in inspector.get_table_names():
columns = {col['name'] for col in inspector.get_columns('feature_stats_cache')}
stats_columns = [
('last_overseer_milestone', 'INTEGER DEFAULT 0'),
]
with engine.connect() as conn:
for col_name, col_type in stats_columns:
if col_name not in columns:
try:
conn.execute(text(f'ALTER TABLE feature_stats_cache ADD COLUMN {col_name} {col_type}'))
conn.commit()
logger.info(f"Added column {col_name} to feature_stats_cache table")
except Exception as e:
logger.debug(f"Column {col_name} may already exist: {e}")
# Create project_verification_state table if it doesn't exist
# (Base.metadata.create_all handles this, but we log for visibility)
if 'project_verification_state' not in inspector.get_table_names():
logger.info("Creating project_verification_state table")
def _get_engine():
"""
Get or create the database engine (singleton pattern).
Returns:
Tuple of (engine, SessionLocal)
"""
global _engine, _SessionLocal
if _engine is None:
db_path = get_registry_path()
db_url = f"sqlite:///{db_path.as_posix()}"
_engine = create_engine(db_url, connect_args={"check_same_thread": False})
# Migrate schema for existing databases
_migrate_schema(_engine)
# Create any new tables
Base.metadata.create_all(bind=_engine)
_SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=_engine)
logger.debug("Initialized registry database at: %s", db_path)
return _engine, _SessionLocal
@contextmanager
def _get_session():
"""
Context manager for database sessions with automatic commit/rollback.
Yields:
SQLAlchemy session
"""
_, SessionLocal = _get_engine()
session = SessionLocal()
try:
yield session
session.commit()
except Exception:
session.rollback()
raise
finally:
session.close()
# =============================================================================
# Project CRUD Functions
# =============================================================================
def register_project(name: str, git_url: str, **kwargs) -> None:
"""
Register a new project in the registry.
Args:
name: The project name (unique identifier).
git_url: Git repository URL (https:// or git@).
Raises:
ValueError: If project name is invalid or git_url is invalid.
RegistryError: If a project with that name already exists.
"""
# Validate name
if not re.match(r'^[a-zA-Z0-9_-]{1,50}$', name):
raise ValueError(
"Invalid project name. Use only letters, numbers, hyphens, "
"and underscores (1-50 chars)."
)
# Validate git URL
if not (git_url.startswith('https://') or git_url.startswith('git@')):
raise ValueError("Invalid git URL. Must start with https:// or git@")
with _get_session() as session:
existing = session.query(Project).filter(Project.name == name).first()
if existing:
logger.warning("Attempted to register duplicate project: %s", name)
raise RegistryError(f"Project '{name}' already exists in registry")
project = Project(
name=name,
git_url=git_url,
target_container_count=1,
created_at=datetime.now()
)
session.add(project)
logger.info("Registered project '%s' with git URL: %s", name, git_url)
def unregister_project(name: str) -> bool:
"""
Remove a project from the registry.
Args:
name: The project name to remove.
Returns:
True if removed, False if project wasn't found.
"""
with _get_session() as session:
project = session.query(Project).filter(Project.name == name).first()
if not project:
logger.debug("Attempted to unregister non-existent project: %s", name)
return False
session.delete(project)
logger.info("Unregistered project: %s", name)
return True
def get_project_path(name: str) -> Path | None:
"""
Look up a project's local clone path by name.
Args:
name: The project name.
Returns:
The project local clone Path, or None if not found.
"""
_, SessionLocal = _get_engine()
session = SessionLocal()
try:
project = session.query(Project).filter(Project.name == name).first()
if project is None:
return None
return get_projects_dir() / name
finally:
session.close()
def get_project_git_url(name: str) -> str | None:
"""
Look up a project's git URL by name.
Args:
name: The project name.
Returns:
The project git URL, or None if not found.
"""
_, SessionLocal = _get_engine()
session = SessionLocal()
try:
project = session.query(Project).filter(Project.name == name).first()
if project is None:
return None
return project.git_url
finally:
session.close()
def list_registered_projects() -> dict[str, dict[str, Any]]:
"""
Get all registered projects.
Returns:
Dictionary mapping project names to their info dictionaries.
"""
_, SessionLocal = _get_engine()
session = SessionLocal()
try:
projects = session.query(Project).all()
projects_dir = get_projects_dir()
result = {}
for p in projects:
local_path = projects_dir / p.name
has_beads = (local_path / ".beads" / "beads.db").exists()
result[p.name] = {
"git_url": p.git_url,
"is_new": not has_beads,
"target_container_count": p.target_container_count,
"local_path": local_path.as_posix(),
"created_at": p.created_at.isoformat() if p.created_at else None
}
return result
finally:
session.close()
def get_project_info(name: str) -> dict[str, Any] | None:
"""
Get full info about a project.
Args:
name: The project name.
Returns:
Project info dictionary, or None if not found.
"""
_, SessionLocal = _get_engine()
session = SessionLocal()
try:
project = session.query(Project).filter(Project.name == name).first()
if project is None:
return None
local_path = get_projects_dir() / project.name
has_beads = (local_path / ".beads" / "beads.db").exists()
return {
"git_url": project.git_url,
"is_new": not has_beads,
"target_container_count": project.target_container_count,
"local_path": local_path.as_posix(),
"created_at": project.created_at.isoformat() if project.created_at else None
}
finally:
session.close()
def update_project_git_url(name: str, new_git_url: str) -> bool:
"""
Update a project's git URL.
Args:
name: The project name.
new_git_url: The new git URL.
Returns:
True if updated, False if project wasn't found.
"""
if not (new_git_url.startswith('https://') or new_git_url.startswith('git@')):
raise ValueError("Invalid git URL. Must start with https:// or git@")
with _get_session() as session:
project = session.query(Project).filter(Project.name == name).first()
if not project:
return False
project.git_url = new_git_url
return True
def mark_project_initialized(name: str) -> bool:
"""No-op. Project state is now derived from beads on disk."""
return True
def update_target_container_count(name: str, count: int) -> bool:
"""
Update a project's target container count.
Args:
name: The project name.
count: The target container count (1-10).
Returns:
True if updated, False if project wasn't found.
"""
if not 1 <= count <= 10:
raise ValueError("Container count must be between 1 and 10")
with _get_session() as session:
project = session.query(Project).filter(Project.name == name).first()
if not project:
return False
project.target_container_count = count
return True
# =============================================================================
# Validation Functions
# =============================================================================
def validate_project_path(path: Path) -> tuple[bool, str]:
"""
Validate that a project path is accessible and writable.
Args:
path: The path to validate.
Returns:
Tuple of (is_valid, error_message).
"""
path = Path(path).resolve()
# Check if path exists
if not path.exists():
return False, f"Path does not exist: {path}"
# Check if it's a directory
if not path.is_dir():
return False, f"Path is not a directory: {path}"
# Check read permissions
if not os.access(path, os.R_OK):
return False, f"No read permission: {path}"
# Check write permissions
if not os.access(path, os.W_OK):
return False, f"No write permission: {path}"
return True, ""
def validate_git_url(git_url: str) -> tuple[bool, str]:
"""
Validate that a git URL is properly formatted.
Args:
git_url: The git URL to validate.
Returns:
Tuple of (is_valid, error_message).
"""
if not git_url:
return False, "Git URL cannot be empty"
if not (git_url.startswith('https://') or git_url.startswith('git@')):
return False, "Git URL must start with https:// or git@"
return True, ""
def cleanup_stale_projects() -> list[str]:
"""
Remove projects from registry whose local clones no longer exist.
Returns:
List of removed project names.
"""
removed = []
with _get_session() as session:
projects = session.query(Project).all()
for project in projects:
local_path = get_projects_dir() / project.name
if not local_path.exists():
session.delete(project)
removed.append(project.name)
if removed:
logger.info("Cleaned up stale projects: %s", removed)
return removed
def list_valid_projects() -> list[dict[str, Any]]:
"""
List all projects that have valid, accessible local clones.
Returns:
List of project info dicts with additional 'name' field.
"""
_, SessionLocal = _get_engine()
session = SessionLocal()
try:
projects = session.query(Project).all()
valid = []
for p in projects:
local_path = get_projects_dir() / p.name
is_valid, _ = validate_project_path(local_path)
if is_valid:
has_beads = (local_path / ".beads" / "beads.db").exists()
valid.append({
"name": p.name,
"git_url": p.git_url,
"is_new": not has_beads,
"target_container_count": p.target_container_count,
"local_path": local_path.as_posix(),
"created_at": p.created_at.isoformat() if p.created_at else None
})
return valid
finally:
session.close()
# =============================================================================
# Container CRUD Functions
# =============================================================================
def create_container(project_name: str, container_number: int, container_type: str = 'coding') -> int:
"""
Create or get an existing container record.
If a container with the same (project_name, container_number, container_type)
already exists, returns its ID and resets its status to 'created'.
Args:
project_name: The project this container belongs to.
container_number: The container number (1-10).
container_type: 'init' or 'coding'.
Returns:
The container's ID.
"""
with _get_session() as session:
# Check if container already exists
existing = session.query(Container).filter(
Container.project_name == project_name,
Container.container_number == container_number,
Container.container_type == container_type
).first()
if existing:
# Reset status for reuse
existing.status = 'created'
existing.current_feature = None
session.flush()
return existing.id
# Create new container
container = Container(
project_name=project_name,
container_number=container_number,
container_type=container_type,
status='created',
created_at=datetime.now()
)
session.add(container)
try:
session.flush()
return container.id
except IntegrityError:
# Race condition: another thread created the container between our check and insert
session.rollback()
existing = session.query(Container).filter(
Container.project_name == project_name,
Container.container_number == container_number,
Container.container_type == container_type
).first()
if existing:
existing.status = 'created'
existing.current_feature = None
session.flush()
return existing.id
raise # Re-raise if still not found (shouldn't happen)
def get_container(project_name: str, container_number: int, container_type: str = 'coding') -> dict[str, Any] | None:
"""
Get a container record by project, number, and type.
Returns:
Container info dict, or None if not found.
"""
_, SessionLocal = _get_engine()
session = SessionLocal()
try:
container = session.query(Container).filter(
Container.project_name == project_name,
Container.container_number == container_number,
Container.container_type == container_type
).first()
if not container:
return None
return {
"id": container.id,
"project_name": container.project_name,
"container_number": container.container_number,
"container_type": container.container_type,
"docker_container_id": container.docker_container_id,
"status": container.status,
"current_feature": container.current_feature,
"created_at": container.created_at.isoformat() if container.created_at else None
}
finally:
session.close()
def delete_container(project_name: str, container_number: int, container_type: str = 'coding') -> bool:
"""
Delete a container record from the database.
Args:
project_name: Name of the project
container_number: Container number
container_type: Container type (default 'coding')
Returns:
True if deleted, False if not found.
"""
_, SessionLocal = _get_engine()
session = SessionLocal()
try:
container = session.query(Container).filter(
Container.project_name == project_name,
Container.container_number == container_number,
Container.container_type == container_type
).first()
if container:
session.delete(container)
session.commit()
return True
return False
finally:
session.close()
def delete_invalid_containers(project_name: str) -> int:
"""
Delete containers with invalid container_number (e.g., -1).
Args:
project_name: Name of the project
Returns:
Count of deleted records.
"""
_, SessionLocal = _get_engine()
session = SessionLocal()
try:
result = session.query(Container).filter(
Container.project_name == project_name,
Container.container_number < 0
).delete()
session.commit()
return result
finally:
session.close()
def list_containers(status_filter: list[str] | None = None) -> list[Container]:
"""
List all containers across all projects.
Args:
status_filter: Optional list of statuses to filter by (e.g., ['running', 'stopping']).
Returns:
List of Container model objects.
"""
_, SessionLocal = _get_engine()
session = SessionLocal()
try:
query = session.query(Container)
if status_filter:
query = query.filter(Container.status.in_(status_filter))
return query.all()
finally:
session.close()
def list_project_containers(project_name: str, container_type: str | None = None) -> list[dict[str, Any]]:
"""
List all containers for a project.
Args:
project_name: The project name.
container_type: Filter by type ('init', 'coding'), or None for all.
Returns:
List of container info dicts.
"""
_, SessionLocal = _get_engine()
session = SessionLocal()
try:
query = session.query(Container).filter(Container.project_name == project_name)
if container_type:
query = query.filter(Container.container_type == container_type)
containers = query.order_by(Container.container_number).all()
return [
{
"id": c.id,
"project_name": c.project_name,
"container_number": c.container_number,
"container_type": c.container_type,
"docker_container_id": c.docker_container_id,
"status": c.status,
"current_feature": c.current_feature,
"created_at": c.created_at.isoformat() if c.created_at else None
}
for c in containers
]
finally:
session.close()
def update_container_status(
project_name: str,
container_number: int,
container_type: str = 'coding',
status: str | None = None,
docker_container_id: str | None = None,
current_feature: str | None = None
) -> bool:
"""
Update a container's status and/or docker ID.
Returns:
True if updated, False if not found.
"""
with _get_session() as session:
container = session.query(Container).filter(
Container.project_name == project_name,
Container.container_number == container_number,
Container.container_type == container_type
).first()
if not container:
return False
if status is not None:
container.status = status
if docker_container_id is not None:
container.docker_container_id = docker_container_id
if current_feature is not None:
# Empty string clears the feature, any other value sets it
container.current_feature = current_feature if current_feature else None
return True
def delete_container(project_name: str, container_number: int, container_type: str = 'coding') -> bool:
"""
Delete a container record.
Returns:
True if deleted, False if not found.
"""
with _get_session() as session:
container = session.query(Container).filter(
Container.project_name == project_name,
Container.container_number == container_number,
Container.container_type == container_type
).first()
if not container:
return False
session.delete(container)
return True
def delete_all_project_containers(project_name: str) -> int:
"""
Delete all container records for a project.
Returns:
Number of containers deleted.
"""
with _get_session() as session:
deleted = session.query(Container).filter(Container.project_name == project_name).delete()
return deleted
def list_all_containers() -> list[dict]:
"""
List all containers across all projects as dicts.
Returns:
List of container info dicts.
"""
_, SessionLocal = _get_engine()
session = SessionLocal()
try:
containers = session.query(Container).all()
return [
{
"project_name": c.project_name,
"container_number": c.container_number,
"container_type": c.container_type or "coding",
"status": c.status,
}
for c in containers
]
finally:
session.close()
# =============================================================================
# Overseer Milestone Tracking Functions
# =============================================================================
def get_overseer_milestone(project_name: str) -> int:
"""
Get the last overseer milestone for a project.
Args:
project_name: The project name.
Returns:
Last overseer milestone (0, 10, 20, ..., 90), or 0 if not found.
"""
_, SessionLocal = _get_engine()
session = SessionLocal()
try:
cache = session.query(FeatureStatsCache).filter_by(project_name=project_name).first()
if cache is None:
return 0
return cache.last_overseer_milestone or 0
finally:
session.close()
def update_overseer_milestone(project_name: str, milestone: int) -> bool:
"""
Update the last overseer milestone for a project.
Args:
project_name: The project name.
milestone: The milestone value (0, 10, 20, ..., 90).
Returns:
True if updated, False if stats cache not found.
"""
with _get_session() as session:
cache = session.query(FeatureStatsCache).filter_by(project_name=project_name).first()