forked from Rockachopa/Timmy-time-dashboard
Compare commits
32 Commits
review-fix
...
fix/csrf-c
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
59e789e2da | ||
| d2a5866650 | |||
| 2381d0b6d0 | |||
| 03ad2027a4 | |||
| 2bfc44ea1b | |||
| fe1fa78ef1 | |||
| 3c46a1b202 | |||
| 001358c64f | |||
| faad0726a2 | |||
| dd4410fe57 | |||
| ef7f31070b | |||
| 6f66670396 | |||
| 4cdd82818b | |||
| 99ad672e4d | |||
| a3f61c67d3 | |||
| 32dbdc68c8 | |||
| 84302aedac | |||
| 2c217104db | |||
| 7452e8a4f0 | |||
| 9732c80892 | |||
| f3b3d1e648 | |||
| 4ba8d25749 | |||
| 2622f0a0fb | |||
| e3d60b89a9 | |||
| 6214ad3225 | |||
| 5f5da2163f | |||
| 0029c34bb1 | |||
| 2577b71207 | |||
| 1a8b8ecaed | |||
| d821e76589 | |||
| bc010ecfba | |||
| faf6c1a5f1 |
@@ -54,6 +54,7 @@ REPO_ROOT = Path(__file__).resolve().parent.parent
|
|||||||
RETRO_FILE = REPO_ROOT / ".loop" / "retro" / "cycles.jsonl"
|
RETRO_FILE = REPO_ROOT / ".loop" / "retro" / "cycles.jsonl"
|
||||||
SUMMARY_FILE = REPO_ROOT / ".loop" / "retro" / "summary.json"
|
SUMMARY_FILE = REPO_ROOT / ".loop" / "retro" / "summary.json"
|
||||||
EPOCH_COUNTER_FILE = REPO_ROOT / ".loop" / "retro" / ".epoch_counter"
|
EPOCH_COUNTER_FILE = REPO_ROOT / ".loop" / "retro" / ".epoch_counter"
|
||||||
|
CYCLE_RESULT_FILE = REPO_ROOT / ".loop" / "cycle_result.json"
|
||||||
|
|
||||||
# How many recent entries to include in rolling summary
|
# How many recent entries to include in rolling summary
|
||||||
SUMMARY_WINDOW = 50
|
SUMMARY_WINDOW = 50
|
||||||
@@ -246,9 +247,37 @@ def update_summary() -> None:
|
|||||||
SUMMARY_FILE.write_text(json.dumps(summary, indent=2) + "\n")
|
SUMMARY_FILE.write_text(json.dumps(summary, indent=2) + "\n")
|
||||||
|
|
||||||
|
|
||||||
|
def _load_cycle_result() -> dict:
|
||||||
|
"""Read .loop/cycle_result.json if it exists; return empty dict on failure."""
|
||||||
|
if not CYCLE_RESULT_FILE.exists():
|
||||||
|
return {}
|
||||||
|
try:
|
||||||
|
raw = CYCLE_RESULT_FILE.read_text().strip()
|
||||||
|
# Strip hermes fence markers (```json ... ```) if present
|
||||||
|
if raw.startswith("```"):
|
||||||
|
lines = raw.splitlines()
|
||||||
|
lines = [l for l in lines if not l.startswith("```")]
|
||||||
|
raw = "\n".join(lines)
|
||||||
|
return json.loads(raw)
|
||||||
|
except (json.JSONDecodeError, OSError):
|
||||||
|
return {}
|
||||||
|
|
||||||
|
|
||||||
def main() -> None:
|
def main() -> None:
|
||||||
args = parse_args()
|
args = parse_args()
|
||||||
|
|
||||||
|
# Backfill from cycle_result.json when CLI args have defaults
|
||||||
|
cr = _load_cycle_result()
|
||||||
|
if cr:
|
||||||
|
if args.issue is None and cr.get("issue"):
|
||||||
|
args.issue = int(cr["issue"])
|
||||||
|
if args.type == "unknown" and cr.get("type"):
|
||||||
|
args.type = cr["type"]
|
||||||
|
if args.tests_passed == 0 and cr.get("tests_passed"):
|
||||||
|
args.tests_passed = int(cr["tests_passed"])
|
||||||
|
if not args.notes and cr.get("notes"):
|
||||||
|
args.notes = cr["notes"]
|
||||||
|
|
||||||
# Auto-detect issue from branch when not explicitly provided
|
# Auto-detect issue from branch when not explicitly provided
|
||||||
if args.issue is None:
|
if args.issue is None:
|
||||||
args.issue = detect_issue_from_branch()
|
args.issue = detect_issue_from_branch()
|
||||||
|
|||||||
@@ -84,6 +84,7 @@ class Settings(BaseSettings):
|
|||||||
# Only used when explicitly enabled and query complexity warrants it.
|
# Only used when explicitly enabled and query complexity warrants it.
|
||||||
grok_enabled: bool = False
|
grok_enabled: bool = False
|
||||||
xai_api_key: str = ""
|
xai_api_key: str = ""
|
||||||
|
xai_base_url: str = "https://api.x.ai/v1"
|
||||||
grok_default_model: str = "grok-3-fast"
|
grok_default_model: str = "grok-3-fast"
|
||||||
grok_max_sats_per_query: int = 200
|
grok_max_sats_per_query: int = 200
|
||||||
grok_free: bool = False # Skip Lightning invoice when user has own API key
|
grok_free: bool = False # Skip Lightning invoice when user has own API key
|
||||||
|
|||||||
@@ -46,6 +46,7 @@ from dashboard.routes.tasks import router as tasks_router
|
|||||||
from dashboard.routes.telegram import router as telegram_router
|
from dashboard.routes.telegram import router as telegram_router
|
||||||
from dashboard.routes.thinking import router as thinking_router
|
from dashboard.routes.thinking import router as thinking_router
|
||||||
from dashboard.routes.tools import router as tools_router
|
from dashboard.routes.tools import router as tools_router
|
||||||
|
from dashboard.routes.tower import router as tower_router
|
||||||
from dashboard.routes.voice import router as voice_router
|
from dashboard.routes.voice import router as voice_router
|
||||||
from dashboard.routes.work_orders import router as work_orders_router
|
from dashboard.routes.work_orders import router as work_orders_router
|
||||||
from dashboard.routes.world import router as world_router
|
from dashboard.routes.world import router as world_router
|
||||||
@@ -376,60 +377,23 @@ def _startup_background_tasks() -> list[asyncio.Task]:
|
|||||||
]
|
]
|
||||||
|
|
||||||
|
|
||||||
def _startup_pruning() -> None:
|
def _try_prune(label: str, prune_fn, days: int) -> None:
|
||||||
"""Auto-prune old memories, thoughts, and events on startup."""
|
"""Run a prune function, log results, swallow errors."""
|
||||||
if settings.memory_prune_days > 0:
|
|
||||||
try:
|
try:
|
||||||
from timmy.memory_system import prune_memories
|
pruned = prune_fn()
|
||||||
|
|
||||||
pruned = prune_memories(
|
|
||||||
older_than_days=settings.memory_prune_days,
|
|
||||||
keep_facts=settings.memory_prune_keep_facts,
|
|
||||||
)
|
|
||||||
if pruned:
|
if pruned:
|
||||||
logger.info(
|
logger.info(
|
||||||
"Memory auto-prune: removed %d entries older than %d days",
|
"%s auto-prune: removed %d entries older than %d days",
|
||||||
|
label,
|
||||||
pruned,
|
pruned,
|
||||||
settings.memory_prune_days,
|
days,
|
||||||
)
|
)
|
||||||
except Exception as exc:
|
except Exception as exc:
|
||||||
logger.debug("Memory auto-prune skipped: %s", exc)
|
logger.debug("%s auto-prune skipped: %s", label, exc)
|
||||||
|
|
||||||
if settings.thoughts_prune_days > 0:
|
|
||||||
try:
|
|
||||||
from timmy.thinking import thinking_engine
|
|
||||||
|
|
||||||
pruned = thinking_engine.prune_old_thoughts(
|
def _check_vault_size() -> None:
|
||||||
keep_days=settings.thoughts_prune_days,
|
"""Warn if the memory vault exceeds the configured size limit."""
|
||||||
keep_min=settings.thoughts_prune_keep_min,
|
|
||||||
)
|
|
||||||
if pruned:
|
|
||||||
logger.info(
|
|
||||||
"Thought auto-prune: removed %d entries older than %d days",
|
|
||||||
pruned,
|
|
||||||
settings.thoughts_prune_days,
|
|
||||||
)
|
|
||||||
except Exception as exc:
|
|
||||||
logger.debug("Thought auto-prune skipped: %s", exc)
|
|
||||||
|
|
||||||
if settings.events_prune_days > 0:
|
|
||||||
try:
|
|
||||||
from swarm.event_log import prune_old_events
|
|
||||||
|
|
||||||
pruned = prune_old_events(
|
|
||||||
keep_days=settings.events_prune_days,
|
|
||||||
keep_min=settings.events_prune_keep_min,
|
|
||||||
)
|
|
||||||
if pruned:
|
|
||||||
logger.info(
|
|
||||||
"Event auto-prune: removed %d entries older than %d days",
|
|
||||||
pruned,
|
|
||||||
settings.events_prune_days,
|
|
||||||
)
|
|
||||||
except Exception as exc:
|
|
||||||
logger.debug("Event auto-prune skipped: %s", exc)
|
|
||||||
|
|
||||||
if settings.memory_vault_max_mb > 0:
|
|
||||||
try:
|
try:
|
||||||
vault_path = Path(settings.repo_root) / "memory" / "notes"
|
vault_path = Path(settings.repo_root) / "memory" / "notes"
|
||||||
if vault_path.exists():
|
if vault_path.exists():
|
||||||
@@ -445,6 +409,48 @@ def _startup_pruning() -> None:
|
|||||||
logger.debug("Vault size check skipped: %s", exc)
|
logger.debug("Vault size check skipped: %s", exc)
|
||||||
|
|
||||||
|
|
||||||
|
def _startup_pruning() -> None:
|
||||||
|
"""Auto-prune old memories, thoughts, and events on startup."""
|
||||||
|
if settings.memory_prune_days > 0:
|
||||||
|
from timmy.memory_system import prune_memories
|
||||||
|
|
||||||
|
_try_prune(
|
||||||
|
"Memory",
|
||||||
|
lambda: prune_memories(
|
||||||
|
older_than_days=settings.memory_prune_days,
|
||||||
|
keep_facts=settings.memory_prune_keep_facts,
|
||||||
|
),
|
||||||
|
settings.memory_prune_days,
|
||||||
|
)
|
||||||
|
|
||||||
|
if settings.thoughts_prune_days > 0:
|
||||||
|
from timmy.thinking import thinking_engine
|
||||||
|
|
||||||
|
_try_prune(
|
||||||
|
"Thought",
|
||||||
|
lambda: thinking_engine.prune_old_thoughts(
|
||||||
|
keep_days=settings.thoughts_prune_days,
|
||||||
|
keep_min=settings.thoughts_prune_keep_min,
|
||||||
|
),
|
||||||
|
settings.thoughts_prune_days,
|
||||||
|
)
|
||||||
|
|
||||||
|
if settings.events_prune_days > 0:
|
||||||
|
from swarm.event_log import prune_old_events
|
||||||
|
|
||||||
|
_try_prune(
|
||||||
|
"Event",
|
||||||
|
lambda: prune_old_events(
|
||||||
|
keep_days=settings.events_prune_days,
|
||||||
|
keep_min=settings.events_prune_keep_min,
|
||||||
|
),
|
||||||
|
settings.events_prune_days,
|
||||||
|
)
|
||||||
|
|
||||||
|
if settings.memory_vault_max_mb > 0:
|
||||||
|
_check_vault_size()
|
||||||
|
|
||||||
|
|
||||||
async def _shutdown_cleanup(
|
async def _shutdown_cleanup(
|
||||||
bg_tasks: list[asyncio.Task],
|
bg_tasks: list[asyncio.Task],
|
||||||
workshop_heartbeat,
|
workshop_heartbeat,
|
||||||
@@ -583,6 +589,7 @@ app.include_router(system_router)
|
|||||||
app.include_router(experiments_router)
|
app.include_router(experiments_router)
|
||||||
app.include_router(db_explorer_router)
|
app.include_router(db_explorer_router)
|
||||||
app.include_router(world_router)
|
app.include_router(world_router)
|
||||||
|
app.include_router(tower_router)
|
||||||
|
|
||||||
|
|
||||||
@app.websocket("/ws")
|
@app.websocket("/ws")
|
||||||
|
|||||||
@@ -175,18 +175,12 @@ class CSRFMiddleware(BaseHTTPMiddleware):
|
|||||||
return await call_next(request)
|
return await call_next(request)
|
||||||
|
|
||||||
# Token validation failed and path is not exempt
|
# Token validation failed and path is not exempt
|
||||||
# We still need to call the app to check if the endpoint is decorated
|
# Resolve the endpoint from routes BEFORE executing to avoid side effects
|
||||||
# with @csrf_exempt, so we'll let it through and check after routing
|
endpoint = self._resolve_endpoint(request)
|
||||||
response = await call_next(request)
|
|
||||||
|
|
||||||
# After routing, check if the endpoint is marked as exempt
|
|
||||||
endpoint = request.scope.get("endpoint")
|
|
||||||
if endpoint and is_csrf_exempt(endpoint):
|
if endpoint and is_csrf_exempt(endpoint):
|
||||||
# Endpoint is marked as exempt, allow the response
|
return await call_next(request)
|
||||||
return response
|
|
||||||
|
|
||||||
# Endpoint is not exempt and token validation failed
|
# Endpoint is not exempt and token validation failed — reject without executing
|
||||||
# Return 403 error
|
|
||||||
return JSONResponse(
|
return JSONResponse(
|
||||||
status_code=403,
|
status_code=403,
|
||||||
content={
|
content={
|
||||||
@@ -196,6 +190,42 @@ class CSRFMiddleware(BaseHTTPMiddleware):
|
|||||||
},
|
},
|
||||||
)
|
)
|
||||||
|
|
||||||
|
def _resolve_endpoint(self, request: Request) -> Callable | None:
|
||||||
|
"""Resolve the endpoint for a request without executing it.
|
||||||
|
|
||||||
|
Walks the app chain to find routes, then matches against the request
|
||||||
|
scope. This allows checking @csrf_exempt before the handler runs
|
||||||
|
(avoiding side effects on CSRF rejection).
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
The endpoint callable if found, None otherwise.
|
||||||
|
"""
|
||||||
|
try:
|
||||||
|
from starlette.routing import Match
|
||||||
|
|
||||||
|
# Walk the middleware/app chain to find something with routes
|
||||||
|
routes = None
|
||||||
|
current = self.app
|
||||||
|
for _ in range(10): # Safety limit
|
||||||
|
routes = getattr(current, "routes", None)
|
||||||
|
if routes:
|
||||||
|
break
|
||||||
|
current = getattr(current, "app", None)
|
||||||
|
if current is None:
|
||||||
|
break
|
||||||
|
|
||||||
|
if not routes:
|
||||||
|
return None
|
||||||
|
|
||||||
|
scope = dict(request.scope)
|
||||||
|
for route in routes:
|
||||||
|
match, child_scope = route.matches(scope)
|
||||||
|
if match == Match.FULL:
|
||||||
|
return child_scope.get("endpoint")
|
||||||
|
except Exception:
|
||||||
|
logger.debug("Failed to resolve endpoint for CSRF check")
|
||||||
|
return None
|
||||||
|
|
||||||
def _is_likely_exempt(self, path: str) -> bool:
|
def _is_likely_exempt(self, path: str) -> bool:
|
||||||
"""Check if a path is likely to be CSRF exempt.
|
"""Check if a path is likely to be CSRF exempt.
|
||||||
|
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
from datetime import date, datetime
|
from datetime import UTC, date, datetime
|
||||||
from enum import StrEnum
|
from enum import StrEnum
|
||||||
|
|
||||||
from sqlalchemy import JSON, Boolean, Column, Date, DateTime, Index, Integer, String
|
from sqlalchemy import JSON, Boolean, Column, Date, DateTime, Index, Integer, String
|
||||||
@@ -40,8 +40,13 @@ class Task(Base):
|
|||||||
deferred_at = Column(DateTime, nullable=True)
|
deferred_at = Column(DateTime, nullable=True)
|
||||||
|
|
||||||
# Timestamps
|
# Timestamps
|
||||||
created_at = Column(DateTime, default=datetime.utcnow, nullable=False)
|
created_at = Column(DateTime, default=lambda: datetime.now(UTC), nullable=False)
|
||||||
updated_at = Column(DateTime, default=datetime.utcnow, onupdate=datetime.utcnow, nullable=False)
|
updated_at = Column(
|
||||||
|
DateTime,
|
||||||
|
default=lambda: datetime.now(UTC),
|
||||||
|
onupdate=lambda: datetime.now(UTC),
|
||||||
|
nullable=False,
|
||||||
|
)
|
||||||
|
|
||||||
__table_args__ = (Index("ix_task_state_order", "state", "sort_order"),)
|
__table_args__ = (Index("ix_task_state_order", "state", "sort_order"),)
|
||||||
|
|
||||||
@@ -59,4 +64,4 @@ class JournalEntry(Base):
|
|||||||
gratitude = Column(String(500), nullable=True)
|
gratitude = Column(String(500), nullable=True)
|
||||||
energy_level = Column(Integer, nullable=True) # User-reported, 1-10
|
energy_level = Column(Integer, nullable=True) # User-reported, 1-10
|
||||||
|
|
||||||
created_at = Column(DateTime, default=datetime.utcnow, nullable=False)
|
created_at = Column(DateTime, default=lambda: datetime.now(UTC), nullable=False)
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
import logging
|
import logging
|
||||||
from datetime import date, datetime
|
from datetime import UTC, date, datetime
|
||||||
|
|
||||||
from fastapi import APIRouter, Depends, Form, HTTPException, Request
|
from fastapi import APIRouter, Depends, Form, HTTPException, Request
|
||||||
from fastapi.responses import HTMLResponse
|
from fastapi.responses import HTMLResponse
|
||||||
@@ -19,14 +19,17 @@ router = APIRouter(tags=["calm"])
|
|||||||
|
|
||||||
# Helper functions for state machine logic
|
# Helper functions for state machine logic
|
||||||
def get_now_task(db: Session) -> Task | None:
|
def get_now_task(db: Session) -> Task | None:
|
||||||
|
"""Return the single active NOW task, or None."""
|
||||||
return db.query(Task).filter(Task.state == TaskState.NOW).first()
|
return db.query(Task).filter(Task.state == TaskState.NOW).first()
|
||||||
|
|
||||||
|
|
||||||
def get_next_task(db: Session) -> Task | None:
|
def get_next_task(db: Session) -> Task | None:
|
||||||
|
"""Return the single queued NEXT task, or None."""
|
||||||
return db.query(Task).filter(Task.state == TaskState.NEXT).first()
|
return db.query(Task).filter(Task.state == TaskState.NEXT).first()
|
||||||
|
|
||||||
|
|
||||||
def get_later_tasks(db: Session) -> list[Task]:
|
def get_later_tasks(db: Session) -> list[Task]:
|
||||||
|
"""Return all LATER tasks ordered by MIT flag then sort_order."""
|
||||||
return (
|
return (
|
||||||
db.query(Task)
|
db.query(Task)
|
||||||
.filter(Task.state == TaskState.LATER)
|
.filter(Task.state == TaskState.LATER)
|
||||||
@@ -35,7 +38,63 @@ def get_later_tasks(db: Session) -> list[Task]:
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def _create_mit_tasks(db: Session, titles: list[str | None]) -> list[int]:
|
||||||
|
"""Create MIT tasks from a list of titles, return their IDs."""
|
||||||
|
task_ids: list[int] = []
|
||||||
|
for title in titles:
|
||||||
|
if title:
|
||||||
|
task = Task(
|
||||||
|
title=title,
|
||||||
|
is_mit=True,
|
||||||
|
state=TaskState.LATER,
|
||||||
|
certainty=TaskCertainty.SOFT,
|
||||||
|
)
|
||||||
|
db.add(task)
|
||||||
|
db.commit()
|
||||||
|
db.refresh(task)
|
||||||
|
task_ids.append(task.id)
|
||||||
|
return task_ids
|
||||||
|
|
||||||
|
|
||||||
|
def _create_other_tasks(db: Session, other_tasks: str):
|
||||||
|
"""Create non-MIT tasks from newline-separated text."""
|
||||||
|
for line in other_tasks.split("\n"):
|
||||||
|
line = line.strip()
|
||||||
|
if line:
|
||||||
|
task = Task(
|
||||||
|
title=line,
|
||||||
|
state=TaskState.LATER,
|
||||||
|
certainty=TaskCertainty.FUZZY,
|
||||||
|
)
|
||||||
|
db.add(task)
|
||||||
|
|
||||||
|
|
||||||
|
def _seed_now_next(db: Session):
|
||||||
|
"""Set initial NOW/NEXT states when both slots are empty."""
|
||||||
|
if get_now_task(db) or get_next_task(db):
|
||||||
|
return
|
||||||
|
later_tasks = (
|
||||||
|
db.query(Task)
|
||||||
|
.filter(Task.state == TaskState.LATER)
|
||||||
|
.order_by(Task.is_mit.desc(), Task.sort_order)
|
||||||
|
.all()
|
||||||
|
)
|
||||||
|
if later_tasks:
|
||||||
|
later_tasks[0].state = TaskState.NOW
|
||||||
|
db.add(later_tasks[0])
|
||||||
|
db.flush()
|
||||||
|
if len(later_tasks) > 1:
|
||||||
|
later_tasks[1].state = TaskState.NEXT
|
||||||
|
db.add(later_tasks[1])
|
||||||
|
|
||||||
|
|
||||||
def promote_tasks(db: Session):
|
def promote_tasks(db: Session):
|
||||||
|
"""Enforce the NOW/NEXT/LATER state machine invariants.
|
||||||
|
|
||||||
|
- At most one NOW task (extras demoted to NEXT).
|
||||||
|
- If no NOW, promote NEXT -> NOW.
|
||||||
|
- If no NEXT, promote highest-priority LATER -> NEXT.
|
||||||
|
"""
|
||||||
# Ensure only one NOW task exists. If multiple, demote extras to NEXT.
|
# Ensure only one NOW task exists. If multiple, demote extras to NEXT.
|
||||||
now_tasks = db.query(Task).filter(Task.state == TaskState.NOW).all()
|
now_tasks = db.query(Task).filter(Task.state == TaskState.NOW).all()
|
||||||
if len(now_tasks) > 1:
|
if len(now_tasks) > 1:
|
||||||
@@ -74,6 +133,7 @@ def promote_tasks(db: Session):
|
|||||||
# Endpoints
|
# Endpoints
|
||||||
@router.get("/calm", response_class=HTMLResponse)
|
@router.get("/calm", response_class=HTMLResponse)
|
||||||
async def get_calm_view(request: Request, db: Session = Depends(get_db)):
|
async def get_calm_view(request: Request, db: Session = Depends(get_db)):
|
||||||
|
"""Render the main CALM dashboard with NOW/NEXT/LATER counts."""
|
||||||
now_task = get_now_task(db)
|
now_task = get_now_task(db)
|
||||||
next_task = get_next_task(db)
|
next_task = get_next_task(db)
|
||||||
later_tasks_count = len(get_later_tasks(db))
|
later_tasks_count = len(get_later_tasks(db))
|
||||||
@@ -90,6 +150,7 @@ async def get_calm_view(request: Request, db: Session = Depends(get_db)):
|
|||||||
|
|
||||||
@router.get("/calm/ritual/morning", response_class=HTMLResponse)
|
@router.get("/calm/ritual/morning", response_class=HTMLResponse)
|
||||||
async def get_morning_ritual_form(request: Request):
|
async def get_morning_ritual_form(request: Request):
|
||||||
|
"""Render the morning ritual intake form."""
|
||||||
return templates.TemplateResponse(request, "calm/morning_ritual_form.html", {})
|
return templates.TemplateResponse(request, "calm/morning_ritual_form.html", {})
|
||||||
|
|
||||||
|
|
||||||
@@ -102,63 +163,20 @@ async def post_morning_ritual(
|
|||||||
mit3_title: str = Form(None),
|
mit3_title: str = Form(None),
|
||||||
other_tasks: str = Form(""),
|
other_tasks: str = Form(""),
|
||||||
):
|
):
|
||||||
# Create Journal Entry
|
"""Process morning ritual: create MITs, other tasks, and set initial states."""
|
||||||
mit_task_ids = []
|
|
||||||
journal_entry = JournalEntry(entry_date=date.today())
|
journal_entry = JournalEntry(entry_date=date.today())
|
||||||
db.add(journal_entry)
|
db.add(journal_entry)
|
||||||
db.commit()
|
db.commit()
|
||||||
db.refresh(journal_entry)
|
db.refresh(journal_entry)
|
||||||
|
|
||||||
# Create MIT tasks
|
journal_entry.mit_task_ids = _create_mit_tasks(db, [mit1_title, mit2_title, mit3_title])
|
||||||
for mit_title in [mit1_title, mit2_title, mit3_title]:
|
|
||||||
if mit_title:
|
|
||||||
task = Task(
|
|
||||||
title=mit_title,
|
|
||||||
is_mit=True,
|
|
||||||
state=TaskState.LATER, # Initially LATER, will be promoted
|
|
||||||
certainty=TaskCertainty.SOFT,
|
|
||||||
)
|
|
||||||
db.add(task)
|
|
||||||
db.commit()
|
|
||||||
db.refresh(task)
|
|
||||||
mit_task_ids.append(task.id)
|
|
||||||
|
|
||||||
journal_entry.mit_task_ids = mit_task_ids
|
|
||||||
db.add(journal_entry)
|
db.add(journal_entry)
|
||||||
|
|
||||||
# Create other tasks
|
_create_other_tasks(db, other_tasks)
|
||||||
for task_title in other_tasks.split("\n"):
|
|
||||||
task_title = task_title.strip()
|
|
||||||
if task_title:
|
|
||||||
task = Task(
|
|
||||||
title=task_title,
|
|
||||||
state=TaskState.LATER,
|
|
||||||
certainty=TaskCertainty.FUZZY,
|
|
||||||
)
|
|
||||||
db.add(task)
|
|
||||||
|
|
||||||
db.commit()
|
db.commit()
|
||||||
|
|
||||||
# Set initial NOW/NEXT states
|
_seed_now_next(db)
|
||||||
# Set initial NOW/NEXT states after all tasks are created
|
db.commit()
|
||||||
if not get_now_task(db) and not get_next_task(db):
|
|
||||||
later_tasks = (
|
|
||||||
db.query(Task)
|
|
||||||
.filter(Task.state == TaskState.LATER)
|
|
||||||
.order_by(Task.is_mit.desc(), Task.sort_order)
|
|
||||||
.all()
|
|
||||||
)
|
|
||||||
if later_tasks:
|
|
||||||
# Set the highest priority LATER task to NOW
|
|
||||||
later_tasks[0].state = TaskState.NOW
|
|
||||||
db.add(later_tasks[0])
|
|
||||||
db.flush() # Flush to make the change visible for the next query
|
|
||||||
|
|
||||||
# Set the next highest priority LATER task to NEXT
|
|
||||||
if len(later_tasks) > 1:
|
|
||||||
later_tasks[1].state = TaskState.NEXT
|
|
||||||
db.add(later_tasks[1])
|
|
||||||
db.commit() # Commit changes after initial NOW/NEXT setup
|
|
||||||
|
|
||||||
return templates.TemplateResponse(
|
return templates.TemplateResponse(
|
||||||
request,
|
request,
|
||||||
@@ -173,6 +191,7 @@ async def post_morning_ritual(
|
|||||||
|
|
||||||
@router.get("/calm/ritual/evening", response_class=HTMLResponse)
|
@router.get("/calm/ritual/evening", response_class=HTMLResponse)
|
||||||
async def get_evening_ritual_form(request: Request, db: Session = Depends(get_db)):
|
async def get_evening_ritual_form(request: Request, db: Session = Depends(get_db)):
|
||||||
|
"""Render the evening ritual form for today's journal entry."""
|
||||||
journal_entry = db.query(JournalEntry).filter(JournalEntry.entry_date == date.today()).first()
|
journal_entry = db.query(JournalEntry).filter(JournalEntry.entry_date == date.today()).first()
|
||||||
if not journal_entry:
|
if not journal_entry:
|
||||||
raise HTTPException(status_code=404, detail="No journal entry for today")
|
raise HTTPException(status_code=404, detail="No journal entry for today")
|
||||||
@@ -189,6 +208,7 @@ async def post_evening_ritual(
|
|||||||
gratitude: str = Form(None),
|
gratitude: str = Form(None),
|
||||||
energy_level: int = Form(None),
|
energy_level: int = Form(None),
|
||||||
):
|
):
|
||||||
|
"""Process evening ritual: save reflection/gratitude, archive active tasks."""
|
||||||
journal_entry = db.query(JournalEntry).filter(JournalEntry.entry_date == date.today()).first()
|
journal_entry = db.query(JournalEntry).filter(JournalEntry.entry_date == date.today()).first()
|
||||||
if not journal_entry:
|
if not journal_entry:
|
||||||
raise HTTPException(status_code=404, detail="No journal entry for today")
|
raise HTTPException(status_code=404, detail="No journal entry for today")
|
||||||
@@ -206,7 +226,7 @@ async def post_evening_ritual(
|
|||||||
)
|
)
|
||||||
for task in active_tasks:
|
for task in active_tasks:
|
||||||
task.state = TaskState.DEFERRED # Or DONE, depending on desired archiving logic
|
task.state = TaskState.DEFERRED # Or DONE, depending on desired archiving logic
|
||||||
task.deferred_at = datetime.utcnow()
|
task.deferred_at = datetime.now(UTC)
|
||||||
db.add(task)
|
db.add(task)
|
||||||
|
|
||||||
db.commit()
|
db.commit()
|
||||||
@@ -223,6 +243,7 @@ async def create_new_task(
|
|||||||
is_mit: bool = Form(False),
|
is_mit: bool = Form(False),
|
||||||
certainty: TaskCertainty = Form(TaskCertainty.SOFT),
|
certainty: TaskCertainty = Form(TaskCertainty.SOFT),
|
||||||
):
|
):
|
||||||
|
"""Create a new task in LATER state and return updated count."""
|
||||||
task = Task(
|
task = Task(
|
||||||
title=title,
|
title=title,
|
||||||
description=description,
|
description=description,
|
||||||
@@ -247,6 +268,7 @@ async def start_task(
|
|||||||
task_id: int,
|
task_id: int,
|
||||||
db: Session = Depends(get_db),
|
db: Session = Depends(get_db),
|
||||||
):
|
):
|
||||||
|
"""Move a task to NOW state, demoting the current NOW to NEXT."""
|
||||||
current_now_task = get_now_task(db)
|
current_now_task = get_now_task(db)
|
||||||
if current_now_task and current_now_task.id != task_id:
|
if current_now_task and current_now_task.id != task_id:
|
||||||
current_now_task.state = TaskState.NEXT # Demote current NOW to NEXT
|
current_now_task.state = TaskState.NEXT # Demote current NOW to NEXT
|
||||||
@@ -257,7 +279,7 @@ async def start_task(
|
|||||||
raise HTTPException(status_code=404, detail="Task not found")
|
raise HTTPException(status_code=404, detail="Task not found")
|
||||||
|
|
||||||
task.state = TaskState.NOW
|
task.state = TaskState.NOW
|
||||||
task.started_at = datetime.utcnow()
|
task.started_at = datetime.now(UTC)
|
||||||
db.add(task)
|
db.add(task)
|
||||||
db.commit()
|
db.commit()
|
||||||
|
|
||||||
@@ -281,12 +303,13 @@ async def complete_task(
|
|||||||
task_id: int,
|
task_id: int,
|
||||||
db: Session = Depends(get_db),
|
db: Session = Depends(get_db),
|
||||||
):
|
):
|
||||||
|
"""Mark a task as DONE and trigger state promotion."""
|
||||||
task = db.query(Task).filter(Task.id == task_id).first()
|
task = db.query(Task).filter(Task.id == task_id).first()
|
||||||
if not task:
|
if not task:
|
||||||
raise HTTPException(status_code=404, detail="Task not found")
|
raise HTTPException(status_code=404, detail="Task not found")
|
||||||
|
|
||||||
task.state = TaskState.DONE
|
task.state = TaskState.DONE
|
||||||
task.completed_at = datetime.utcnow()
|
task.completed_at = datetime.now(UTC)
|
||||||
db.add(task)
|
db.add(task)
|
||||||
db.commit()
|
db.commit()
|
||||||
|
|
||||||
@@ -309,12 +332,13 @@ async def defer_task(
|
|||||||
task_id: int,
|
task_id: int,
|
||||||
db: Session = Depends(get_db),
|
db: Session = Depends(get_db),
|
||||||
):
|
):
|
||||||
|
"""Defer a task and trigger state promotion."""
|
||||||
task = db.query(Task).filter(Task.id == task_id).first()
|
task = db.query(Task).filter(Task.id == task_id).first()
|
||||||
if not task:
|
if not task:
|
||||||
raise HTTPException(status_code=404, detail="Task not found")
|
raise HTTPException(status_code=404, detail="Task not found")
|
||||||
|
|
||||||
task.state = TaskState.DEFERRED
|
task.state = TaskState.DEFERRED
|
||||||
task.deferred_at = datetime.utcnow()
|
task.deferred_at = datetime.now(UTC)
|
||||||
db.add(task)
|
db.add(task)
|
||||||
db.commit()
|
db.commit()
|
||||||
|
|
||||||
@@ -333,6 +357,7 @@ async def defer_task(
|
|||||||
|
|
||||||
@router.get("/calm/partials/later_tasks_list", response_class=HTMLResponse)
|
@router.get("/calm/partials/later_tasks_list", response_class=HTMLResponse)
|
||||||
async def get_later_tasks_list(request: Request, db: Session = Depends(get_db)):
|
async def get_later_tasks_list(request: Request, db: Session = Depends(get_db)):
|
||||||
|
"""Render the expandable list of LATER tasks."""
|
||||||
later_tasks = get_later_tasks(db)
|
later_tasks = get_later_tasks(db)
|
||||||
return templates.TemplateResponse(
|
return templates.TemplateResponse(
|
||||||
"calm/partials/later_tasks_list.html",
|
"calm/partials/later_tasks_list.html",
|
||||||
@@ -348,6 +373,7 @@ async def reorder_tasks(
|
|||||||
later_task_ids: str = Form(""),
|
later_task_ids: str = Form(""),
|
||||||
next_task_id: int | None = Form(None),
|
next_task_id: int | None = Form(None),
|
||||||
):
|
):
|
||||||
|
"""Reorder LATER tasks and optionally promote one to NEXT."""
|
||||||
# Reorder LATER tasks
|
# Reorder LATER tasks
|
||||||
if later_task_ids:
|
if later_task_ids:
|
||||||
ids_in_order = [int(x.strip()) for x in later_task_ids.split(",") if x.strip()]
|
ids_in_order = [int(x.strip()) for x in later_task_ids.split(",") if x.strip()]
|
||||||
|
|||||||
@@ -16,52 +16,11 @@ router = APIRouter(tags=["system"])
|
|||||||
|
|
||||||
@router.get("/lightning/ledger", response_class=HTMLResponse)
|
@router.get("/lightning/ledger", response_class=HTMLResponse)
|
||||||
async def lightning_ledger(request: Request):
|
async def lightning_ledger(request: Request):
|
||||||
"""Ledger and balance page."""
|
"""Ledger and balance page backed by the in-memory Lightning ledger."""
|
||||||
# Mock data for now, as this seems to be a UI-first feature
|
from lightning.ledger import get_balance, get_transactions
|
||||||
balance = {
|
|
||||||
"available_sats": 1337,
|
|
||||||
"incoming_total_sats": 2000,
|
|
||||||
"outgoing_total_sats": 663,
|
|
||||||
"fees_paid_sats": 5,
|
|
||||||
"net_sats": 1337,
|
|
||||||
"pending_incoming_sats": 0,
|
|
||||||
"pending_outgoing_sats": 0,
|
|
||||||
}
|
|
||||||
|
|
||||||
# Mock transactions
|
balance = get_balance()
|
||||||
from collections import namedtuple
|
transactions = get_transactions()
|
||||||
from enum import Enum
|
|
||||||
|
|
||||||
class TxType(Enum):
|
|
||||||
incoming = "incoming"
|
|
||||||
outgoing = "outgoing"
|
|
||||||
|
|
||||||
class TxStatus(Enum):
|
|
||||||
completed = "completed"
|
|
||||||
pending = "pending"
|
|
||||||
|
|
||||||
Tx = namedtuple(
|
|
||||||
"Tx", ["tx_type", "status", "amount_sats", "payment_hash", "memo", "created_at"]
|
|
||||||
)
|
|
||||||
|
|
||||||
transactions = [
|
|
||||||
Tx(
|
|
||||||
TxType.outgoing,
|
|
||||||
TxStatus.completed,
|
|
||||||
50,
|
|
||||||
"hash1",
|
|
||||||
"Model inference",
|
|
||||||
"2026-03-04 10:00:00",
|
|
||||||
),
|
|
||||||
Tx(
|
|
||||||
TxType.incoming,
|
|
||||||
TxStatus.completed,
|
|
||||||
1000,
|
|
||||||
"hash2",
|
|
||||||
"Manual deposit",
|
|
||||||
"2026-03-03 15:00:00",
|
|
||||||
),
|
|
||||||
]
|
|
||||||
|
|
||||||
return templates.TemplateResponse(
|
return templates.TemplateResponse(
|
||||||
request,
|
request,
|
||||||
@@ -70,7 +29,7 @@ async def lightning_ledger(request: Request):
|
|||||||
"balance": balance,
|
"balance": balance,
|
||||||
"transactions": transactions,
|
"transactions": transactions,
|
||||||
"tx_types": ["incoming", "outgoing"],
|
"tx_types": ["incoming", "outgoing"],
|
||||||
"tx_statuses": ["completed", "pending"],
|
"tx_statuses": ["pending", "settled", "failed", "expired"],
|
||||||
"filter_type": None,
|
"filter_type": None,
|
||||||
"filter_status": None,
|
"filter_status": None,
|
||||||
"stats": {},
|
"stats": {},
|
||||||
|
|||||||
@@ -5,7 +5,7 @@ import sqlite3
|
|||||||
import uuid
|
import uuid
|
||||||
from collections.abc import Generator
|
from collections.abc import Generator
|
||||||
from contextlib import closing, contextmanager
|
from contextlib import closing, contextmanager
|
||||||
from datetime import datetime
|
from datetime import UTC, datetime
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
|
||||||
from fastapi import APIRouter, Form, HTTPException, Request
|
from fastapi import APIRouter, Form, HTTPException, Request
|
||||||
@@ -219,7 +219,7 @@ async def create_task_form(
|
|||||||
raise HTTPException(status_code=400, detail="Task title cannot be empty")
|
raise HTTPException(status_code=400, detail="Task title cannot be empty")
|
||||||
|
|
||||||
task_id = str(uuid.uuid4())
|
task_id = str(uuid.uuid4())
|
||||||
now = datetime.utcnow().isoformat()
|
now = datetime.now(UTC).isoformat()
|
||||||
priority = priority if priority in VALID_PRIORITIES else "normal"
|
priority = priority if priority in VALID_PRIORITIES else "normal"
|
||||||
|
|
||||||
with _get_db() as db:
|
with _get_db() as db:
|
||||||
@@ -287,7 +287,7 @@ async def modify_task(
|
|||||||
async def _set_status(request: Request, task_id: str, new_status: str):
|
async def _set_status(request: Request, task_id: str, new_status: str):
|
||||||
"""Helper to update status and return refreshed task card."""
|
"""Helper to update status and return refreshed task card."""
|
||||||
completed_at = (
|
completed_at = (
|
||||||
datetime.utcnow().isoformat() if new_status in ("completed", "vetoed", "failed") else None
|
datetime.now(UTC).isoformat() if new_status in ("completed", "vetoed", "failed") else None
|
||||||
)
|
)
|
||||||
with _get_db() as db:
|
with _get_db() as db:
|
||||||
db.execute(
|
db.execute(
|
||||||
@@ -316,7 +316,7 @@ async def api_create_task(request: Request):
|
|||||||
raise HTTPException(422, "title is required")
|
raise HTTPException(422, "title is required")
|
||||||
|
|
||||||
task_id = str(uuid.uuid4())
|
task_id = str(uuid.uuid4())
|
||||||
now = datetime.utcnow().isoformat()
|
now = datetime.now(UTC).isoformat()
|
||||||
priority = body.get("priority", "normal")
|
priority = body.get("priority", "normal")
|
||||||
if priority not in VALID_PRIORITIES:
|
if priority not in VALID_PRIORITIES:
|
||||||
priority = "normal"
|
priority = "normal"
|
||||||
@@ -358,7 +358,7 @@ async def api_update_status(task_id: str, request: Request):
|
|||||||
raise HTTPException(422, f"Invalid status. Must be one of: {VALID_STATUSES}")
|
raise HTTPException(422, f"Invalid status. Must be one of: {VALID_STATUSES}")
|
||||||
|
|
||||||
completed_at = (
|
completed_at = (
|
||||||
datetime.utcnow().isoformat() if new_status in ("completed", "vetoed", "failed") else None
|
datetime.now(UTC).isoformat() if new_status in ("completed", "vetoed", "failed") else None
|
||||||
)
|
)
|
||||||
with _get_db() as db:
|
with _get_db() as db:
|
||||||
db.execute(
|
db.execute(
|
||||||
|
|||||||
108
src/dashboard/routes/tower.py
Normal file
108
src/dashboard/routes/tower.py
Normal file
@@ -0,0 +1,108 @@
|
|||||||
|
"""Tower dashboard — real-time Spark visualization via WebSocket.
|
||||||
|
|
||||||
|
GET /tower — HTML Tower dashboard (Thinking / Predicting / Advising)
|
||||||
|
WS /tower/ws — WebSocket stream of Spark engine state updates
|
||||||
|
"""
|
||||||
|
|
||||||
|
import asyncio
|
||||||
|
import json
|
||||||
|
import logging
|
||||||
|
|
||||||
|
from fastapi import APIRouter, Request, WebSocket
|
||||||
|
from fastapi.responses import HTMLResponse
|
||||||
|
|
||||||
|
from dashboard.templating import templates
|
||||||
|
from spark.engine import spark_engine
|
||||||
|
|
||||||
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
router = APIRouter(prefix="/tower", tags=["tower"])
|
||||||
|
|
||||||
|
_PUSH_INTERVAL = 5 # seconds between state broadcasts
|
||||||
|
|
||||||
|
|
||||||
|
def _spark_snapshot() -> dict:
|
||||||
|
"""Build a JSON-serialisable snapshot of Spark state."""
|
||||||
|
status = spark_engine.status()
|
||||||
|
|
||||||
|
timeline = spark_engine.get_timeline(limit=10)
|
||||||
|
events = []
|
||||||
|
for ev in timeline:
|
||||||
|
entry = {
|
||||||
|
"event_type": ev.event_type,
|
||||||
|
"description": ev.description,
|
||||||
|
"importance": ev.importance,
|
||||||
|
"created_at": ev.created_at,
|
||||||
|
}
|
||||||
|
if ev.agent_id:
|
||||||
|
entry["agent_id"] = ev.agent_id[:8]
|
||||||
|
if ev.task_id:
|
||||||
|
entry["task_id"] = ev.task_id[:8]
|
||||||
|
try:
|
||||||
|
entry["data"] = json.loads(ev.data)
|
||||||
|
except (json.JSONDecodeError, TypeError):
|
||||||
|
entry["data"] = {}
|
||||||
|
events.append(entry)
|
||||||
|
|
||||||
|
predictions = spark_engine.get_predictions(limit=5)
|
||||||
|
preds = []
|
||||||
|
for p in predictions:
|
||||||
|
pred = {
|
||||||
|
"task_id": p.task_id[:8] if p.task_id else "?",
|
||||||
|
"accuracy": p.accuracy,
|
||||||
|
"evaluated": p.evaluated_at is not None,
|
||||||
|
"created_at": p.created_at,
|
||||||
|
}
|
||||||
|
try:
|
||||||
|
pred["predicted"] = json.loads(p.predicted_value)
|
||||||
|
except (json.JSONDecodeError, TypeError):
|
||||||
|
pred["predicted"] = {}
|
||||||
|
preds.append(pred)
|
||||||
|
|
||||||
|
advisories = spark_engine.get_advisories()
|
||||||
|
advs = [
|
||||||
|
{
|
||||||
|
"category": a.category,
|
||||||
|
"priority": a.priority,
|
||||||
|
"title": a.title,
|
||||||
|
"detail": a.detail,
|
||||||
|
"suggested_action": a.suggested_action,
|
||||||
|
}
|
||||||
|
for a in advisories
|
||||||
|
]
|
||||||
|
|
||||||
|
return {
|
||||||
|
"type": "spark_state",
|
||||||
|
"status": status,
|
||||||
|
"events": events,
|
||||||
|
"predictions": preds,
|
||||||
|
"advisories": advs,
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@router.get("", response_class=HTMLResponse)
|
||||||
|
async def tower_ui(request: Request):
|
||||||
|
"""Render the Tower dashboard page."""
|
||||||
|
snapshot = _spark_snapshot()
|
||||||
|
return templates.TemplateResponse(
|
||||||
|
request,
|
||||||
|
"tower.html",
|
||||||
|
{"snapshot": snapshot},
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@router.websocket("/ws")
|
||||||
|
async def tower_ws(websocket: WebSocket) -> None:
|
||||||
|
"""Stream Spark state snapshots to the Tower dashboard."""
|
||||||
|
await websocket.accept()
|
||||||
|
logger.info("Tower WS connected")
|
||||||
|
|
||||||
|
try:
|
||||||
|
# Send initial snapshot
|
||||||
|
await websocket.send_text(json.dumps(_spark_snapshot()))
|
||||||
|
|
||||||
|
while True:
|
||||||
|
await asyncio.sleep(_PUSH_INTERVAL)
|
||||||
|
await websocket.send_text(json.dumps(_spark_snapshot()))
|
||||||
|
except Exception:
|
||||||
|
logger.debug("Tower WS disconnected")
|
||||||
@@ -5,7 +5,7 @@ import sqlite3
|
|||||||
import uuid
|
import uuid
|
||||||
from collections.abc import Generator
|
from collections.abc import Generator
|
||||||
from contextlib import closing, contextmanager
|
from contextlib import closing, contextmanager
|
||||||
from datetime import datetime
|
from datetime import UTC, datetime
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
|
||||||
from fastapi import APIRouter, Form, HTTPException, Request
|
from fastapi import APIRouter, Form, HTTPException, Request
|
||||||
@@ -144,7 +144,7 @@ async def submit_work_order(
|
|||||||
related_files: str = Form(""),
|
related_files: str = Form(""),
|
||||||
):
|
):
|
||||||
wo_id = str(uuid.uuid4())
|
wo_id = str(uuid.uuid4())
|
||||||
now = datetime.utcnow().isoformat()
|
now = datetime.now(UTC).isoformat()
|
||||||
priority = priority if priority in PRIORITIES else "medium"
|
priority = priority if priority in PRIORITIES else "medium"
|
||||||
category = category if category in CATEGORIES else "suggestion"
|
category = category if category in CATEGORIES else "suggestion"
|
||||||
|
|
||||||
@@ -211,7 +211,7 @@ async def active_partial(request: Request):
|
|||||||
|
|
||||||
async def _update_status(request: Request, wo_id: str, new_status: str, **extra):
|
async def _update_status(request: Request, wo_id: str, new_status: str, **extra):
|
||||||
completed_at = (
|
completed_at = (
|
||||||
datetime.utcnow().isoformat() if new_status in ("completed", "rejected") else None
|
datetime.now(UTC).isoformat() if new_status in ("completed", "rejected") else None
|
||||||
)
|
)
|
||||||
with _get_db() as db:
|
with _get_db() as db:
|
||||||
sets = ["status=?", "completed_at=COALESCE(?, completed_at)"]
|
sets = ["status=?", "completed_at=COALESCE(?, completed_at)"]
|
||||||
|
|||||||
@@ -138,6 +138,47 @@
|
|||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
<!-- Spark Intelligence -->
|
||||||
|
{% from "macros.html" import panel %}
|
||||||
|
<div class="mc-card-spaced">
|
||||||
|
<div class="card">
|
||||||
|
<div class="card-header">
|
||||||
|
<h2 class="card-title">Spark Intelligence</h2>
|
||||||
|
<div>
|
||||||
|
<span class="badge" id="spark-status-badge">Loading...</span>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<div class="grid grid-3">
|
||||||
|
<div class="stat">
|
||||||
|
<div class="stat-value" id="spark-events">-</div>
|
||||||
|
<div class="stat-label">Events</div>
|
||||||
|
</div>
|
||||||
|
<div class="stat">
|
||||||
|
<div class="stat-value" id="spark-memories">-</div>
|
||||||
|
<div class="stat-label">Memories</div>
|
||||||
|
</div>
|
||||||
|
<div class="stat">
|
||||||
|
<div class="stat-value" id="spark-predictions">-</div>
|
||||||
|
<div class="stat-label">Predictions</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<div class="grid grid-2 mc-section-gap">
|
||||||
|
{% call panel("SPARK TIMELINE", id="spark-timeline-panel",
|
||||||
|
hx_get="/spark/timeline",
|
||||||
|
hx_trigger="load, every 10s") %}
|
||||||
|
<div class="spark-timeline-scroll">
|
||||||
|
<p class="chat-history-placeholder">Loading timeline...</p>
|
||||||
|
</div>
|
||||||
|
{% endcall %}
|
||||||
|
{% call panel("SPARK INSIGHTS", id="spark-insights-panel",
|
||||||
|
hx_get="/spark/insights",
|
||||||
|
hx_trigger="load, every 30s") %}
|
||||||
|
<p class="chat-history-placeholder">Loading insights...</p>
|
||||||
|
{% endcall %}
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
<!-- Chat History -->
|
<!-- Chat History -->
|
||||||
<div class="card mc-card-spaced">
|
<div class="card mc-card-spaced">
|
||||||
<div class="card-header">
|
<div class="card-header">
|
||||||
@@ -428,7 +469,34 @@ async function loadGrokStats() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Load Spark status
|
||||||
|
async function loadSparkStatus() {
|
||||||
|
try {
|
||||||
|
var response = await fetch('/spark');
|
||||||
|
var data = await response.json();
|
||||||
|
var st = data.status || {};
|
||||||
|
|
||||||
|
document.getElementById('spark-events').textContent = st.total_events || 0;
|
||||||
|
document.getElementById('spark-memories').textContent = st.total_memories || 0;
|
||||||
|
document.getElementById('spark-predictions').textContent = st.total_predictions || 0;
|
||||||
|
|
||||||
|
var badge = document.getElementById('spark-status-badge');
|
||||||
|
if (st.total_events > 0) {
|
||||||
|
badge.textContent = 'Active';
|
||||||
|
badge.className = 'badge badge-success';
|
||||||
|
} else {
|
||||||
|
badge.textContent = 'Idle';
|
||||||
|
badge.className = 'badge badge-warning';
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
var badge = document.getElementById('spark-status-badge');
|
||||||
|
badge.textContent = 'Offline';
|
||||||
|
badge.className = 'badge badge-danger';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Initial load
|
// Initial load
|
||||||
|
loadSparkStatus();
|
||||||
loadSovereignty();
|
loadSovereignty();
|
||||||
loadHealth();
|
loadHealth();
|
||||||
loadSwarmStats();
|
loadSwarmStats();
|
||||||
@@ -442,5 +510,6 @@ setInterval(loadHealth, 10000);
|
|||||||
setInterval(loadSwarmStats, 5000);
|
setInterval(loadSwarmStats, 5000);
|
||||||
setInterval(updateHeartbeat, 5000);
|
setInterval(updateHeartbeat, 5000);
|
||||||
setInterval(loadGrokStats, 10000);
|
setInterval(loadGrokStats, 10000);
|
||||||
|
setInterval(loadSparkStatus, 15000);
|
||||||
</script>
|
</script>
|
||||||
{% endblock %}
|
{% endblock %}
|
||||||
|
|||||||
180
src/dashboard/templates/tower.html
Normal file
180
src/dashboard/templates/tower.html
Normal file
@@ -0,0 +1,180 @@
|
|||||||
|
{% extends "base.html" %}
|
||||||
|
|
||||||
|
{% block title %}Timmy Time — Tower{% endblock %}
|
||||||
|
|
||||||
|
{% block extra_styles %}{% endblock %}
|
||||||
|
|
||||||
|
{% block content %}
|
||||||
|
<div class="container-fluid tower-container py-3">
|
||||||
|
|
||||||
|
<div class="tower-header">
|
||||||
|
<div class="tower-title">TOWER</div>
|
||||||
|
<div class="tower-subtitle">
|
||||||
|
Real-time Spark visualization —
|
||||||
|
<span id="tower-conn" class="tower-conn-badge tower-conn-connecting">CONNECTING</span>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="row g-3">
|
||||||
|
|
||||||
|
<!-- Left: THINKING (events) -->
|
||||||
|
<div class="col-12 col-lg-4 d-flex flex-column gap-3">
|
||||||
|
<div class="card mc-panel tower-phase-card">
|
||||||
|
<div class="card-header mc-panel-header tower-phase-thinking">// THINKING</div>
|
||||||
|
<div class="card-body p-3 tower-scroll" id="tower-events">
|
||||||
|
<div class="tower-empty">Waiting for Spark data…</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<!-- Middle: PREDICTING (EIDOS) -->
|
||||||
|
<div class="col-12 col-lg-4 d-flex flex-column gap-3">
|
||||||
|
<div class="card mc-panel tower-phase-card">
|
||||||
|
<div class="card-header mc-panel-header tower-phase-predicting">// PREDICTING</div>
|
||||||
|
<div class="card-body p-3" id="tower-predictions">
|
||||||
|
<div class="tower-empty">Waiting for Spark data…</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<div class="card mc-panel">
|
||||||
|
<div class="card-header mc-panel-header">// EIDOS STATS</div>
|
||||||
|
<div class="card-body p-3">
|
||||||
|
<div class="tower-stat-grid" id="tower-stats">
|
||||||
|
<div class="tower-stat"><span class="tower-stat-label">EVENTS</span><span class="tower-stat-value" id="ts-events">0</span></div>
|
||||||
|
<div class="tower-stat"><span class="tower-stat-label">MEMORIES</span><span class="tower-stat-value" id="ts-memories">0</span></div>
|
||||||
|
<div class="tower-stat"><span class="tower-stat-label">PREDICTIONS</span><span class="tower-stat-value" id="ts-preds">0</span></div>
|
||||||
|
<div class="tower-stat"><span class="tower-stat-label">ACCURACY</span><span class="tower-stat-value" id="ts-accuracy">—</span></div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<!-- Right: ADVISING -->
|
||||||
|
<div class="col-12 col-lg-4 d-flex flex-column gap-3">
|
||||||
|
<div class="card mc-panel tower-phase-card">
|
||||||
|
<div class="card-header mc-panel-header tower-phase-advising">// ADVISING</div>
|
||||||
|
<div class="card-body p-3 tower-scroll" id="tower-advisories">
|
||||||
|
<div class="tower-empty">Waiting for Spark data…</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<script>
|
||||||
|
(function() {
|
||||||
|
var ws = null;
|
||||||
|
var badge = document.getElementById('tower-conn');
|
||||||
|
|
||||||
|
function setConn(state) {
|
||||||
|
badge.textContent = state.toUpperCase();
|
||||||
|
badge.className = 'tower-conn-badge tower-conn-' + state;
|
||||||
|
}
|
||||||
|
|
||||||
|
function esc(s) { var d = document.createElement('div'); d.textContent = s; return d.innerHTML; }
|
||||||
|
|
||||||
|
function renderEvents(events) {
|
||||||
|
var el = document.getElementById('tower-events');
|
||||||
|
if (!events || !events.length) { el.innerHTML = '<div class="tower-empty">No events captured yet.</div>'; return; }
|
||||||
|
var html = '';
|
||||||
|
for (var i = 0; i < events.length; i++) {
|
||||||
|
var ev = events[i];
|
||||||
|
var dots = ev.importance >= 0.8 ? '\u25cf\u25cf\u25cf' : ev.importance >= 0.5 ? '\u25cf\u25cf' : '\u25cf';
|
||||||
|
html += '<div class="tower-event tower-etype-' + esc(ev.event_type) + '">'
|
||||||
|
+ '<div class="tower-ev-head">'
|
||||||
|
+ '<span class="tower-ev-badge">' + esc(ev.event_type.replace(/_/g, ' ').toUpperCase()) + '</span>'
|
||||||
|
+ '<span class="tower-ev-dots">' + dots + '</span>'
|
||||||
|
+ '</div>'
|
||||||
|
+ '<div class="tower-ev-desc">' + esc(ev.description) + '</div>'
|
||||||
|
+ '<div class="tower-ev-time">' + esc((ev.created_at || '').slice(0, 19)) + '</div>'
|
||||||
|
+ '</div>';
|
||||||
|
}
|
||||||
|
el.innerHTML = html;
|
||||||
|
}
|
||||||
|
|
||||||
|
function renderPredictions(preds) {
|
||||||
|
var el = document.getElementById('tower-predictions');
|
||||||
|
if (!preds || !preds.length) { el.innerHTML = '<div class="tower-empty">No predictions yet.</div>'; return; }
|
||||||
|
var html = '';
|
||||||
|
for (var i = 0; i < preds.length; i++) {
|
||||||
|
var p = preds[i];
|
||||||
|
var cls = p.evaluated ? 'tower-pred-done' : 'tower-pred-pending';
|
||||||
|
var accTxt = p.accuracy != null ? Math.round(p.accuracy * 100) + '%' : 'PENDING';
|
||||||
|
var accCls = p.accuracy != null ? (p.accuracy >= 0.7 ? 'text-success' : p.accuracy < 0.4 ? 'text-danger' : 'text-warning') : '';
|
||||||
|
html += '<div class="tower-pred ' + cls + '">'
|
||||||
|
+ '<div class="tower-pred-head">'
|
||||||
|
+ '<span class="tower-pred-task">' + esc(p.task_id) + '</span>'
|
||||||
|
+ '<span class="tower-pred-acc ' + accCls + '">' + accTxt + '</span>'
|
||||||
|
+ '</div>';
|
||||||
|
if (p.predicted) {
|
||||||
|
var pr = p.predicted;
|
||||||
|
html += '<div class="tower-pred-detail">';
|
||||||
|
if (pr.likely_winner) html += '<span>Winner: ' + esc(pr.likely_winner.slice(0, 8)) + '</span> ';
|
||||||
|
if (pr.success_probability != null) html += '<span>Success: ' + Math.round(pr.success_probability * 100) + '%</span> ';
|
||||||
|
html += '</div>';
|
||||||
|
}
|
||||||
|
html += '<div class="tower-ev-time">' + esc((p.created_at || '').slice(0, 19)) + '</div>'
|
||||||
|
+ '</div>';
|
||||||
|
}
|
||||||
|
el.innerHTML = html;
|
||||||
|
}
|
||||||
|
|
||||||
|
function renderAdvisories(advs) {
|
||||||
|
var el = document.getElementById('tower-advisories');
|
||||||
|
if (!advs || !advs.length) { el.innerHTML = '<div class="tower-empty">No advisories yet.</div>'; return; }
|
||||||
|
var html = '';
|
||||||
|
for (var i = 0; i < advs.length; i++) {
|
||||||
|
var a = advs[i];
|
||||||
|
var prio = a.priority >= 0.7 ? 'high' : a.priority >= 0.4 ? 'medium' : 'low';
|
||||||
|
html += '<div class="tower-advisory tower-adv-' + prio + '">'
|
||||||
|
+ '<div class="tower-adv-head">'
|
||||||
|
+ '<span class="tower-adv-cat">' + esc(a.category.replace(/_/g, ' ').toUpperCase()) + '</span>'
|
||||||
|
+ '<span class="tower-adv-prio">' + Math.round(a.priority * 100) + '%</span>'
|
||||||
|
+ '</div>'
|
||||||
|
+ '<div class="tower-adv-title">' + esc(a.title) + '</div>'
|
||||||
|
+ '<div class="tower-adv-detail">' + esc(a.detail) + '</div>'
|
||||||
|
+ '<div class="tower-adv-action">' + esc(a.suggested_action) + '</div>'
|
||||||
|
+ '</div>';
|
||||||
|
}
|
||||||
|
el.innerHTML = html;
|
||||||
|
}
|
||||||
|
|
||||||
|
function renderStats(status) {
|
||||||
|
if (!status) return;
|
||||||
|
document.getElementById('ts-events').textContent = status.events_captured || 0;
|
||||||
|
document.getElementById('ts-memories').textContent = status.memories_stored || 0;
|
||||||
|
var p = status.predictions || {};
|
||||||
|
document.getElementById('ts-preds').textContent = p.total_predictions || 0;
|
||||||
|
var acc = p.avg_accuracy;
|
||||||
|
var accEl = document.getElementById('ts-accuracy');
|
||||||
|
if (acc != null) {
|
||||||
|
accEl.textContent = Math.round(acc * 100) + '%';
|
||||||
|
accEl.className = 'tower-stat-value ' + (acc >= 0.7 ? 'text-success' : acc < 0.4 ? 'text-danger' : 'text-warning');
|
||||||
|
} else {
|
||||||
|
accEl.textContent = '\u2014';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function handleMsg(data) {
|
||||||
|
if (data.type !== 'spark_state') return;
|
||||||
|
renderEvents(data.events);
|
||||||
|
renderPredictions(data.predictions);
|
||||||
|
renderAdvisories(data.advisories);
|
||||||
|
renderStats(data.status);
|
||||||
|
}
|
||||||
|
|
||||||
|
function connect() {
|
||||||
|
var proto = location.protocol === 'https:' ? 'wss:' : 'ws:';
|
||||||
|
ws = new WebSocket(proto + '//' + location.host + '/tower/ws');
|
||||||
|
ws.onopen = function() { setConn('live'); };
|
||||||
|
ws.onclose = function() { setConn('offline'); setTimeout(connect, 3000); };
|
||||||
|
ws.onerror = function() { setConn('offline'); };
|
||||||
|
ws.onmessage = function(e) {
|
||||||
|
try { handleMsg(JSON.parse(e.data)); } catch(err) { console.error('Tower WS parse error', err); }
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
connect();
|
||||||
|
})();
|
||||||
|
</script>
|
||||||
|
{% endblock %}
|
||||||
@@ -149,6 +149,52 @@ def _log_error_event(
|
|||||||
logger.debug("Failed to log error event: %s", log_exc)
|
logger.debug("Failed to log error event: %s", log_exc)
|
||||||
|
|
||||||
|
|
||||||
|
def _build_report_description(
|
||||||
|
exc: Exception,
|
||||||
|
source: str,
|
||||||
|
context: dict | None,
|
||||||
|
error_hash: str,
|
||||||
|
tb_str: str,
|
||||||
|
affected_file: str,
|
||||||
|
affected_line: int,
|
||||||
|
git_ctx: dict,
|
||||||
|
) -> str:
|
||||||
|
"""Build the markdown description for a bug report task."""
|
||||||
|
parts = [
|
||||||
|
f"**Error:** {type(exc).__name__}: {str(exc)}",
|
||||||
|
f"**Source:** {source}",
|
||||||
|
f"**File:** {affected_file}:{affected_line}",
|
||||||
|
f"**Git:** {git_ctx.get('branch', '?')} @ {git_ctx.get('commit', '?')}",
|
||||||
|
f"**Time:** {datetime.now(UTC).isoformat()}",
|
||||||
|
f"**Hash:** {error_hash}",
|
||||||
|
]
|
||||||
|
|
||||||
|
if context:
|
||||||
|
ctx_str = ", ".join(f"{k}={v}" for k, v in context.items())
|
||||||
|
parts.append(f"**Context:** {ctx_str}")
|
||||||
|
|
||||||
|
parts.append(f"\n**Stack Trace:**\n```\n{tb_str[:2000]}\n```")
|
||||||
|
return "\n".join(parts)
|
||||||
|
|
||||||
|
|
||||||
|
def _log_bug_report_created(source: str, task_id: str, error_hash: str, title: str) -> None:
|
||||||
|
"""Log a BUG_REPORT_CREATED event (best-effort)."""
|
||||||
|
try:
|
||||||
|
from swarm.event_log import EventType, log_event
|
||||||
|
|
||||||
|
log_event(
|
||||||
|
EventType.BUG_REPORT_CREATED,
|
||||||
|
source=source,
|
||||||
|
task_id=task_id,
|
||||||
|
data={
|
||||||
|
"error_hash": error_hash,
|
||||||
|
"title": title[:100],
|
||||||
|
},
|
||||||
|
)
|
||||||
|
except Exception as exc:
|
||||||
|
logger.warning("Bug report event log error: %s", exc)
|
||||||
|
|
||||||
|
|
||||||
def _create_bug_report(
|
def _create_bug_report(
|
||||||
exc: Exception,
|
exc: Exception,
|
||||||
source: str,
|
source: str,
|
||||||
@@ -164,25 +210,20 @@ def _create_bug_report(
|
|||||||
from swarm.task_queue.models import create_task
|
from swarm.task_queue.models import create_task
|
||||||
|
|
||||||
title = f"[BUG] {type(exc).__name__}: {str(exc)[:80]}"
|
title = f"[BUG] {type(exc).__name__}: {str(exc)[:80]}"
|
||||||
|
description = _build_report_description(
|
||||||
description_parts = [
|
exc,
|
||||||
f"**Error:** {type(exc).__name__}: {str(exc)}",
|
source,
|
||||||
f"**Source:** {source}",
|
context,
|
||||||
f"**File:** {affected_file}:{affected_line}",
|
error_hash,
|
||||||
f"**Git:** {git_ctx.get('branch', '?')} @ {git_ctx.get('commit', '?')}",
|
tb_str,
|
||||||
f"**Time:** {datetime.now(UTC).isoformat()}",
|
affected_file,
|
||||||
f"**Hash:** {error_hash}",
|
affected_line,
|
||||||
]
|
git_ctx,
|
||||||
|
)
|
||||||
if context:
|
|
||||||
ctx_str = ", ".join(f"{k}={v}" for k, v in context.items())
|
|
||||||
description_parts.append(f"**Context:** {ctx_str}")
|
|
||||||
|
|
||||||
description_parts.append(f"\n**Stack Trace:**\n```\n{tb_str[:2000]}\n```")
|
|
||||||
|
|
||||||
task = create_task(
|
task = create_task(
|
||||||
title=title,
|
title=title,
|
||||||
description="\n".join(description_parts),
|
description=description,
|
||||||
assigned_to="default",
|
assigned_to="default",
|
||||||
created_by="system",
|
created_by="system",
|
||||||
priority="normal",
|
priority="normal",
|
||||||
@@ -190,24 +231,9 @@ def _create_bug_report(
|
|||||||
auto_approve=True,
|
auto_approve=True,
|
||||||
task_type="bug_report",
|
task_type="bug_report",
|
||||||
)
|
)
|
||||||
task_id = task.id
|
|
||||||
|
|
||||||
try:
|
_log_bug_report_created(source, task.id, error_hash, title)
|
||||||
from swarm.event_log import EventType, log_event
|
return task.id
|
||||||
|
|
||||||
log_event(
|
|
||||||
EventType.BUG_REPORT_CREATED,
|
|
||||||
source=source,
|
|
||||||
task_id=task_id,
|
|
||||||
data={
|
|
||||||
"error_hash": error_hash,
|
|
||||||
"title": title[:100],
|
|
||||||
},
|
|
||||||
)
|
|
||||||
except Exception as exc:
|
|
||||||
logger.warning("Bug report screenshot error: %s", exc)
|
|
||||||
|
|
||||||
return task_id
|
|
||||||
|
|
||||||
except Exception as task_exc:
|
except Exception as task_exc:
|
||||||
logger.debug("Failed to create bug report task: %s", task_exc)
|
logger.debug("Failed to create bug report task: %s", task_exc)
|
||||||
|
|||||||
@@ -64,7 +64,7 @@ class EventBus:
|
|||||||
|
|
||||||
@bus.subscribe("agent.task.*")
|
@bus.subscribe("agent.task.*")
|
||||||
async def handle_task(event: Event):
|
async def handle_task(event: Event):
|
||||||
logger.debug(f"Task event: {event.data}")
|
logger.debug("Task event: %s", event.data)
|
||||||
|
|
||||||
await bus.publish(Event(
|
await bus.publish(Event(
|
||||||
type="agent.task.assigned",
|
type="agent.task.assigned",
|
||||||
|
|||||||
@@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
from .api import router
|
from .api import router
|
||||||
from .cascade import CascadeRouter, Provider, ProviderStatus, get_router
|
from .cascade import CascadeRouter, Provider, ProviderStatus, get_router
|
||||||
|
from .history import HealthHistoryStore, get_history_store
|
||||||
|
|
||||||
__all__ = [
|
__all__ = [
|
||||||
"CascadeRouter",
|
"CascadeRouter",
|
||||||
@@ -9,4 +10,6 @@ __all__ = [
|
|||||||
"ProviderStatus",
|
"ProviderStatus",
|
||||||
"get_router",
|
"get_router",
|
||||||
"router",
|
"router",
|
||||||
|
"HealthHistoryStore",
|
||||||
|
"get_history_store",
|
||||||
]
|
]
|
||||||
|
|||||||
@@ -8,6 +8,7 @@ from fastapi import APIRouter, Depends, HTTPException
|
|||||||
from pydantic import BaseModel
|
from pydantic import BaseModel
|
||||||
|
|
||||||
from .cascade import CascadeRouter, get_router
|
from .cascade import CascadeRouter, get_router
|
||||||
|
from .history import HealthHistoryStore, get_history_store
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
router = APIRouter(prefix="/api/v1/router", tags=["router"])
|
router = APIRouter(prefix="/api/v1/router", tags=["router"])
|
||||||
@@ -199,6 +200,17 @@ async def reload_config(
|
|||||||
raise HTTPException(status_code=500, detail=f"Reload failed: {exc}") from exc
|
raise HTTPException(status_code=500, detail=f"Reload failed: {exc}") from exc
|
||||||
|
|
||||||
|
|
||||||
|
@router.get("/history")
|
||||||
|
async def get_history(
|
||||||
|
hours: int = 24,
|
||||||
|
store: Annotated[HealthHistoryStore, Depends(get_history_store)] = None,
|
||||||
|
) -> list[dict[str, Any]]:
|
||||||
|
"""Get provider health history for the last N hours."""
|
||||||
|
if store is None:
|
||||||
|
store = get_history_store()
|
||||||
|
return store.get_history(hours=hours)
|
||||||
|
|
||||||
|
|
||||||
@router.get("/config")
|
@router.get("/config")
|
||||||
async def get_config(
|
async def get_config(
|
||||||
cascade: Annotated[CascadeRouter, Depends(get_cascade_router)],
|
cascade: Annotated[CascadeRouter, Depends(get_cascade_router)],
|
||||||
|
|||||||
@@ -221,39 +221,35 @@ class CascadeRouter:
|
|||||||
raise RuntimeError("PyYAML not installed")
|
raise RuntimeError("PyYAML not installed")
|
||||||
|
|
||||||
content = self.config_path.read_text()
|
content = self.config_path.read_text()
|
||||||
# Expand environment variables
|
|
||||||
content = self._expand_env_vars(content)
|
content = self._expand_env_vars(content)
|
||||||
data = yaml.safe_load(content)
|
data = yaml.safe_load(content)
|
||||||
|
|
||||||
# Load cascade settings
|
self.config = self._parse_router_config(data)
|
||||||
|
self._load_providers(data)
|
||||||
|
|
||||||
|
except Exception as exc:
|
||||||
|
logger.error("Failed to load config: %s", exc)
|
||||||
|
|
||||||
|
def _parse_router_config(self, data: dict) -> RouterConfig:
|
||||||
|
"""Build a RouterConfig from parsed YAML data."""
|
||||||
cascade = data.get("cascade", {})
|
cascade = data.get("cascade", {})
|
||||||
|
cb = cascade.get("circuit_breaker", {})
|
||||||
# Load fallback chains
|
|
||||||
fallback_chains = data.get("fallback_chains", {})
|
|
||||||
|
|
||||||
# Load multi-modal settings
|
|
||||||
multimodal = data.get("multimodal", {})
|
multimodal = data.get("multimodal", {})
|
||||||
|
|
||||||
self.config = RouterConfig(
|
return RouterConfig(
|
||||||
timeout_seconds=cascade.get("timeout_seconds", 30),
|
timeout_seconds=cascade.get("timeout_seconds", 30),
|
||||||
max_retries_per_provider=cascade.get("max_retries_per_provider", 2),
|
max_retries_per_provider=cascade.get("max_retries_per_provider", 2),
|
||||||
retry_delay_seconds=cascade.get("retry_delay_seconds", 1),
|
retry_delay_seconds=cascade.get("retry_delay_seconds", 1),
|
||||||
circuit_breaker_failure_threshold=cascade.get("circuit_breaker", {}).get(
|
circuit_breaker_failure_threshold=cb.get("failure_threshold", 5),
|
||||||
"failure_threshold", 5
|
circuit_breaker_recovery_timeout=cb.get("recovery_timeout", 60),
|
||||||
),
|
circuit_breaker_half_open_max_calls=cb.get("half_open_max_calls", 2),
|
||||||
circuit_breaker_recovery_timeout=cascade.get("circuit_breaker", {}).get(
|
|
||||||
"recovery_timeout", 60
|
|
||||||
),
|
|
||||||
circuit_breaker_half_open_max_calls=cascade.get("circuit_breaker", {}).get(
|
|
||||||
"half_open_max_calls", 2
|
|
||||||
),
|
|
||||||
auto_pull_models=multimodal.get("auto_pull", True),
|
auto_pull_models=multimodal.get("auto_pull", True),
|
||||||
fallback_chains=fallback_chains,
|
fallback_chains=data.get("fallback_chains", {}),
|
||||||
)
|
)
|
||||||
|
|
||||||
# Load providers
|
def _load_providers(self, data: dict) -> None:
|
||||||
|
"""Load, filter, and sort providers from parsed YAML data."""
|
||||||
for p_data in data.get("providers", []):
|
for p_data in data.get("providers", []):
|
||||||
# Skip disabled providers
|
|
||||||
if not p_data.get("enabled", False):
|
if not p_data.get("enabled", False):
|
||||||
continue
|
continue
|
||||||
|
|
||||||
@@ -268,18 +264,13 @@ class CascadeRouter:
|
|||||||
models=p_data.get("models", []),
|
models=p_data.get("models", []),
|
||||||
)
|
)
|
||||||
|
|
||||||
# Check if provider is actually available
|
|
||||||
if self._check_provider_available(provider):
|
if self._check_provider_available(provider):
|
||||||
self.providers.append(provider)
|
self.providers.append(provider)
|
||||||
else:
|
else:
|
||||||
logger.warning("Provider %s not available, skipping", provider.name)
|
logger.warning("Provider %s not available, skipping", provider.name)
|
||||||
|
|
||||||
# Sort by priority
|
|
||||||
self.providers.sort(key=lambda p: p.priority)
|
self.providers.sort(key=lambda p: p.priority)
|
||||||
|
|
||||||
except Exception as exc:
|
|
||||||
logger.error("Failed to load config: %s", exc)
|
|
||||||
|
|
||||||
def _expand_env_vars(self, content: str) -> str:
|
def _expand_env_vars(self, content: str) -> str:
|
||||||
"""Expand ${VAR} syntax in YAML content.
|
"""Expand ${VAR} syntax in YAML content.
|
||||||
|
|
||||||
@@ -564,6 +555,7 @@ class CascadeRouter:
|
|||||||
messages=messages,
|
messages=messages,
|
||||||
model=model or provider.get_default_model(),
|
model=model or provider.get_default_model(),
|
||||||
temperature=temperature,
|
temperature=temperature,
|
||||||
|
max_tokens=max_tokens,
|
||||||
content_type=content_type,
|
content_type=content_type,
|
||||||
)
|
)
|
||||||
elif provider.type == "openai":
|
elif provider.type == "openai":
|
||||||
@@ -604,6 +596,7 @@ class CascadeRouter:
|
|||||||
messages: list[dict],
|
messages: list[dict],
|
||||||
model: str,
|
model: str,
|
||||||
temperature: float,
|
temperature: float,
|
||||||
|
max_tokens: int | None = None,
|
||||||
content_type: ContentType = ContentType.TEXT,
|
content_type: ContentType = ContentType.TEXT,
|
||||||
) -> dict:
|
) -> dict:
|
||||||
"""Call Ollama API with multi-modal support."""
|
"""Call Ollama API with multi-modal support."""
|
||||||
@@ -614,13 +607,15 @@ class CascadeRouter:
|
|||||||
# Transform messages for Ollama format (including images)
|
# Transform messages for Ollama format (including images)
|
||||||
transformed_messages = self._transform_messages_for_ollama(messages)
|
transformed_messages = self._transform_messages_for_ollama(messages)
|
||||||
|
|
||||||
|
options = {"temperature": temperature}
|
||||||
|
if max_tokens:
|
||||||
|
options["num_predict"] = max_tokens
|
||||||
|
|
||||||
payload = {
|
payload = {
|
||||||
"model": model,
|
"model": model,
|
||||||
"messages": transformed_messages,
|
"messages": transformed_messages,
|
||||||
"stream": False,
|
"stream": False,
|
||||||
"options": {
|
"options": options,
|
||||||
"temperature": temperature,
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
|
|
||||||
timeout = aiohttp.ClientTimeout(total=self.config.timeout_seconds)
|
timeout = aiohttp.ClientTimeout(total=self.config.timeout_seconds)
|
||||||
@@ -764,7 +759,7 @@ class CascadeRouter:
|
|||||||
|
|
||||||
client = openai.AsyncOpenAI(
|
client = openai.AsyncOpenAI(
|
||||||
api_key=provider.api_key,
|
api_key=provider.api_key,
|
||||||
base_url=provider.base_url or "https://api.x.ai/v1",
|
base_url=provider.base_url or settings.xai_base_url,
|
||||||
timeout=httpx.Timeout(300.0),
|
timeout=httpx.Timeout(300.0),
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|||||||
152
src/infrastructure/router/history.py
Normal file
152
src/infrastructure/router/history.py
Normal file
@@ -0,0 +1,152 @@
|
|||||||
|
"""Provider health history — time-series snapshots for dashboard visualization."""
|
||||||
|
|
||||||
|
import asyncio
|
||||||
|
import logging
|
||||||
|
import sqlite3
|
||||||
|
from datetime import UTC, datetime, timedelta
|
||||||
|
from pathlib import Path
|
||||||
|
|
||||||
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
_store: "HealthHistoryStore | None" = None
|
||||||
|
|
||||||
|
|
||||||
|
class HealthHistoryStore:
|
||||||
|
"""Stores timestamped provider health snapshots in SQLite."""
|
||||||
|
|
||||||
|
def __init__(self, db_path: str = "data/router_history.db") -> None:
|
||||||
|
self.db_path = db_path
|
||||||
|
if db_path != ":memory:":
|
||||||
|
Path(db_path).parent.mkdir(parents=True, exist_ok=True)
|
||||||
|
self._conn = sqlite3.connect(db_path, check_same_thread=False)
|
||||||
|
self._conn.row_factory = sqlite3.Row
|
||||||
|
self._init_schema()
|
||||||
|
self._bg_task: asyncio.Task | None = None
|
||||||
|
|
||||||
|
def _init_schema(self) -> None:
|
||||||
|
self._conn.execute("""
|
||||||
|
CREATE TABLE IF NOT EXISTS snapshots (
|
||||||
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||||
|
timestamp TEXT NOT NULL,
|
||||||
|
provider_name TEXT NOT NULL,
|
||||||
|
status TEXT NOT NULL,
|
||||||
|
error_rate REAL NOT NULL,
|
||||||
|
avg_latency_ms REAL NOT NULL,
|
||||||
|
circuit_state TEXT NOT NULL,
|
||||||
|
total_requests INTEGER NOT NULL
|
||||||
|
)
|
||||||
|
""")
|
||||||
|
self._conn.execute("""
|
||||||
|
CREATE INDEX IF NOT EXISTS idx_snapshots_ts
|
||||||
|
ON snapshots(timestamp)
|
||||||
|
""")
|
||||||
|
self._conn.commit()
|
||||||
|
|
||||||
|
def record_snapshot(self, providers: list[dict]) -> None:
|
||||||
|
"""Record a health snapshot for all providers."""
|
||||||
|
ts = datetime.now(UTC).isoformat()
|
||||||
|
rows = [
|
||||||
|
(
|
||||||
|
ts,
|
||||||
|
p["name"],
|
||||||
|
p["status"],
|
||||||
|
p["error_rate"],
|
||||||
|
p["avg_latency_ms"],
|
||||||
|
p["circuit_state"],
|
||||||
|
p["total_requests"],
|
||||||
|
)
|
||||||
|
for p in providers
|
||||||
|
]
|
||||||
|
self._conn.executemany(
|
||||||
|
"""INSERT INTO snapshots
|
||||||
|
(timestamp, provider_name, status, error_rate,
|
||||||
|
avg_latency_ms, circuit_state, total_requests)
|
||||||
|
VALUES (?, ?, ?, ?, ?, ?, ?)""",
|
||||||
|
rows,
|
||||||
|
)
|
||||||
|
self._conn.commit()
|
||||||
|
|
||||||
|
def get_history(self, hours: int = 24) -> list[dict]:
|
||||||
|
"""Return snapshots from the last N hours, grouped by timestamp."""
|
||||||
|
cutoff = (datetime.now(UTC) - timedelta(hours=hours)).isoformat()
|
||||||
|
rows = self._conn.execute(
|
||||||
|
"""SELECT timestamp, provider_name, status, error_rate,
|
||||||
|
avg_latency_ms, circuit_state, total_requests
|
||||||
|
FROM snapshots WHERE timestamp >= ? ORDER BY timestamp""",
|
||||||
|
(cutoff,),
|
||||||
|
).fetchall()
|
||||||
|
|
||||||
|
# Group by timestamp
|
||||||
|
snapshots: dict[str, list[dict]] = {}
|
||||||
|
for row in rows:
|
||||||
|
ts = row["timestamp"]
|
||||||
|
if ts not in snapshots:
|
||||||
|
snapshots[ts] = []
|
||||||
|
snapshots[ts].append(
|
||||||
|
{
|
||||||
|
"name": row["provider_name"],
|
||||||
|
"status": row["status"],
|
||||||
|
"error_rate": row["error_rate"],
|
||||||
|
"avg_latency_ms": row["avg_latency_ms"],
|
||||||
|
"circuit_state": row["circuit_state"],
|
||||||
|
"total_requests": row["total_requests"],
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
return [{"timestamp": ts, "providers": providers} for ts, providers in snapshots.items()]
|
||||||
|
|
||||||
|
def prune(self, keep_hours: int = 168) -> int:
|
||||||
|
"""Remove snapshots older than keep_hours. Returns rows deleted."""
|
||||||
|
cutoff = (datetime.now(UTC) - timedelta(hours=keep_hours)).isoformat()
|
||||||
|
cursor = self._conn.execute("DELETE FROM snapshots WHERE timestamp < ?", (cutoff,))
|
||||||
|
self._conn.commit()
|
||||||
|
return cursor.rowcount
|
||||||
|
|
||||||
|
def close(self) -> None:
|
||||||
|
"""Close the database connection."""
|
||||||
|
if self._bg_task and not self._bg_task.done():
|
||||||
|
self._bg_task.cancel()
|
||||||
|
self._conn.close()
|
||||||
|
|
||||||
|
def _capture_snapshot(self, cascade_router) -> None: # noqa: ANN001
|
||||||
|
"""Capture current provider state as a snapshot."""
|
||||||
|
providers = []
|
||||||
|
for p in cascade_router.providers:
|
||||||
|
providers.append(
|
||||||
|
{
|
||||||
|
"name": p.name,
|
||||||
|
"status": p.status.value,
|
||||||
|
"error_rate": round(p.metrics.error_rate, 4),
|
||||||
|
"avg_latency_ms": round(p.metrics.avg_latency_ms, 2),
|
||||||
|
"circuit_state": p.circuit_state.value,
|
||||||
|
"total_requests": p.metrics.total_requests,
|
||||||
|
}
|
||||||
|
)
|
||||||
|
self.record_snapshot(providers)
|
||||||
|
|
||||||
|
async def start_background_task(
|
||||||
|
self,
|
||||||
|
cascade_router,
|
||||||
|
interval_seconds: int = 60, # noqa: ANN001
|
||||||
|
) -> None:
|
||||||
|
"""Start periodic snapshot capture."""
|
||||||
|
|
||||||
|
async def _loop() -> None:
|
||||||
|
while True:
|
||||||
|
try:
|
||||||
|
self._capture_snapshot(cascade_router)
|
||||||
|
logger.debug("Recorded health snapshot")
|
||||||
|
except Exception:
|
||||||
|
logger.exception("Failed to record health snapshot")
|
||||||
|
await asyncio.sleep(interval_seconds)
|
||||||
|
|
||||||
|
self._bg_task = asyncio.create_task(_loop())
|
||||||
|
logger.info("Health history background task started (interval=%ds)", interval_seconds)
|
||||||
|
|
||||||
|
|
||||||
|
def get_history_store() -> HealthHistoryStore:
|
||||||
|
"""Get or create the singleton history store."""
|
||||||
|
global _store # noqa: PLW0603
|
||||||
|
if _store is None:
|
||||||
|
_store = HealthHistoryStore()
|
||||||
|
return _store
|
||||||
1
src/lightning/__init__.py
Normal file
1
src/lightning/__init__.py
Normal file
@@ -0,0 +1 @@
|
|||||||
|
"""Lightning Network integration for tool-usage micro-payments."""
|
||||||
69
src/lightning/factory.py
Normal file
69
src/lightning/factory.py
Normal file
@@ -0,0 +1,69 @@
|
|||||||
|
"""Lightning backend factory.
|
||||||
|
|
||||||
|
Returns a mock or real LND backend based on ``settings.lightning_backend``.
|
||||||
|
"""
|
||||||
|
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
import hashlib
|
||||||
|
import logging
|
||||||
|
import secrets
|
||||||
|
from dataclasses import dataclass
|
||||||
|
|
||||||
|
from config import settings
|
||||||
|
|
||||||
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
|
@dataclass
|
||||||
|
class Invoice:
|
||||||
|
"""Minimal Lightning invoice representation."""
|
||||||
|
|
||||||
|
payment_hash: str
|
||||||
|
payment_request: str
|
||||||
|
amount_sats: int
|
||||||
|
memo: str
|
||||||
|
|
||||||
|
|
||||||
|
class MockBackend:
|
||||||
|
"""In-memory mock Lightning backend for development and testing."""
|
||||||
|
|
||||||
|
def create_invoice(self, amount_sats: int, memo: str = "") -> Invoice:
|
||||||
|
"""Create a fake invoice with a random payment hash."""
|
||||||
|
raw = secrets.token_bytes(32)
|
||||||
|
payment_hash = hashlib.sha256(raw).hexdigest()
|
||||||
|
payment_request = f"lnbc{amount_sats}mock{payment_hash[:20]}"
|
||||||
|
logger.debug("Mock invoice: %s sats — %s", amount_sats, payment_hash[:12])
|
||||||
|
return Invoice(
|
||||||
|
payment_hash=payment_hash,
|
||||||
|
payment_request=payment_request,
|
||||||
|
amount_sats=amount_sats,
|
||||||
|
memo=memo,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
# Singleton — lazily created
|
||||||
|
_backend: MockBackend | None = None
|
||||||
|
|
||||||
|
|
||||||
|
def get_backend() -> MockBackend:
|
||||||
|
"""Return the configured Lightning backend (currently mock-only).
|
||||||
|
|
||||||
|
Raises ``ValueError`` if an unsupported backend is requested.
|
||||||
|
"""
|
||||||
|
global _backend # noqa: PLW0603
|
||||||
|
if _backend is not None:
|
||||||
|
return _backend
|
||||||
|
|
||||||
|
kind = settings.lightning_backend
|
||||||
|
if kind == "mock":
|
||||||
|
_backend = MockBackend()
|
||||||
|
elif kind == "lnd":
|
||||||
|
# LND gRPC integration is on the roadmap — for now fall back to mock.
|
||||||
|
logger.warning("LND backend not yet implemented — using mock")
|
||||||
|
_backend = MockBackend()
|
||||||
|
else:
|
||||||
|
raise ValueError(f"Unknown lightning_backend: {kind!r}")
|
||||||
|
|
||||||
|
logger.info("Lightning backend: %s", kind)
|
||||||
|
return _backend
|
||||||
146
src/lightning/ledger.py
Normal file
146
src/lightning/ledger.py
Normal file
@@ -0,0 +1,146 @@
|
|||||||
|
"""In-memory Lightning transaction ledger.
|
||||||
|
|
||||||
|
Tracks invoices, settlements, and balances per the schema in
|
||||||
|
``docs/adr/018-lightning-ledger.md``. Uses a simple in-memory list so the
|
||||||
|
dashboard can display real (ephemeral) data without requiring SQLite yet.
|
||||||
|
"""
|
||||||
|
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
import logging
|
||||||
|
import uuid
|
||||||
|
from dataclasses import dataclass
|
||||||
|
from datetime import UTC, datetime
|
||||||
|
from enum import StrEnum
|
||||||
|
|
||||||
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
|
class TxType(StrEnum):
|
||||||
|
incoming = "incoming"
|
||||||
|
outgoing = "outgoing"
|
||||||
|
|
||||||
|
|
||||||
|
class TxStatus(StrEnum):
|
||||||
|
pending = "pending"
|
||||||
|
settled = "settled"
|
||||||
|
failed = "failed"
|
||||||
|
expired = "expired"
|
||||||
|
|
||||||
|
|
||||||
|
@dataclass
|
||||||
|
class LedgerEntry:
|
||||||
|
"""Single ledger row matching the ADR-018 schema."""
|
||||||
|
|
||||||
|
id: str
|
||||||
|
tx_type: TxType
|
||||||
|
status: TxStatus
|
||||||
|
payment_hash: str
|
||||||
|
amount_sats: int
|
||||||
|
memo: str
|
||||||
|
source: str
|
||||||
|
created_at: str
|
||||||
|
invoice: str = ""
|
||||||
|
preimage: str = ""
|
||||||
|
task_id: str = ""
|
||||||
|
agent_id: str = ""
|
||||||
|
settled_at: str = ""
|
||||||
|
fee_sats: int = 0
|
||||||
|
|
||||||
|
|
||||||
|
# ── In-memory store ──────────────────────────────────────────────────
|
||||||
|
_entries: list[LedgerEntry] = []
|
||||||
|
|
||||||
|
|
||||||
|
def create_invoice_entry(
|
||||||
|
payment_hash: str,
|
||||||
|
amount_sats: int,
|
||||||
|
memo: str = "",
|
||||||
|
source: str = "tool_usage",
|
||||||
|
task_id: str = "",
|
||||||
|
agent_id: str = "",
|
||||||
|
invoice: str = "",
|
||||||
|
) -> LedgerEntry:
|
||||||
|
"""Record a new incoming invoice in the ledger."""
|
||||||
|
entry = LedgerEntry(
|
||||||
|
id=uuid.uuid4().hex[:16],
|
||||||
|
tx_type=TxType.incoming,
|
||||||
|
status=TxStatus.pending,
|
||||||
|
payment_hash=payment_hash,
|
||||||
|
amount_sats=amount_sats,
|
||||||
|
memo=memo,
|
||||||
|
source=source,
|
||||||
|
task_id=task_id,
|
||||||
|
agent_id=agent_id,
|
||||||
|
invoice=invoice,
|
||||||
|
created_at=datetime.now(UTC).isoformat(),
|
||||||
|
)
|
||||||
|
_entries.append(entry)
|
||||||
|
logger.debug("Ledger entry created: %s (%s sats)", entry.id, amount_sats)
|
||||||
|
return entry
|
||||||
|
|
||||||
|
|
||||||
|
def mark_settled(payment_hash: str, preimage: str = "") -> LedgerEntry | None:
|
||||||
|
"""Mark a pending entry as settled by payment hash."""
|
||||||
|
for entry in _entries:
|
||||||
|
if entry.payment_hash == payment_hash and entry.status == TxStatus.pending:
|
||||||
|
entry.status = TxStatus.settled
|
||||||
|
entry.preimage = preimage
|
||||||
|
entry.settled_at = datetime.now(UTC).isoformat()
|
||||||
|
logger.debug("Ledger settled: %s", payment_hash[:12])
|
||||||
|
return entry
|
||||||
|
return None
|
||||||
|
|
||||||
|
|
||||||
|
def get_balance() -> dict:
|
||||||
|
"""Compute the current balance from settled and pending entries."""
|
||||||
|
incoming_total = sum(
|
||||||
|
e.amount_sats
|
||||||
|
for e in _entries
|
||||||
|
if e.tx_type == TxType.incoming and e.status == TxStatus.settled
|
||||||
|
)
|
||||||
|
outgoing_total = sum(
|
||||||
|
e.amount_sats
|
||||||
|
for e in _entries
|
||||||
|
if e.tx_type == TxType.outgoing and e.status == TxStatus.settled
|
||||||
|
)
|
||||||
|
fees = sum(e.fee_sats for e in _entries if e.status == TxStatus.settled)
|
||||||
|
pending_in = sum(
|
||||||
|
e.amount_sats
|
||||||
|
for e in _entries
|
||||||
|
if e.tx_type == TxType.incoming and e.status == TxStatus.pending
|
||||||
|
)
|
||||||
|
pending_out = sum(
|
||||||
|
e.amount_sats
|
||||||
|
for e in _entries
|
||||||
|
if e.tx_type == TxType.outgoing and e.status == TxStatus.pending
|
||||||
|
)
|
||||||
|
net = incoming_total - outgoing_total - fees
|
||||||
|
return {
|
||||||
|
"incoming_total_sats": incoming_total,
|
||||||
|
"outgoing_total_sats": outgoing_total,
|
||||||
|
"fees_paid_sats": fees,
|
||||||
|
"net_sats": net,
|
||||||
|
"pending_incoming_sats": pending_in,
|
||||||
|
"pending_outgoing_sats": pending_out,
|
||||||
|
"available_sats": net - pending_out,
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
def get_transactions(
|
||||||
|
tx_type: str | None = None,
|
||||||
|
status: str | None = None,
|
||||||
|
limit: int = 50,
|
||||||
|
) -> list[LedgerEntry]:
|
||||||
|
"""Return ledger entries, optionally filtered."""
|
||||||
|
result = _entries
|
||||||
|
if tx_type:
|
||||||
|
result = [e for e in result if e.tx_type.value == tx_type]
|
||||||
|
if status:
|
||||||
|
result = [e for e in result if e.status.value == status]
|
||||||
|
return list(reversed(result))[:limit]
|
||||||
|
|
||||||
|
|
||||||
|
def clear() -> None:
|
||||||
|
"""Reset the ledger (for testing)."""
|
||||||
|
_entries.clear()
|
||||||
@@ -119,22 +119,9 @@ class BaseAgent(ABC):
|
|||||||
"""
|
"""
|
||||||
pass
|
pass
|
||||||
|
|
||||||
async def run(self, message: str) -> str:
|
|
||||||
"""Run the agent with a message.
|
|
||||||
|
|
||||||
Retries on transient failures (connection errors, timeouts) with
|
|
||||||
exponential backoff. GPU contention from concurrent Ollama
|
|
||||||
requests causes ReadError / ReadTimeout — these are transient
|
|
||||||
and should be retried, not raised immediately (#70).
|
|
||||||
|
|
||||||
Returns:
|
|
||||||
Agent response
|
|
||||||
"""
|
|
||||||
max_retries = 3
|
|
||||||
last_exception = None
|
|
||||||
# Transient errors that indicate Ollama contention or temporary
|
# Transient errors that indicate Ollama contention or temporary
|
||||||
# unavailability — these deserve a retry with backoff.
|
# unavailability — these deserve a retry with backoff.
|
||||||
_transient = (
|
_TRANSIENT = (
|
||||||
httpx.ConnectError,
|
httpx.ConnectError,
|
||||||
httpx.ReadError,
|
httpx.ReadError,
|
||||||
httpx.ReadTimeout,
|
httpx.ReadTimeout,
|
||||||
@@ -143,51 +130,73 @@ class BaseAgent(ABC):
|
|||||||
TimeoutError,
|
TimeoutError,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
async def run(self, message: str, *, max_retries: int = 3) -> str:
|
||||||
|
"""Run the agent with a message, retrying on transient failures.
|
||||||
|
|
||||||
|
GPU contention from concurrent Ollama requests causes ReadError /
|
||||||
|
ReadTimeout — these are transient and retried with exponential
|
||||||
|
backoff (#70).
|
||||||
|
"""
|
||||||
|
response = await self._run_with_retries(message, max_retries)
|
||||||
|
await self._emit_response_event(message, response)
|
||||||
|
return response
|
||||||
|
|
||||||
|
async def _run_with_retries(self, message: str, max_retries: int) -> str:
|
||||||
|
"""Execute agent.run() with retry logic for transient errors."""
|
||||||
for attempt in range(1, max_retries + 1):
|
for attempt in range(1, max_retries + 1):
|
||||||
try:
|
try:
|
||||||
result = self.agent.run(message, stream=False)
|
result = self.agent.run(message, stream=False)
|
||||||
response = result.content if hasattr(result, "content") else str(result)
|
return result.content if hasattr(result, "content") else str(result)
|
||||||
break # Success, exit the retry loop
|
except self._TRANSIENT as exc:
|
||||||
except _transient as exc:
|
self._handle_retry_or_raise(
|
||||||
last_exception = exc
|
exc,
|
||||||
|
attempt,
|
||||||
|
max_retries,
|
||||||
|
transient=True,
|
||||||
|
)
|
||||||
|
await asyncio.sleep(min(2**attempt, 16))
|
||||||
|
except Exception as exc:
|
||||||
|
self._handle_retry_or_raise(
|
||||||
|
exc,
|
||||||
|
attempt,
|
||||||
|
max_retries,
|
||||||
|
transient=False,
|
||||||
|
)
|
||||||
|
await asyncio.sleep(min(2 ** (attempt - 1), 8))
|
||||||
|
# Unreachable — _handle_retry_or_raise raises on last attempt.
|
||||||
|
raise RuntimeError("retry loop exited unexpectedly") # pragma: no cover
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def _handle_retry_or_raise(
|
||||||
|
exc: Exception,
|
||||||
|
attempt: int,
|
||||||
|
max_retries: int,
|
||||||
|
*,
|
||||||
|
transient: bool,
|
||||||
|
) -> None:
|
||||||
|
"""Log a retry warning or raise after exhausting attempts."""
|
||||||
if attempt < max_retries:
|
if attempt < max_retries:
|
||||||
# Contention backoff — longer waits because the GPU
|
if transient:
|
||||||
# needs time to finish the other request.
|
|
||||||
wait = min(2**attempt, 16)
|
|
||||||
logger.warning(
|
logger.warning(
|
||||||
"Ollama contention on attempt %d/%d: %s. Waiting %ds before retry...",
|
"Ollama contention on attempt %d/%d: %s. Waiting before retry...",
|
||||||
attempt,
|
attempt,
|
||||||
max_retries,
|
max_retries,
|
||||||
type(exc).__name__,
|
type(exc).__name__,
|
||||||
wait,
|
|
||||||
)
|
)
|
||||||
await asyncio.sleep(wait)
|
|
||||||
else:
|
else:
|
||||||
logger.error(
|
|
||||||
"Ollama unreachable after %d attempts: %s",
|
|
||||||
max_retries,
|
|
||||||
exc,
|
|
||||||
)
|
|
||||||
raise last_exception from exc
|
|
||||||
except Exception as exc:
|
|
||||||
last_exception = exc
|
|
||||||
if attempt < max_retries:
|
|
||||||
logger.warning(
|
logger.warning(
|
||||||
"Agent run failed on attempt %d/%d: %s. Retrying...",
|
"Agent run failed on attempt %d/%d: %s. Retrying...",
|
||||||
attempt,
|
attempt,
|
||||||
max_retries,
|
max_retries,
|
||||||
exc,
|
exc,
|
||||||
)
|
)
|
||||||
await asyncio.sleep(min(2 ** (attempt - 1), 8))
|
|
||||||
else:
|
else:
|
||||||
logger.error(
|
label = "Ollama unreachable" if transient else "Agent run failed"
|
||||||
"Agent run failed after %d attempts: %s",
|
logger.error("%s after %d attempts: %s", label, max_retries, exc)
|
||||||
max_retries,
|
raise exc
|
||||||
exc,
|
|
||||||
)
|
|
||||||
raise last_exception from exc
|
|
||||||
|
|
||||||
# Emit completion event
|
async def _emit_response_event(self, message: str, response: str) -> None:
|
||||||
|
"""Publish a completion event to the event bus if connected."""
|
||||||
if self.event_bus:
|
if self.event_bus:
|
||||||
await self.event_bus.publish(
|
await self.event_bus.publish(
|
||||||
Event(
|
Event(
|
||||||
@@ -197,8 +206,6 @@ class BaseAgent(ABC):
|
|||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
return response
|
|
||||||
|
|
||||||
def get_capabilities(self) -> list[str]:
|
def get_capabilities(self) -> list[str]:
|
||||||
"""Get list of capabilities this agent provides."""
|
"""Get list of capabilities this agent provides."""
|
||||||
return self.tools
|
return self.tools
|
||||||
|
|||||||
@@ -99,23 +99,27 @@ class GrokBackend:
|
|||||||
|
|
||||||
def _get_client(self):
|
def _get_client(self):
|
||||||
"""Create OpenAI client configured for xAI endpoint."""
|
"""Create OpenAI client configured for xAI endpoint."""
|
||||||
|
from config import settings
|
||||||
|
|
||||||
import httpx
|
import httpx
|
||||||
from openai import OpenAI
|
from openai import OpenAI
|
||||||
|
|
||||||
return OpenAI(
|
return OpenAI(
|
||||||
api_key=self._api_key,
|
api_key=self._api_key,
|
||||||
base_url="https://api.x.ai/v1",
|
base_url=settings.xai_base_url,
|
||||||
timeout=httpx.Timeout(300.0),
|
timeout=httpx.Timeout(300.0),
|
||||||
)
|
)
|
||||||
|
|
||||||
async def _get_async_client(self):
|
async def _get_async_client(self):
|
||||||
"""Create async OpenAI client configured for xAI endpoint."""
|
"""Create async OpenAI client configured for xAI endpoint."""
|
||||||
|
from config import settings
|
||||||
|
|
||||||
import httpx
|
import httpx
|
||||||
from openai import AsyncOpenAI
|
from openai import AsyncOpenAI
|
||||||
|
|
||||||
return AsyncOpenAI(
|
return AsyncOpenAI(
|
||||||
api_key=self._api_key,
|
api_key=self._api_key,
|
||||||
base_url="https://api.x.ai/v1",
|
base_url=settings.xai_base_url,
|
||||||
timeout=httpx.Timeout(300.0),
|
timeout=httpx.Timeout(300.0),
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|||||||
@@ -37,6 +37,35 @@ def _is_interactive() -> bool:
|
|||||||
return hasattr(sys.stdin, "isatty") and sys.stdin.isatty()
|
return hasattr(sys.stdin, "isatty") and sys.stdin.isatty()
|
||||||
|
|
||||||
|
|
||||||
|
def _prompt_interactive(req, tool_name: str, tool_args: dict) -> None:
|
||||||
|
"""Display tool details and prompt the human for approval."""
|
||||||
|
description = format_action_description(tool_name, tool_args)
|
||||||
|
impact = get_impact_level(tool_name)
|
||||||
|
|
||||||
|
typer.echo()
|
||||||
|
typer.echo(typer.style("Tool confirmation required", bold=True))
|
||||||
|
typer.echo(f" Impact: {impact.upper()}")
|
||||||
|
typer.echo(f" {description}")
|
||||||
|
typer.echo()
|
||||||
|
|
||||||
|
if typer.confirm("Allow this action?", default=False):
|
||||||
|
req.confirm()
|
||||||
|
logger.info("CLI: approved %s", tool_name)
|
||||||
|
else:
|
||||||
|
req.reject(note="User rejected from CLI")
|
||||||
|
logger.info("CLI: rejected %s", tool_name)
|
||||||
|
|
||||||
|
|
||||||
|
def _decide_autonomous(req, tool_name: str, tool_args: dict) -> None:
|
||||||
|
"""Auto-approve allowlisted tools; reject everything else."""
|
||||||
|
if is_allowlisted(tool_name, tool_args):
|
||||||
|
req.confirm()
|
||||||
|
logger.info("AUTO-APPROVED (allowlist): %s", tool_name)
|
||||||
|
else:
|
||||||
|
req.reject(note="Auto-rejected: not in allowlist")
|
||||||
|
logger.info("AUTO-REJECTED (not allowlisted): %s %s", tool_name, str(tool_args)[:100])
|
||||||
|
|
||||||
|
|
||||||
def _handle_tool_confirmation(agent, run_output, session_id: str, *, autonomous: bool = False):
|
def _handle_tool_confirmation(agent, run_output, session_id: str, *, autonomous: bool = False):
|
||||||
"""Prompt user to approve/reject dangerous tool calls.
|
"""Prompt user to approve/reject dangerous tool calls.
|
||||||
|
|
||||||
@@ -51,6 +80,7 @@ def _handle_tool_confirmation(agent, run_output, session_id: str, *, autonomous:
|
|||||||
Returns the final RunOutput after all confirmations are resolved.
|
Returns the final RunOutput after all confirmations are resolved.
|
||||||
"""
|
"""
|
||||||
interactive = _is_interactive() and not autonomous
|
interactive = _is_interactive() and not autonomous
|
||||||
|
decide = _prompt_interactive if interactive else _decide_autonomous
|
||||||
|
|
||||||
max_rounds = 10 # safety limit
|
max_rounds = 10 # safety limit
|
||||||
for _ in range(max_rounds):
|
for _ in range(max_rounds):
|
||||||
@@ -66,39 +96,10 @@ def _handle_tool_confirmation(agent, run_output, session_id: str, *, autonomous:
|
|||||||
for req in reqs:
|
for req in reqs:
|
||||||
if not getattr(req, "needs_confirmation", False):
|
if not getattr(req, "needs_confirmation", False):
|
||||||
continue
|
continue
|
||||||
|
|
||||||
te = req.tool_execution
|
te = req.tool_execution
|
||||||
tool_name = getattr(te, "tool_name", "unknown")
|
tool_name = getattr(te, "tool_name", "unknown")
|
||||||
tool_args = getattr(te, "tool_args", {}) or {}
|
tool_args = getattr(te, "tool_args", {}) or {}
|
||||||
|
decide(req, tool_name, tool_args)
|
||||||
if interactive:
|
|
||||||
# Human present — prompt for approval
|
|
||||||
description = format_action_description(tool_name, tool_args)
|
|
||||||
impact = get_impact_level(tool_name)
|
|
||||||
|
|
||||||
typer.echo()
|
|
||||||
typer.echo(typer.style("Tool confirmation required", bold=True))
|
|
||||||
typer.echo(f" Impact: {impact.upper()}")
|
|
||||||
typer.echo(f" {description}")
|
|
||||||
typer.echo()
|
|
||||||
|
|
||||||
approved = typer.confirm("Allow this action?", default=False)
|
|
||||||
if approved:
|
|
||||||
req.confirm()
|
|
||||||
logger.info("CLI: approved %s", tool_name)
|
|
||||||
else:
|
|
||||||
req.reject(note="User rejected from CLI")
|
|
||||||
logger.info("CLI: rejected %s", tool_name)
|
|
||||||
else:
|
|
||||||
# Autonomous mode — check allowlist
|
|
||||||
if is_allowlisted(tool_name, tool_args):
|
|
||||||
req.confirm()
|
|
||||||
logger.info("AUTO-APPROVED (allowlist): %s", tool_name)
|
|
||||||
else:
|
|
||||||
req.reject(note="Auto-rejected: not in allowlist")
|
|
||||||
logger.info(
|
|
||||||
"AUTO-REJECTED (not allowlisted): %s %s", tool_name, str(tool_args)[:100]
|
|
||||||
)
|
|
||||||
|
|
||||||
# Resume the run so the agent sees the confirmation result
|
# Resume the run so the agent sees the confirmation result
|
||||||
try:
|
try:
|
||||||
@@ -138,7 +139,7 @@ def think(
|
|||||||
model_size: str | None = _MODEL_SIZE_OPTION,
|
model_size: str | None = _MODEL_SIZE_OPTION,
|
||||||
):
|
):
|
||||||
"""Ask Timmy to think carefully about a topic."""
|
"""Ask Timmy to think carefully about a topic."""
|
||||||
timmy = create_timmy(backend=backend, model_size=model_size, session_id=_CLI_SESSION_ID)
|
timmy = create_timmy(backend=backend, session_id=_CLI_SESSION_ID)
|
||||||
timmy.print_response(f"Think carefully about: {topic}", stream=True, session_id=_CLI_SESSION_ID)
|
timmy.print_response(f"Think carefully about: {topic}", stream=True, session_id=_CLI_SESSION_ID)
|
||||||
|
|
||||||
|
|
||||||
@@ -201,7 +202,7 @@ def chat(
|
|||||||
session_id = str(uuid.uuid4())
|
session_id = str(uuid.uuid4())
|
||||||
else:
|
else:
|
||||||
session_id = _CLI_SESSION_ID
|
session_id = _CLI_SESSION_ID
|
||||||
timmy = create_timmy(backend=backend, model_size=model_size, session_id=session_id)
|
timmy = create_timmy(backend=backend, session_id=session_id)
|
||||||
|
|
||||||
# Use agent.run() so we can intercept paused runs for tool confirmation.
|
# Use agent.run() so we can intercept paused runs for tool confirmation.
|
||||||
run_output = timmy.run(message_str, stream=False, session_id=session_id)
|
run_output = timmy.run(message_str, stream=False, session_id=session_id)
|
||||||
@@ -278,7 +279,7 @@ def status(
|
|||||||
model_size: str | None = _MODEL_SIZE_OPTION,
|
model_size: str | None = _MODEL_SIZE_OPTION,
|
||||||
):
|
):
|
||||||
"""Print Timmy's operational status."""
|
"""Print Timmy's operational status."""
|
||||||
timmy = create_timmy(backend=backend, model_size=model_size, session_id=_CLI_SESSION_ID)
|
timmy = create_timmy(backend=backend, session_id=_CLI_SESSION_ID)
|
||||||
timmy.print_response(STATUS_PROMPT, stream=False, session_id=_CLI_SESSION_ID)
|
timmy.print_response(STATUS_PROMPT, stream=False, session_id=_CLI_SESSION_ID)
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -174,15 +174,8 @@ class ConversationManager:
|
|||||||
|
|
||||||
return None
|
return None
|
||||||
|
|
||||||
def should_use_tools(self, message: str, context: ConversationContext) -> bool:
|
_TOOL_KEYWORDS = frozenset(
|
||||||
"""Determine if this message likely requires tools.
|
{
|
||||||
|
|
||||||
Returns True if tools are likely needed, False for simple chat.
|
|
||||||
"""
|
|
||||||
message_lower = message.lower().strip()
|
|
||||||
|
|
||||||
# Tool keywords that suggest tool usage is needed
|
|
||||||
tool_keywords = [
|
|
||||||
"search",
|
"search",
|
||||||
"look up",
|
"look up",
|
||||||
"find",
|
"find",
|
||||||
@@ -203,10 +196,11 @@ class ConversationManager:
|
|||||||
"shell",
|
"shell",
|
||||||
"command",
|
"command",
|
||||||
"install",
|
"install",
|
||||||
]
|
}
|
||||||
|
)
|
||||||
|
|
||||||
# Chat-only keywords that definitely don't need tools
|
_CHAT_ONLY_KEYWORDS = frozenset(
|
||||||
chat_only = [
|
{
|
||||||
"hello",
|
"hello",
|
||||||
"hi ",
|
"hi ",
|
||||||
"hey",
|
"hey",
|
||||||
@@ -221,30 +215,47 @@ class ConversationManager:
|
|||||||
"goodbye",
|
"goodbye",
|
||||||
"tell me about yourself",
|
"tell me about yourself",
|
||||||
"what can you do",
|
"what can you do",
|
||||||
]
|
}
|
||||||
|
)
|
||||||
|
|
||||||
# Check for chat-only patterns first
|
_SIMPLE_QUESTION_PREFIXES = ("what is", "who is", "how does", "why is", "when did", "where is")
|
||||||
for pattern in chat_only:
|
_TIME_WORDS = ("today", "now", "current", "latest", "this week", "this month")
|
||||||
if pattern in message_lower:
|
|
||||||
|
def _is_chat_only(self, message_lower: str) -> bool:
|
||||||
|
"""Return True if the message matches a chat-only pattern."""
|
||||||
|
return any(kw in message_lower for kw in self._CHAT_ONLY_KEYWORDS)
|
||||||
|
|
||||||
|
def _has_tool_keyword(self, message_lower: str) -> bool:
|
||||||
|
"""Return True if the message contains a tool-related keyword."""
|
||||||
|
return any(kw in message_lower for kw in self._TOOL_KEYWORDS)
|
||||||
|
|
||||||
|
def _is_simple_question(self, message_lower: str) -> bool | None:
|
||||||
|
"""Check if message is a simple question.
|
||||||
|
|
||||||
|
Returns True if it needs tools (real-time info), False if it
|
||||||
|
doesn't, or None if the message isn't a simple question.
|
||||||
|
"""
|
||||||
|
for prefix in self._SIMPLE_QUESTION_PREFIXES:
|
||||||
|
if message_lower.startswith(prefix):
|
||||||
|
return any(t in message_lower for t in self._TIME_WORDS)
|
||||||
|
return None
|
||||||
|
|
||||||
|
def should_use_tools(self, message: str, context: ConversationContext) -> bool:
|
||||||
|
"""Determine if this message likely requires tools.
|
||||||
|
|
||||||
|
Returns True if tools are likely needed, False for simple chat.
|
||||||
|
"""
|
||||||
|
message_lower = message.lower().strip()
|
||||||
|
|
||||||
|
if self._is_chat_only(message_lower):
|
||||||
return False
|
return False
|
||||||
|
if self._has_tool_keyword(message_lower):
|
||||||
# Check for tool keywords
|
|
||||||
for keyword in tool_keywords:
|
|
||||||
if keyword in message_lower:
|
|
||||||
return True
|
return True
|
||||||
|
|
||||||
# Simple questions (starting with what, who, how, why, when, where)
|
simple = self._is_simple_question(message_lower)
|
||||||
# usually don't need tools unless about current/real-time info
|
if simple is not None:
|
||||||
simple_question_words = ["what is", "who is", "how does", "why is", "when did", "where is"]
|
return simple
|
||||||
for word in simple_question_words:
|
|
||||||
if message_lower.startswith(word):
|
|
||||||
# Check if it's asking about current/real-time info
|
|
||||||
time_words = ["today", "now", "current", "latest", "this week", "this month"]
|
|
||||||
if any(t in message_lower for t in time_words):
|
|
||||||
return True
|
|
||||||
return False
|
|
||||||
|
|
||||||
# Default: don't use tools for unclear cases
|
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -21,12 +21,16 @@ Usage::
|
|||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
import logging
|
import logging
|
||||||
|
from typing import TYPE_CHECKING
|
||||||
|
|
||||||
|
if TYPE_CHECKING:
|
||||||
|
from PIL import ImageDraw
|
||||||
import os
|
import os
|
||||||
import shutil
|
import shutil
|
||||||
import sqlite3
|
import sqlite3
|
||||||
import uuid
|
import uuid
|
||||||
from contextlib import closing
|
from contextlib import closing
|
||||||
from datetime import datetime
|
from datetime import UTC, datetime
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
|
||||||
import httpx
|
import httpx
|
||||||
@@ -192,7 +196,7 @@ def _bridge_to_work_order(title: str, body: str, category: str) -> None:
|
|||||||
body,
|
body,
|
||||||
category,
|
category,
|
||||||
"timmy-thinking",
|
"timmy-thinking",
|
||||||
datetime.utcnow().isoformat(),
|
datetime.now(UTC).isoformat(),
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
conn.commit()
|
conn.commit()
|
||||||
@@ -200,15 +204,61 @@ def _bridge_to_work_order(title: str, body: str, category: str) -> None:
|
|||||||
logger.debug("Work order bridge failed: %s", exc)
|
logger.debug("Work order bridge failed: %s", exc)
|
||||||
|
|
||||||
|
|
||||||
|
async def _ensure_issue_session():
|
||||||
|
"""Get or create the cached MCP session, connecting if needed.
|
||||||
|
|
||||||
|
Returns the connected ``MCPTools`` instance.
|
||||||
|
"""
|
||||||
|
from agno.tools.mcp import MCPTools
|
||||||
|
|
||||||
|
global _issue_session
|
||||||
|
|
||||||
|
if _issue_session is None:
|
||||||
|
_issue_session = MCPTools(
|
||||||
|
server_params=_gitea_server_params(),
|
||||||
|
timeout_seconds=settings.mcp_timeout,
|
||||||
|
)
|
||||||
|
|
||||||
|
if not getattr(_issue_session, "_connected", False):
|
||||||
|
await _issue_session.connect()
|
||||||
|
_issue_session._connected = True
|
||||||
|
|
||||||
|
return _issue_session
|
||||||
|
|
||||||
|
|
||||||
|
def _build_issue_body(body: str) -> str:
|
||||||
|
"""Append the auto-filing signature to the issue body."""
|
||||||
|
full_body = body
|
||||||
|
if full_body:
|
||||||
|
full_body += "\n\n"
|
||||||
|
full_body += "---\n*Auto-filed by Timmy's thinking engine*"
|
||||||
|
return full_body
|
||||||
|
|
||||||
|
|
||||||
|
def _build_issue_args(title: str, full_body: str) -> dict:
|
||||||
|
"""Build MCP tool arguments for ``issue_write`` with method=create."""
|
||||||
|
owner, repo = settings.gitea_repo.split("/", 1)
|
||||||
|
return {
|
||||||
|
"method": "create",
|
||||||
|
"owner": owner,
|
||||||
|
"repo": repo,
|
||||||
|
"title": title,
|
||||||
|
"body": full_body,
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
def _category_from_labels(labels: str) -> str:
|
||||||
|
"""Derive a work-order category from comma-separated label names."""
|
||||||
|
label_list = [tag.strip() for tag in labels.split(",") if tag.strip()] if labels else []
|
||||||
|
return "bug" if "bug" in label_list else "suggestion"
|
||||||
|
|
||||||
|
|
||||||
async def create_gitea_issue_via_mcp(title: str, body: str = "", labels: str = "") -> str:
|
async def create_gitea_issue_via_mcp(title: str, body: str = "", labels: str = "") -> str:
|
||||||
"""File a Gitea issue via the MCP server (standalone, no LLM loop).
|
"""File a Gitea issue via the MCP server (standalone, no LLM loop).
|
||||||
|
|
||||||
Used by the thinking engine's ``_maybe_file_issues()`` post-hook.
|
Used by the thinking engine's ``_maybe_file_issues()`` post-hook.
|
||||||
Manages its own MCPTools session with lazy connect + graceful failure.
|
Manages its own MCPTools session with lazy connect + graceful failure.
|
||||||
|
|
||||||
Uses ``tools.session.call_tool()`` for direct MCP invocation — the
|
|
||||||
``MCPTools`` wrapper itself does not expose ``call_tool()``.
|
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
title: Issue title.
|
title: Issue title.
|
||||||
body: Issue body (markdown).
|
body: Issue body (markdown).
|
||||||
@@ -221,46 +271,13 @@ async def create_gitea_issue_via_mcp(title: str, body: str = "", labels: str = "
|
|||||||
return "Gitea integration is not configured."
|
return "Gitea integration is not configured."
|
||||||
|
|
||||||
try:
|
try:
|
||||||
from agno.tools.mcp import MCPTools
|
session = await _ensure_issue_session()
|
||||||
|
full_body = _build_issue_body(body)
|
||||||
|
args = _build_issue_args(title, full_body)
|
||||||
|
|
||||||
global _issue_session
|
result = await session.session.call_tool("issue_write", arguments=args)
|
||||||
|
|
||||||
if _issue_session is None:
|
_bridge_to_work_order(title, body, _category_from_labels(labels))
|
||||||
_issue_session = MCPTools(
|
|
||||||
server_params=_gitea_server_params(),
|
|
||||||
timeout_seconds=settings.mcp_timeout,
|
|
||||||
)
|
|
||||||
|
|
||||||
# Ensure connected
|
|
||||||
if not getattr(_issue_session, "_connected", False):
|
|
||||||
await _issue_session.connect()
|
|
||||||
_issue_session._connected = True
|
|
||||||
|
|
||||||
# Append auto-filing signature
|
|
||||||
full_body = body
|
|
||||||
if full_body:
|
|
||||||
full_body += "\n\n"
|
|
||||||
full_body += "---\n*Auto-filed by Timmy's thinking engine*"
|
|
||||||
|
|
||||||
# Parse owner/repo from settings
|
|
||||||
owner, repo = settings.gitea_repo.split("/", 1)
|
|
||||||
|
|
||||||
# Build tool arguments — gitea-mcp uses issue_write with method="create"
|
|
||||||
args = {
|
|
||||||
"method": "create",
|
|
||||||
"owner": owner,
|
|
||||||
"repo": repo,
|
|
||||||
"title": title,
|
|
||||||
"body": full_body,
|
|
||||||
}
|
|
||||||
|
|
||||||
# Call via the underlying MCP session (MCPTools doesn't expose call_tool)
|
|
||||||
result = await _issue_session.session.call_tool("issue_write", arguments=args)
|
|
||||||
|
|
||||||
# Bridge to local work order
|
|
||||||
label_list = [tag.strip() for tag in labels.split(",") if tag.strip()] if labels else []
|
|
||||||
category = "bug" if "bug" in label_list else "suggestion"
|
|
||||||
_bridge_to_work_order(title, body, category)
|
|
||||||
|
|
||||||
logger.info("Created Gitea issue via MCP: %s", title[:60])
|
logger.info("Created Gitea issue via MCP: %s", title[:60])
|
||||||
return f"Created issue: {title}\n{result}"
|
return f"Created issue: {title}\n{result}"
|
||||||
@@ -270,20 +287,8 @@ async def create_gitea_issue_via_mcp(title: str, body: str = "", labels: str = "
|
|||||||
return f"Failed to create issue via MCP: {exc}"
|
return f"Failed to create issue via MCP: {exc}"
|
||||||
|
|
||||||
|
|
||||||
def _generate_avatar_image() -> bytes:
|
def _draw_background(draw: ImageDraw.ImageDraw, size: int) -> None:
|
||||||
"""Generate a Timmy-themed avatar image using Pillow.
|
"""Draw radial gradient background with concentric circles."""
|
||||||
|
|
||||||
Creates a 512x512 wizard-themed avatar with emerald/purple/gold palette.
|
|
||||||
Returns raw PNG bytes. Falls back to a minimal solid-color image if
|
|
||||||
Pillow drawing primitives fail.
|
|
||||||
"""
|
|
||||||
from PIL import Image, ImageDraw
|
|
||||||
|
|
||||||
size = 512
|
|
||||||
img = Image.new("RGB", (size, size), (15, 25, 20))
|
|
||||||
draw = ImageDraw.Draw(img)
|
|
||||||
|
|
||||||
# Background gradient effect — concentric circles
|
|
||||||
for i in range(size // 2, 0, -4):
|
for i in range(size // 2, 0, -4):
|
||||||
g = int(25 + (i / (size // 2)) * 30)
|
g = int(25 + (i / (size // 2)) * 30)
|
||||||
draw.ellipse(
|
draw.ellipse(
|
||||||
@@ -291,33 +296,45 @@ def _generate_avatar_image() -> bytes:
|
|||||||
fill=(10, g, 20),
|
fill=(10, g, 20),
|
||||||
)
|
)
|
||||||
|
|
||||||
# Wizard hat (triangle)
|
|
||||||
|
def _draw_wizard(draw: ImageDraw.ImageDraw) -> None:
|
||||||
|
"""Draw wizard hat, face, eyes, smile, monogram, and robe."""
|
||||||
hat_color = (100, 50, 160) # purple
|
hat_color = (100, 50, 160) # purple
|
||||||
draw.polygon(
|
hat_outline = (180, 130, 255)
|
||||||
[(256, 40), (160, 220), (352, 220)],
|
gold = (220, 190, 50)
|
||||||
fill=hat_color,
|
pupil = (30, 30, 60)
|
||||||
outline=(180, 130, 255),
|
|
||||||
)
|
|
||||||
|
|
||||||
# Hat brim
|
# Hat + brim
|
||||||
draw.ellipse([140, 200, 372, 250], fill=hat_color, outline=(180, 130, 255))
|
draw.polygon([(256, 40), (160, 220), (352, 220)], fill=hat_color, outline=hat_outline)
|
||||||
|
draw.ellipse([140, 200, 372, 250], fill=hat_color, outline=hat_outline)
|
||||||
|
|
||||||
# Face circle
|
# Face
|
||||||
draw.ellipse([190, 220, 322, 370], fill=(60, 180, 100), outline=(80, 220, 120))
|
draw.ellipse([190, 220, 322, 370], fill=(60, 180, 100), outline=(80, 220, 120))
|
||||||
|
|
||||||
# Eyes
|
# Eyes (whites + pupils)
|
||||||
draw.ellipse([220, 275, 248, 310], fill=(255, 255, 255))
|
draw.ellipse([220, 275, 248, 310], fill=(255, 255, 255))
|
||||||
draw.ellipse([264, 275, 292, 310], fill=(255, 255, 255))
|
draw.ellipse([264, 275, 292, 310], fill=(255, 255, 255))
|
||||||
draw.ellipse([228, 285, 242, 300], fill=(30, 30, 60))
|
draw.ellipse([228, 285, 242, 300], fill=pupil)
|
||||||
draw.ellipse([272, 285, 286, 300], fill=(30, 30, 60))
|
draw.ellipse([272, 285, 286, 300], fill=pupil)
|
||||||
|
|
||||||
# Smile
|
# Smile
|
||||||
draw.arc([225, 300, 287, 355], start=10, end=170, fill=(30, 30, 60), width=3)
|
draw.arc([225, 300, 287, 355], start=10, end=170, fill=pupil, width=3)
|
||||||
|
|
||||||
# Stars around the hat
|
# "T" monogram on hat
|
||||||
|
draw.text((243, 100), "T", fill=gold)
|
||||||
|
|
||||||
|
# Robe
|
||||||
|
draw.polygon(
|
||||||
|
[(180, 370), (140, 500), (372, 500), (332, 370)],
|
||||||
|
fill=(40, 100, 70),
|
||||||
|
outline=(60, 160, 100),
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def _draw_stars(draw: ImageDraw.ImageDraw) -> None:
|
||||||
|
"""Draw decorative gold stars around the wizard hat."""
|
||||||
gold = (220, 190, 50)
|
gold = (220, 190, 50)
|
||||||
star_positions = [(120, 100), (380, 120), (100, 300), (400, 280), (256, 10)]
|
for sx, sy in [(120, 100), (380, 120), (100, 300), (400, 280), (256, 10)]:
|
||||||
for sx, sy in star_positions:
|
|
||||||
r = 8
|
r = 8
|
||||||
draw.polygon(
|
draw.polygon(
|
||||||
[
|
[
|
||||||
@@ -333,18 +350,26 @@ def _generate_avatar_image() -> bytes:
|
|||||||
fill=gold,
|
fill=gold,
|
||||||
)
|
)
|
||||||
|
|
||||||
# "T" monogram on the hat
|
|
||||||
draw.text((243, 100), "T", fill=gold)
|
|
||||||
|
|
||||||
# Robe / body
|
def _generate_avatar_image() -> bytes:
|
||||||
draw.polygon(
|
"""Generate a Timmy-themed avatar image using Pillow.
|
||||||
[(180, 370), (140, 500), (372, 500), (332, 370)],
|
|
||||||
fill=(40, 100, 70),
|
|
||||||
outline=(60, 160, 100),
|
|
||||||
)
|
|
||||||
|
|
||||||
|
Creates a 512x512 wizard-themed avatar with emerald/purple/gold palette.
|
||||||
|
Returns raw PNG bytes. Falls back to a minimal solid-color image if
|
||||||
|
Pillow drawing primitives fail.
|
||||||
|
"""
|
||||||
import io
|
import io
|
||||||
|
|
||||||
|
from PIL import Image, ImageDraw
|
||||||
|
|
||||||
|
size = 512
|
||||||
|
img = Image.new("RGB", (size, size), (15, 25, 20))
|
||||||
|
draw = ImageDraw.Draw(img)
|
||||||
|
|
||||||
|
_draw_background(draw, size)
|
||||||
|
_draw_wizard(draw)
|
||||||
|
_draw_stars(draw)
|
||||||
|
|
||||||
buf = io.BytesIO()
|
buf = io.BytesIO()
|
||||||
img.save(buf, format="PNG")
|
img.save(buf, format="PNG")
|
||||||
return buf.getvalue()
|
return buf.getvalue()
|
||||||
|
|||||||
@@ -78,23 +78,26 @@ def _migrate_schema(conn: sqlite3.Connection) -> None:
|
|||||||
cursor = conn.execute("SELECT name FROM sqlite_master WHERE type='table'")
|
cursor = conn.execute("SELECT name FROM sqlite_master WHERE type='table'")
|
||||||
tables = {row[0] for row in cursor.fetchall()}
|
tables = {row[0] for row in cursor.fetchall()}
|
||||||
|
|
||||||
has_memories = "memories" in tables
|
if "memories" not in tables:
|
||||||
has_episodes = "episodes" in tables
|
|
||||||
has_chunks = "chunks" in tables
|
|
||||||
has_facts = "facts" in tables
|
|
||||||
|
|
||||||
# Check if we need to migrate (old schema exists but new one doesn't fully)
|
|
||||||
if not has_memories:
|
|
||||||
logger.info("Migration: Creating unified memories table")
|
logger.info("Migration: Creating unified memories table")
|
||||||
# Schema will be created above
|
# Schema will be created by _ensure_schema above
|
||||||
|
conn.commit()
|
||||||
|
return
|
||||||
|
|
||||||
# Migrate episodes -> memories
|
_migrate_episodes(conn, tables)
|
||||||
if has_episodes and has_memories:
|
_migrate_chunks(conn, tables)
|
||||||
|
_drop_legacy_tables(conn, tables)
|
||||||
|
conn.commit()
|
||||||
|
|
||||||
|
|
||||||
|
def _migrate_episodes(conn: sqlite3.Connection, tables: set[str]) -> None:
|
||||||
|
"""Migrate episodes table rows into the unified memories table."""
|
||||||
|
if "episodes" not in tables:
|
||||||
|
return
|
||||||
logger.info("Migration: Converting episodes table to memories")
|
logger.info("Migration: Converting episodes table to memories")
|
||||||
try:
|
try:
|
||||||
cols = _get_table_columns(conn, "episodes")
|
cols = _get_table_columns(conn, "episodes")
|
||||||
context_type_col = "context_type" if "context_type" in cols else "'conversation'"
|
context_type_col = "context_type" if "context_type" in cols else "'conversation'"
|
||||||
|
|
||||||
conn.execute(f"""
|
conn.execute(f"""
|
||||||
INSERT INTO memories (
|
INSERT INTO memories (
|
||||||
id, content, memory_type, source, embedding,
|
id, content, memory_type, source, embedding,
|
||||||
@@ -115,12 +118,14 @@ def _migrate_schema(conn: sqlite3.Connection) -> None:
|
|||||||
except sqlite3.Error as exc:
|
except sqlite3.Error as exc:
|
||||||
logger.warning("Migration: Failed to migrate episodes: %s", exc)
|
logger.warning("Migration: Failed to migrate episodes: %s", exc)
|
||||||
|
|
||||||
# Migrate chunks -> memories as vault_chunk
|
|
||||||
if has_chunks and has_memories:
|
def _migrate_chunks(conn: sqlite3.Connection, tables: set[str]) -> None:
|
||||||
|
"""Migrate chunks table rows into the unified memories table as vault_chunk."""
|
||||||
|
if "chunks" not in tables:
|
||||||
|
return
|
||||||
logger.info("Migration: Converting chunks table to memories")
|
logger.info("Migration: Converting chunks table to memories")
|
||||||
try:
|
try:
|
||||||
cols = _get_table_columns(conn, "chunks")
|
cols = _get_table_columns(conn, "chunks")
|
||||||
|
|
||||||
id_col = "id" if "id" in cols else "CAST(rowid AS TEXT)"
|
id_col = "id" if "id" in cols else "CAST(rowid AS TEXT)"
|
||||||
content_col = "content" if "content" in cols else "text"
|
content_col = "content" if "content" in cols else "text"
|
||||||
source_col = (
|
source_col = (
|
||||||
@@ -128,7 +133,6 @@ def _migrate_schema(conn: sqlite3.Connection) -> None:
|
|||||||
)
|
)
|
||||||
embedding_col = "embedding" if "embedding" in cols else "NULL"
|
embedding_col = "embedding" if "embedding" in cols else "NULL"
|
||||||
created_col = "created_at" if "created_at" in cols else "datetime('now')"
|
created_col = "created_at" if "created_at" in cols else "datetime('now')"
|
||||||
|
|
||||||
conn.execute(f"""
|
conn.execute(f"""
|
||||||
INSERT INTO memories (
|
INSERT INTO memories (
|
||||||
id, content, memory_type, source, embedding,
|
id, content, memory_type, source, embedding,
|
||||||
@@ -144,16 +148,17 @@ def _migrate_schema(conn: sqlite3.Connection) -> None:
|
|||||||
except sqlite3.Error as exc:
|
except sqlite3.Error as exc:
|
||||||
logger.warning("Migration: Failed to migrate chunks: %s", exc)
|
logger.warning("Migration: Failed to migrate chunks: %s", exc)
|
||||||
|
|
||||||
# Drop old facts table
|
|
||||||
if has_facts:
|
def _drop_legacy_tables(conn: sqlite3.Connection, tables: set[str]) -> None:
|
||||||
|
"""Drop old facts table if it exists."""
|
||||||
|
if "facts" not in tables:
|
||||||
|
return
|
||||||
try:
|
try:
|
||||||
conn.execute("DROP TABLE facts")
|
conn.execute("DROP TABLE facts")
|
||||||
logger.info("Migration: Dropped old facts table")
|
logger.info("Migration: Dropped old facts table")
|
||||||
except sqlite3.Error as exc:
|
except sqlite3.Error as exc:
|
||||||
logger.warning("Migration: Failed to drop facts: %s", exc)
|
logger.warning("Migration: Failed to drop facts: %s", exc)
|
||||||
|
|
||||||
conn.commit()
|
|
||||||
|
|
||||||
|
|
||||||
def _get_table_columns(conn: sqlite3.Connection, table_name: str) -> set[str]:
|
def _get_table_columns(conn: sqlite3.Connection, table_name: str) -> set[str]:
|
||||||
"""Get the column names for a table."""
|
"""Get the column names for a table."""
|
||||||
|
|||||||
@@ -46,6 +46,64 @@ DB_PATH = PROJECT_ROOT / "data" / "memory.db"
|
|||||||
# ───────────────────────────────────────────────────────────────────────────────
|
# ───────────────────────────────────────────────────────────────────────────────
|
||||||
|
|
||||||
|
|
||||||
|
_DEFAULT_HOT_MEMORY_TEMPLATE = """\
|
||||||
|
# Timmy Hot Memory
|
||||||
|
|
||||||
|
> Working RAM — always loaded, ~300 lines max, pruned monthly
|
||||||
|
> Last updated: {date}
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Current Status
|
||||||
|
|
||||||
|
**Agent State:** Operational
|
||||||
|
**Mode:** Development
|
||||||
|
**Active Tasks:** 0
|
||||||
|
**Pending Decisions:** None
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Standing Rules
|
||||||
|
|
||||||
|
1. **Sovereignty First** — No cloud dependencies
|
||||||
|
2. **Local-Only Inference** — Ollama on localhost
|
||||||
|
3. **Privacy by Design** — Telemetry disabled
|
||||||
|
4. **Tool Minimalism** — Use tools only when necessary
|
||||||
|
5. **Memory Discipline** — Write handoffs at session end
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Agent Roster
|
||||||
|
|
||||||
|
| Agent | Role | Status |
|
||||||
|
|-------|------|--------|
|
||||||
|
| Timmy | Core | Active |
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## User Profile
|
||||||
|
|
||||||
|
**Name:** (not set)
|
||||||
|
**Interests:** (to be learned)
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Key Decisions
|
||||||
|
|
||||||
|
(none yet)
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Pending Actions
|
||||||
|
|
||||||
|
- [ ] Learn user's name
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
*Prune date: {prune_date}*
|
||||||
|
"""
|
||||||
|
|
||||||
|
|
||||||
@contextmanager
|
@contextmanager
|
||||||
def get_connection() -> Generator[sqlite3.Connection, None, None]:
|
def get_connection() -> Generator[sqlite3.Connection, None, None]:
|
||||||
"""Get database connection to unified memory database."""
|
"""Get database connection to unified memory database."""
|
||||||
@@ -732,66 +790,12 @@ class HotMemory:
|
|||||||
logger.debug(
|
logger.debug(
|
||||||
"HotMemory._create_default() - creating default MEMORY.md for backward compatibility"
|
"HotMemory._create_default() - creating default MEMORY.md for backward compatibility"
|
||||||
)
|
)
|
||||||
default_content = """# Timmy Hot Memory
|
now = datetime.now(UTC)
|
||||||
|
content = _DEFAULT_HOT_MEMORY_TEMPLATE.format(
|
||||||
> Working RAM — always loaded, ~300 lines max, pruned monthly
|
date=now.strftime("%Y-%m-%d"),
|
||||||
> Last updated: {date}
|
prune_date=now.replace(day=25).strftime("%Y-%m-%d"),
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
## Current Status
|
|
||||||
|
|
||||||
**Agent State:** Operational
|
|
||||||
**Mode:** Development
|
|
||||||
**Active Tasks:** 0
|
|
||||||
**Pending Decisions:** None
|
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
## Standing Rules
|
|
||||||
|
|
||||||
1. **Sovereignty First** — No cloud dependencies
|
|
||||||
2. **Local-Only Inference** — Ollama on localhost
|
|
||||||
3. **Privacy by Design** — Telemetry disabled
|
|
||||||
4. **Tool Minimalism** — Use tools only when necessary
|
|
||||||
5. **Memory Discipline** — Write handoffs at session end
|
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
## Agent Roster
|
|
||||||
|
|
||||||
| Agent | Role | Status |
|
|
||||||
|-------|------|--------|
|
|
||||||
| Timmy | Core | Active |
|
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
## User Profile
|
|
||||||
|
|
||||||
**Name:** (not set)
|
|
||||||
**Interests:** (to be learned)
|
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
## Key Decisions
|
|
||||||
|
|
||||||
(none yet)
|
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
## Pending Actions
|
|
||||||
|
|
||||||
- [ ] Learn user's name
|
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
*Prune date: {prune_date}*
|
|
||||||
""".format(
|
|
||||||
date=datetime.now(UTC).strftime("%Y-%m-%d"),
|
|
||||||
prune_date=(datetime.now(UTC).replace(day=25)).strftime("%Y-%m-%d"),
|
|
||||||
)
|
)
|
||||||
|
self.path.write_text(content)
|
||||||
self.path.write_text(default_content)
|
|
||||||
logger.info("HotMemory: Created default MEMORY.md")
|
logger.info("HotMemory: Created default MEMORY.md")
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -392,31 +392,26 @@ def _build_insights(
|
|||||||
return insights or ["Conversations look healthy. Keep up the good work."]
|
return insights or ["Conversations look healthy. Keep up the good work."]
|
||||||
|
|
||||||
|
|
||||||
def self_reflect(limit: int = 30) -> str:
|
def _format_recurring_topics(repeated: list[tuple[str, int]]) -> list[str]:
|
||||||
"""Review recent conversations and reflect on Timmy's own behavior.
|
"""Format the recurring-topics section of a reflection report."""
|
||||||
|
if repeated:
|
||||||
|
lines = ["### Recurring Topics"]
|
||||||
|
for word, count in repeated:
|
||||||
|
lines.append(f'- "{word}" ({count} mentions)')
|
||||||
|
lines.append("")
|
||||||
|
return lines
|
||||||
|
return ["### Recurring Topics\nNo strong patterns detected.\n"]
|
||||||
|
|
||||||
Scans past session entries for patterns: low-confidence responses,
|
|
||||||
errors, repeated topics, and conversation quality signals. Returns
|
|
||||||
a structured reflection that Timmy can use to improve.
|
|
||||||
|
|
||||||
Args:
|
def _assemble_report(
|
||||||
limit: How many recent entries to review (default 30).
|
entries: list[dict],
|
||||||
|
errors: list[dict],
|
||||||
Returns:
|
timmy_msgs: list[dict],
|
||||||
A formatted self-reflection report.
|
user_msgs: list[dict],
|
||||||
"""
|
low_conf: list[dict],
|
||||||
sl = get_session_logger()
|
repeated: list[tuple[str, int]],
|
||||||
sl.flush()
|
) -> str:
|
||||||
entries = sl.get_recent_entries(limit=limit)
|
"""Assemble the full self-reflection report from analyzed data."""
|
||||||
|
|
||||||
if not entries:
|
|
||||||
return "No conversation history to reflect on yet."
|
|
||||||
|
|
||||||
_messages, errors, timmy_msgs, user_msgs = _categorize_entries(entries)
|
|
||||||
low_conf = _find_low_confidence(timmy_msgs)
|
|
||||||
repeated = _find_repeated_topics(user_msgs)
|
|
||||||
|
|
||||||
# Build reflection report
|
|
||||||
sections: list[str] = ["## Self-Reflection Report\n"]
|
sections: list[str] = ["## Self-Reflection Report\n"]
|
||||||
sections.append(
|
sections.append(
|
||||||
f"Reviewed {len(entries)} recent entries: "
|
f"Reviewed {len(entries)} recent entries: "
|
||||||
@@ -446,16 +441,37 @@ def self_reflect(limit: int = 30) -> str:
|
|||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
if repeated:
|
sections.extend(_format_recurring_topics(repeated))
|
||||||
sections.append("### Recurring Topics")
|
|
||||||
for word, count in repeated:
|
|
||||||
sections.append(f'- "{word}" ({count} mentions)')
|
|
||||||
sections.append("")
|
|
||||||
else:
|
|
||||||
sections.append("### Recurring Topics\nNo strong patterns detected.\n")
|
|
||||||
|
|
||||||
sections.append("### Insights")
|
sections.append("### Insights")
|
||||||
for insight in _build_insights(low_conf, errors, repeated):
|
for insight in _build_insights(low_conf, errors, repeated):
|
||||||
sections.append(f"- {insight}")
|
sections.append(f"- {insight}")
|
||||||
|
|
||||||
return "\n".join(sections)
|
return "\n".join(sections)
|
||||||
|
|
||||||
|
|
||||||
|
def self_reflect(limit: int = 30) -> str:
|
||||||
|
"""Review recent conversations and reflect on Timmy's own behavior.
|
||||||
|
|
||||||
|
Scans past session entries for patterns: low-confidence responses,
|
||||||
|
errors, repeated topics, and conversation quality signals. Returns
|
||||||
|
a structured reflection that Timmy can use to improve.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
limit: How many recent entries to review (default 30).
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
A formatted self-reflection report.
|
||||||
|
"""
|
||||||
|
sl = get_session_logger()
|
||||||
|
sl.flush()
|
||||||
|
entries = sl.get_recent_entries(limit=limit)
|
||||||
|
|
||||||
|
if not entries:
|
||||||
|
return "No conversation history to reflect on yet."
|
||||||
|
|
||||||
|
_messages, errors, timmy_msgs, user_msgs = _categorize_entries(entries)
|
||||||
|
low_conf = _find_low_confidence(timmy_msgs)
|
||||||
|
repeated = _find_repeated_topics(user_msgs)
|
||||||
|
|
||||||
|
return _assemble_report(entries, errors, timmy_msgs, user_msgs, low_conf, repeated)
|
||||||
|
|||||||
@@ -341,6 +341,11 @@ class ThinkingEngine:
|
|||||||
)
|
)
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
# Capture arrival time *before* the LLM call so the thought
|
||||||
|
# timestamp reflects when the cycle started, not when the
|
||||||
|
# (potentially slow) generation finished. Fixes #582.
|
||||||
|
arrived_at = datetime.now(UTC).isoformat()
|
||||||
|
|
||||||
memory_context, system_context, recent_thoughts = self._build_thinking_context()
|
memory_context, system_context, recent_thoughts = self._build_thinking_context()
|
||||||
|
|
||||||
content, seed_type = await self._generate_novel_thought(
|
content, seed_type = await self._generate_novel_thought(
|
||||||
@@ -352,7 +357,7 @@ class ThinkingEngine:
|
|||||||
if not content:
|
if not content:
|
||||||
return None
|
return None
|
||||||
|
|
||||||
thought = self._store_thought(content, seed_type)
|
thought = self._store_thought(content, seed_type, arrived_at=arrived_at)
|
||||||
self._last_thought_id = thought.id
|
self._last_thought_id = thought.id
|
||||||
|
|
||||||
await self._process_thinking_result(thought)
|
await self._process_thinking_result(thought)
|
||||||
@@ -1173,14 +1178,25 @@ class ThinkingEngine:
|
|||||||
raw = run.content if hasattr(run, "content") else str(run)
|
raw = run.content if hasattr(run, "content") else str(run)
|
||||||
return _THINK_TAG_RE.sub("", raw) if raw else raw
|
return _THINK_TAG_RE.sub("", raw) if raw else raw
|
||||||
|
|
||||||
def _store_thought(self, content: str, seed_type: str) -> Thought:
|
def _store_thought(
|
||||||
"""Persist a thought to SQLite."""
|
self,
|
||||||
|
content: str,
|
||||||
|
seed_type: str,
|
||||||
|
*,
|
||||||
|
arrived_at: str | None = None,
|
||||||
|
) -> Thought:
|
||||||
|
"""Persist a thought to SQLite.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
arrived_at: ISO-8601 timestamp captured when the thinking cycle
|
||||||
|
started. Falls back to now() for callers that don't supply it.
|
||||||
|
"""
|
||||||
thought = Thought(
|
thought = Thought(
|
||||||
id=str(uuid.uuid4()),
|
id=str(uuid.uuid4()),
|
||||||
content=content,
|
content=content,
|
||||||
seed_type=seed_type,
|
seed_type=seed_type,
|
||||||
parent_id=self._last_thought_id,
|
parent_id=self._last_thought_id,
|
||||||
created_at=datetime.now(UTC).isoformat(),
|
created_at=arrived_at or datetime.now(UTC).isoformat(),
|
||||||
)
|
)
|
||||||
|
|
||||||
with _get_conn(self._db_path) as conn:
|
with _get_conn(self._db_path) as conn:
|
||||||
@@ -1261,6 +1277,53 @@ class ThinkingEngine:
|
|||||||
logger.debug("Failed to broadcast thought: %s", exc)
|
logger.debug("Failed to broadcast thought: %s", exc)
|
||||||
|
|
||||||
|
|
||||||
|
def _query_thoughts(
|
||||||
|
db_path: Path, query: str, seed_type: str | None, limit: int
|
||||||
|
) -> list[sqlite3.Row]:
|
||||||
|
"""Run the thought-search SQL and return matching rows."""
|
||||||
|
pattern = f"%{query}%"
|
||||||
|
with _get_conn(db_path) as conn:
|
||||||
|
if seed_type:
|
||||||
|
return conn.execute(
|
||||||
|
"""
|
||||||
|
SELECT id, content, seed_type, created_at
|
||||||
|
FROM thoughts
|
||||||
|
WHERE content LIKE ? AND seed_type = ?
|
||||||
|
ORDER BY created_at DESC
|
||||||
|
LIMIT ?
|
||||||
|
""",
|
||||||
|
(pattern, seed_type, limit),
|
||||||
|
).fetchall()
|
||||||
|
return conn.execute(
|
||||||
|
"""
|
||||||
|
SELECT id, content, seed_type, created_at
|
||||||
|
FROM thoughts
|
||||||
|
WHERE content LIKE ?
|
||||||
|
ORDER BY created_at DESC
|
||||||
|
LIMIT ?
|
||||||
|
""",
|
||||||
|
(pattern, limit),
|
||||||
|
).fetchall()
|
||||||
|
|
||||||
|
|
||||||
|
def _format_thought_rows(rows: list[sqlite3.Row], query: str, seed_type: str | None) -> str:
|
||||||
|
"""Format thought rows into a human-readable string."""
|
||||||
|
lines = [f'Found {len(rows)} thought(s) matching "{query}":']
|
||||||
|
if seed_type:
|
||||||
|
lines[0] += f' [seed_type="{seed_type}"]'
|
||||||
|
lines.append("")
|
||||||
|
|
||||||
|
for row in rows:
|
||||||
|
ts = datetime.fromisoformat(row["created_at"])
|
||||||
|
local_ts = ts.astimezone()
|
||||||
|
time_str = local_ts.strftime("%Y-%m-%d %I:%M %p").lstrip("0")
|
||||||
|
seed = row["seed_type"]
|
||||||
|
content = row["content"].replace("\n", " ") # Flatten newlines for display
|
||||||
|
lines.append(f"[{time_str}] ({seed}) {content[:150]}")
|
||||||
|
|
||||||
|
return "\n".join(lines)
|
||||||
|
|
||||||
|
|
||||||
def search_thoughts(query: str, seed_type: str | None = None, limit: int = 10) -> str:
|
def search_thoughts(query: str, seed_type: str | None = None, limit: int = 10) -> str:
|
||||||
"""Search Timmy's thought history for reflections matching a query.
|
"""Search Timmy's thought history for reflections matching a query.
|
||||||
|
|
||||||
@@ -1278,58 +1341,17 @@ def search_thoughts(query: str, seed_type: str | None = None, limit: int = 10) -
|
|||||||
Formatted string with matching thoughts, newest first, including
|
Formatted string with matching thoughts, newest first, including
|
||||||
timestamps and seed types. Returns a helpful message if no matches found.
|
timestamps and seed types. Returns a helpful message if no matches found.
|
||||||
"""
|
"""
|
||||||
# Clamp limit to reasonable bounds
|
|
||||||
limit = max(1, min(limit, 50))
|
limit = max(1, min(limit, 50))
|
||||||
|
|
||||||
try:
|
try:
|
||||||
engine = thinking_engine
|
rows = _query_thoughts(thinking_engine._db_path, query, seed_type, limit)
|
||||||
db_path = engine._db_path
|
|
||||||
|
|
||||||
# Build query with optional seed_type filter
|
|
||||||
with _get_conn(db_path) as conn:
|
|
||||||
if seed_type:
|
|
||||||
rows = conn.execute(
|
|
||||||
"""
|
|
||||||
SELECT id, content, seed_type, created_at
|
|
||||||
FROM thoughts
|
|
||||||
WHERE content LIKE ? AND seed_type = ?
|
|
||||||
ORDER BY created_at DESC
|
|
||||||
LIMIT ?
|
|
||||||
""",
|
|
||||||
(f"%{query}%", seed_type, limit),
|
|
||||||
).fetchall()
|
|
||||||
else:
|
|
||||||
rows = conn.execute(
|
|
||||||
"""
|
|
||||||
SELECT id, content, seed_type, created_at
|
|
||||||
FROM thoughts
|
|
||||||
WHERE content LIKE ?
|
|
||||||
ORDER BY created_at DESC
|
|
||||||
LIMIT ?
|
|
||||||
""",
|
|
||||||
(f"%{query}%", limit),
|
|
||||||
).fetchall()
|
|
||||||
|
|
||||||
if not rows:
|
if not rows:
|
||||||
if seed_type:
|
if seed_type:
|
||||||
return f'No thoughts found matching "{query}" with seed_type="{seed_type}".'
|
return f'No thoughts found matching "{query}" with seed_type="{seed_type}".'
|
||||||
return f'No thoughts found matching "{query}".'
|
return f'No thoughts found matching "{query}".'
|
||||||
|
|
||||||
# Format results
|
return _format_thought_rows(rows, query, seed_type)
|
||||||
lines = [f'Found {len(rows)} thought(s) matching "{query}":']
|
|
||||||
if seed_type:
|
|
||||||
lines[0] += f' [seed_type="{seed_type}"]'
|
|
||||||
lines.append("")
|
|
||||||
|
|
||||||
for row in rows:
|
|
||||||
ts = datetime.fromisoformat(row["created_at"])
|
|
||||||
local_ts = ts.astimezone()
|
|
||||||
time_str = local_ts.strftime("%Y-%m-%d %I:%M %p").lstrip("0")
|
|
||||||
seed = row["seed_type"]
|
|
||||||
content = row["content"].replace("\n", " ") # Flatten newlines for display
|
|
||||||
lines.append(f"[{time_str}] ({seed}) {content[:150]}")
|
|
||||||
|
|
||||||
return "\n".join(lines)
|
|
||||||
|
|
||||||
except Exception as exc:
|
except Exception as exc:
|
||||||
logger.warning("Thought search failed: %s", exc)
|
logger.warning("Thought search failed: %s", exc)
|
||||||
|
|||||||
@@ -89,45 +89,31 @@ def list_swarm_agents() -> dict[str, Any]:
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
def delegate_to_kimi(task: str, working_directory: str = "") -> dict[str, Any]:
|
def _find_kimi_cli() -> str | None:
|
||||||
"""Delegate a coding task to Kimi, the external coding agent.
|
"""Return the path to the kimi CLI binary, or None if not installed."""
|
||||||
|
|
||||||
Kimi has 262K context and is optimized for code tasks: writing,
|
|
||||||
debugging, refactoring, test writing. Timmy thinks and plans,
|
|
||||||
Kimi executes bulk code changes.
|
|
||||||
|
|
||||||
Args:
|
|
||||||
task: Clear, specific coding task description. Include file paths
|
|
||||||
and expected behavior. Good: "Fix the bug in src/timmy/session.py
|
|
||||||
where sessions don't persist." Bad: "Fix all bugs."
|
|
||||||
working_directory: Directory for Kimi to work in. Defaults to repo root.
|
|
||||||
|
|
||||||
Returns:
|
|
||||||
Dict with success status and Kimi's output or error.
|
|
||||||
"""
|
|
||||||
import shutil
|
import shutil
|
||||||
import subprocess
|
|
||||||
|
return shutil.which("kimi")
|
||||||
|
|
||||||
|
|
||||||
|
def _resolve_workdir(working_directory: str) -> str | dict[str, Any]:
|
||||||
|
"""Return a validated working directory path, or an error dict."""
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
|
||||||
from config import settings
|
from config import settings
|
||||||
|
|
||||||
kimi_path = shutil.which("kimi")
|
|
||||||
if not kimi_path:
|
|
||||||
return {
|
|
||||||
"success": False,
|
|
||||||
"error": "kimi CLI not found on PATH. Install with: pip install kimi-cli",
|
|
||||||
}
|
|
||||||
|
|
||||||
workdir = working_directory or settings.repo_root
|
workdir = working_directory or settings.repo_root
|
||||||
if not Path(workdir).is_dir():
|
if not Path(workdir).is_dir():
|
||||||
return {
|
return {
|
||||||
"success": False,
|
"success": False,
|
||||||
"error": f"Working directory does not exist: {workdir}",
|
"error": f"Working directory does not exist: {workdir}",
|
||||||
}
|
}
|
||||||
|
return workdir
|
||||||
|
|
||||||
cmd = [kimi_path, "--print", "-p", task]
|
|
||||||
|
|
||||||
logger.info("Delegating to Kimi: %s (cwd=%s)", task[:80], workdir)
|
def _run_kimi(cmd: list[str], workdir: str) -> dict[str, Any]:
|
||||||
|
"""Execute the kimi subprocess and return a result dict."""
|
||||||
|
import subprocess
|
||||||
|
|
||||||
try:
|
try:
|
||||||
result = subprocess.run(
|
result = subprocess.run(
|
||||||
@@ -157,3 +143,34 @@ def delegate_to_kimi(task: str, working_directory: str = "") -> dict[str, Any]:
|
|||||||
"success": False,
|
"success": False,
|
||||||
"error": f"Failed to run Kimi: {exc}",
|
"error": f"Failed to run Kimi: {exc}",
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
def delegate_to_kimi(task: str, working_directory: str = "") -> dict[str, Any]:
|
||||||
|
"""Delegate a coding task to Kimi, the external coding agent.
|
||||||
|
|
||||||
|
Kimi has 262K context and is optimized for code tasks: writing,
|
||||||
|
debugging, refactoring, test writing. Timmy thinks and plans,
|
||||||
|
Kimi executes bulk code changes.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
task: Clear, specific coding task description. Include file paths
|
||||||
|
and expected behavior. Good: "Fix the bug in src/timmy/session.py
|
||||||
|
where sessions don't persist." Bad: "Fix all bugs."
|
||||||
|
working_directory: Directory for Kimi to work in. Defaults to repo root.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
Dict with success status and Kimi's output or error.
|
||||||
|
"""
|
||||||
|
kimi_path = _find_kimi_cli()
|
||||||
|
if not kimi_path:
|
||||||
|
return {
|
||||||
|
"success": False,
|
||||||
|
"error": "kimi CLI not found on PATH. Install with: pip install kimi-cli",
|
||||||
|
}
|
||||||
|
|
||||||
|
workdir = _resolve_workdir(working_directory)
|
||||||
|
if isinstance(workdir, dict):
|
||||||
|
return workdir
|
||||||
|
|
||||||
|
logger.info("Delegating to Kimi: %s (cwd=%s)", task[:80], workdir)
|
||||||
|
return _run_kimi([kimi_path, "--print", "-p", task], workdir)
|
||||||
|
|||||||
@@ -326,6 +326,46 @@ def get_live_system_status() -> dict[str, Any]:
|
|||||||
return result
|
return result
|
||||||
|
|
||||||
|
|
||||||
|
def _build_pytest_cmd(venv_python: Path, scope: str) -> list[str]:
|
||||||
|
"""Build the pytest command list for the given scope."""
|
||||||
|
cmd = [str(venv_python), "-m", "pytest", "-x", "-q", "--tb=short", "--timeout=30"]
|
||||||
|
|
||||||
|
if scope == "fast":
|
||||||
|
cmd.extend(
|
||||||
|
[
|
||||||
|
"--ignore=tests/functional",
|
||||||
|
"--ignore=tests/e2e",
|
||||||
|
"--ignore=tests/integrations",
|
||||||
|
"tests/",
|
||||||
|
]
|
||||||
|
)
|
||||||
|
elif scope == "full":
|
||||||
|
cmd.append("tests/")
|
||||||
|
else:
|
||||||
|
cmd.append(scope)
|
||||||
|
|
||||||
|
return cmd
|
||||||
|
|
||||||
|
|
||||||
|
def _parse_pytest_output(output: str) -> dict[str, int]:
|
||||||
|
"""Extract passed/failed/error counts from pytest output."""
|
||||||
|
import re
|
||||||
|
|
||||||
|
passed = failed = errors = 0
|
||||||
|
for line in output.splitlines():
|
||||||
|
if "passed" in line or "failed" in line or "error" in line:
|
||||||
|
nums = re.findall(r"(\d+) (passed|failed|error)", line)
|
||||||
|
for count, kind in nums:
|
||||||
|
if kind == "passed":
|
||||||
|
passed = int(count)
|
||||||
|
elif kind == "failed":
|
||||||
|
failed = int(count)
|
||||||
|
elif kind == "error":
|
||||||
|
errors = int(count)
|
||||||
|
|
||||||
|
return {"passed": passed, "failed": failed, "errors": errors}
|
||||||
|
|
||||||
|
|
||||||
def run_self_tests(scope: str = "fast", _repo_root: str | None = None) -> dict[str, Any]:
|
def run_self_tests(scope: str = "fast", _repo_root: str | None = None) -> dict[str, Any]:
|
||||||
"""Run Timmy's own test suite and report results.
|
"""Run Timmy's own test suite and report results.
|
||||||
|
|
||||||
@@ -349,49 +389,17 @@ def run_self_tests(scope: str = "fast", _repo_root: str | None = None) -> dict[s
|
|||||||
if not venv_python.exists():
|
if not venv_python.exists():
|
||||||
return {"success": False, "error": f"No venv found at {venv_python}"}
|
return {"success": False, "error": f"No venv found at {venv_python}"}
|
||||||
|
|
||||||
cmd = [str(venv_python), "-m", "pytest", "-x", "-q", "--tb=short", "--timeout=30"]
|
cmd = _build_pytest_cmd(venv_python, scope)
|
||||||
|
|
||||||
if scope == "fast":
|
|
||||||
# Unit tests only — skip functional/e2e/integration
|
|
||||||
cmd.extend(
|
|
||||||
[
|
|
||||||
"--ignore=tests/functional",
|
|
||||||
"--ignore=tests/e2e",
|
|
||||||
"--ignore=tests/integrations",
|
|
||||||
"tests/",
|
|
||||||
]
|
|
||||||
)
|
|
||||||
elif scope == "full":
|
|
||||||
cmd.append("tests/")
|
|
||||||
else:
|
|
||||||
# Specific path
|
|
||||||
cmd.append(scope)
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
result = subprocess.run(cmd, capture_output=True, text=True, timeout=120, cwd=repo)
|
result = subprocess.run(cmd, capture_output=True, text=True, timeout=120, cwd=repo)
|
||||||
output = result.stdout + result.stderr
|
output = result.stdout + result.stderr
|
||||||
|
counts = _parse_pytest_output(output)
|
||||||
# Parse pytest output for counts
|
|
||||||
passed = failed = errors = 0
|
|
||||||
for line in output.splitlines():
|
|
||||||
if "passed" in line or "failed" in line or "error" in line:
|
|
||||||
import re
|
|
||||||
|
|
||||||
nums = re.findall(r"(\d+) (passed|failed|error)", line)
|
|
||||||
for count, kind in nums:
|
|
||||||
if kind == "passed":
|
|
||||||
passed = int(count)
|
|
||||||
elif kind == "failed":
|
|
||||||
failed = int(count)
|
|
||||||
elif kind == "error":
|
|
||||||
errors = int(count)
|
|
||||||
|
|
||||||
return {
|
return {
|
||||||
"success": result.returncode == 0,
|
"success": result.returncode == 0,
|
||||||
"passed": passed,
|
**counts,
|
||||||
"failed": failed,
|
"total": counts["passed"] + counts["failed"] + counts["errors"],
|
||||||
"errors": errors,
|
|
||||||
"total": passed + failed + errors,
|
|
||||||
"return_code": result.returncode,
|
"return_code": result.returncode,
|
||||||
"summary": output[-2000:] if len(output) > 2000 else output,
|
"summary": output[-2000:] if len(output) > 2000 else output,
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -86,6 +86,40 @@ def _pip_snapshot(mood: str, confidence: float) -> dict:
|
|||||||
return pip_familiar.snapshot().to_dict()
|
return pip_familiar.snapshot().to_dict()
|
||||||
|
|
||||||
|
|
||||||
|
def _resolve_mood(state) -> str:
|
||||||
|
"""Map cognitive mood/engagement to a presence mood string."""
|
||||||
|
if state.engagement == "idle" and state.mood == "settled":
|
||||||
|
return "calm"
|
||||||
|
return _MOOD_MAP.get(state.mood, "calm")
|
||||||
|
|
||||||
|
|
||||||
|
def _resolve_confidence(state) -> float:
|
||||||
|
"""Compute normalised confidence from cognitive tracker state."""
|
||||||
|
if state._confidence_count > 0:
|
||||||
|
raw = state._confidence_sum / state._confidence_count
|
||||||
|
else:
|
||||||
|
raw = 0.7
|
||||||
|
return round(max(0.0, min(1.0, raw)), 2)
|
||||||
|
|
||||||
|
|
||||||
|
def _build_active_threads(state) -> list[dict]:
|
||||||
|
"""Convert active commitments into presence thread dicts."""
|
||||||
|
return [
|
||||||
|
{"type": "thinking", "ref": c[:80], "status": "active"}
|
||||||
|
for c in state.active_commitments[:10]
|
||||||
|
]
|
||||||
|
|
||||||
|
|
||||||
|
def _build_environment() -> dict:
|
||||||
|
"""Return the environment section using local wall-clock time."""
|
||||||
|
local_now = datetime.now()
|
||||||
|
return {
|
||||||
|
"time_of_day": _time_of_day(local_now.hour),
|
||||||
|
"local_time": local_now.strftime("%-I:%M %p"),
|
||||||
|
"day_of_week": local_now.strftime("%A"),
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
def get_state_dict() -> dict:
|
def get_state_dict() -> dict:
|
||||||
"""Build presence state dict from current cognitive state.
|
"""Build presence state dict from current cognitive state.
|
||||||
|
|
||||||
@@ -98,37 +132,19 @@ def get_state_dict() -> dict:
|
|||||||
state = cognitive_tracker.get_state()
|
state = cognitive_tracker.get_state()
|
||||||
now = datetime.now(UTC)
|
now = datetime.now(UTC)
|
||||||
|
|
||||||
# Map cognitive mood to presence mood
|
mood = _resolve_mood(state)
|
||||||
mood = _MOOD_MAP.get(state.mood, "calm")
|
confidence = _resolve_confidence(state)
|
||||||
if state.engagement == "idle" and state.mood == "settled":
|
|
||||||
mood = "calm"
|
|
||||||
|
|
||||||
# Confidence from cognitive tracker
|
|
||||||
if state._confidence_count > 0:
|
|
||||||
confidence = state._confidence_sum / state._confidence_count
|
|
||||||
else:
|
|
||||||
confidence = 0.7
|
|
||||||
|
|
||||||
# Build active threads from commitments
|
|
||||||
threads = []
|
|
||||||
for commitment in state.active_commitments[:10]:
|
|
||||||
threads.append({"type": "thinking", "ref": commitment[:80], "status": "active"})
|
|
||||||
|
|
||||||
# Activity
|
|
||||||
activity = _ACTIVITY_MAP.get(state.engagement, "idle")
|
activity = _ACTIVITY_MAP.get(state.engagement, "idle")
|
||||||
|
|
||||||
# Environment
|
|
||||||
local_now = datetime.now()
|
|
||||||
|
|
||||||
return {
|
return {
|
||||||
"version": 1,
|
"version": 1,
|
||||||
"liveness": now.strftime("%Y-%m-%dT%H:%M:%SZ"),
|
"liveness": now.strftime("%Y-%m-%dT%H:%M:%SZ"),
|
||||||
"current_focus": state.focus_topic or "",
|
"current_focus": state.focus_topic or "",
|
||||||
"active_threads": threads,
|
"active_threads": _build_active_threads(state),
|
||||||
"recent_events": [],
|
"recent_events": [],
|
||||||
"concerns": [],
|
"concerns": [],
|
||||||
"mood": mood,
|
"mood": mood,
|
||||||
"confidence": round(max(0.0, min(1.0, confidence)), 2),
|
"confidence": confidence,
|
||||||
"energy": round(_current_energy(), 2),
|
"energy": round(_current_energy(), 2),
|
||||||
"identity": {
|
"identity": {
|
||||||
"name": "Timmy",
|
"name": "Timmy",
|
||||||
@@ -143,11 +159,7 @@ def get_state_dict() -> dict:
|
|||||||
"visitor_present": False,
|
"visitor_present": False,
|
||||||
"conversation_turns": state.conversation_depth,
|
"conversation_turns": state.conversation_depth,
|
||||||
},
|
},
|
||||||
"environment": {
|
"environment": _build_environment(),
|
||||||
"time_of_day": _time_of_day(local_now.hour),
|
|
||||||
"local_time": local_now.strftime("%-I:%M %p"),
|
|
||||||
"day_of_week": local_now.strftime("%A"),
|
|
||||||
},
|
|
||||||
"familiar": _pip_snapshot(mood, confidence),
|
"familiar": _pip_snapshot(mood, confidence),
|
||||||
"meta": {
|
"meta": {
|
||||||
"schema_version": 1,
|
"schema_version": 1,
|
||||||
|
|||||||
@@ -2493,3 +2493,57 @@
|
|||||||
.db-cell { max-width: 300px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
|
.db-cell { max-width: 300px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
|
||||||
.db-cell:hover { white-space: normal; word-break: break-all; }
|
.db-cell:hover { white-space: normal; word-break: break-all; }
|
||||||
.db-truncated { font-size: 0.7rem; color: var(--amber); padding: 0.3rem 0; }
|
.db-truncated { font-size: 0.7rem; color: var(--amber); padding: 0.3rem 0; }
|
||||||
|
|
||||||
|
/* ── Tower ────────────────────────────────────────────────────────────── */
|
||||||
|
.tower-container { max-width: 1400px; margin: 0 auto; }
|
||||||
|
.tower-header { margin-bottom: 1rem; }
|
||||||
|
.tower-title { font-size: 1.6rem; font-weight: 700; color: var(--green); letter-spacing: 0.15em; }
|
||||||
|
.tower-subtitle { font-size: 0.85rem; color: var(--text-dim); }
|
||||||
|
|
||||||
|
.tower-conn-badge { font-size: 0.7rem; font-weight: 600; padding: 2px 8px; border-radius: 3px; letter-spacing: 0.08em; }
|
||||||
|
.tower-conn-live { color: var(--green); border: 1px solid var(--green); }
|
||||||
|
.tower-conn-offline { color: var(--red); border: 1px solid var(--red); }
|
||||||
|
.tower-conn-connecting { color: var(--amber); border: 1px solid var(--amber); }
|
||||||
|
|
||||||
|
.tower-phase-card { min-height: 300px; }
|
||||||
|
.tower-phase-thinking { border-left: 3px solid var(--purple); }
|
||||||
|
.tower-phase-predicting { border-left: 3px solid var(--orange); }
|
||||||
|
.tower-phase-advising { border-left: 3px solid var(--green); }
|
||||||
|
.tower-scroll { max-height: 50vh; overflow-y: auto; }
|
||||||
|
.tower-empty { text-align: center; color: var(--text-dim); padding: 16px; font-size: 0.85rem; }
|
||||||
|
|
||||||
|
.tower-stat-grid { display: grid; grid-template-columns: repeat(4, 1fr); gap: 0.5rem; text-align: center; }
|
||||||
|
.tower-stat-label { display: block; font-size: 0.65rem; color: var(--text-dim); letter-spacing: 0.1em; }
|
||||||
|
.tower-stat-value { display: block; font-size: 1.1rem; font-weight: 700; color: var(--text-bright); }
|
||||||
|
|
||||||
|
.tower-event { padding: 8px; margin-bottom: 6px; border-left: 3px solid var(--border); border-radius: 3px; background: var(--bg-card); }
|
||||||
|
.tower-etype-task_posted { border-left-color: var(--purple); }
|
||||||
|
.tower-etype-bid_submitted { border-left-color: var(--orange); }
|
||||||
|
.tower-etype-task_completed { border-left-color: var(--green); }
|
||||||
|
.tower-etype-task_failed { border-left-color: var(--red); }
|
||||||
|
.tower-etype-agent_joined { border-left-color: var(--purple); }
|
||||||
|
.tower-etype-tool_executed { border-left-color: var(--amber); }
|
||||||
|
.tower-ev-head { display: flex; justify-content: space-between; align-items: center; margin-bottom: 4px; }
|
||||||
|
.tower-ev-badge { font-size: 0.65rem; font-weight: 600; color: var(--text-bright); letter-spacing: 0.08em; }
|
||||||
|
.tower-ev-dots { font-size: 0.6rem; color: var(--amber); }
|
||||||
|
.tower-ev-desc { font-size: 0.8rem; color: var(--text); }
|
||||||
|
.tower-ev-time { font-size: 0.65rem; color: var(--text-dim); margin-top: 2px; }
|
||||||
|
|
||||||
|
.tower-pred { padding: 8px; margin-bottom: 6px; border-radius: 3px; background: var(--bg-card); border-left: 3px solid var(--orange); }
|
||||||
|
.tower-pred-done { border-left-color: var(--green); }
|
||||||
|
.tower-pred-pending { border-left-color: var(--amber); }
|
||||||
|
.tower-pred-head { display: flex; justify-content: space-between; align-items: center; }
|
||||||
|
.tower-pred-task { font-size: 0.75rem; font-weight: 600; color: var(--text-bright); font-family: monospace; }
|
||||||
|
.tower-pred-acc { font-size: 0.75rem; font-weight: 700; }
|
||||||
|
.tower-pred-detail { font-size: 0.75rem; color: var(--text-dim); margin-top: 4px; }
|
||||||
|
|
||||||
|
.tower-advisory { padding: 8px; margin-bottom: 6px; border-radius: 3px; background: var(--bg-card); border-left: 3px solid var(--border); }
|
||||||
|
.tower-adv-high { border-left-color: var(--red); }
|
||||||
|
.tower-adv-medium { border-left-color: var(--orange); }
|
||||||
|
.tower-adv-low { border-left-color: var(--green); }
|
||||||
|
.tower-adv-head { display: flex; justify-content: space-between; font-size: 0.65rem; margin-bottom: 4px; }
|
||||||
|
.tower-adv-cat { font-weight: 600; color: var(--text-dim); letter-spacing: 0.08em; }
|
||||||
|
.tower-adv-prio { font-weight: 700; color: var(--amber); }
|
||||||
|
.tower-adv-title { font-size: 0.85rem; font-weight: 600; color: var(--text-bright); }
|
||||||
|
.tower-adv-detail { font-size: 0.8rem; color: var(--text); margin-top: 2px; }
|
||||||
|
.tower-adv-action { font-size: 0.75rem; color: var(--green); margin-top: 4px; font-style: italic; }
|
||||||
|
|||||||
100
tests/dashboard/middleware/test_csrf_no_side_effects.py
Normal file
100
tests/dashboard/middleware/test_csrf_no_side_effects.py
Normal file
@@ -0,0 +1,100 @@
|
|||||||
|
"""Tests that CSRF rejection does NOT execute the endpoint handler.
|
||||||
|
|
||||||
|
Regression test for #626: the middleware was calling call_next() before
|
||||||
|
checking @csrf_exempt, causing side effects even on CSRF-rejected requests.
|
||||||
|
"""
|
||||||
|
|
||||||
|
import pytest
|
||||||
|
from fastapi import FastAPI
|
||||||
|
from fastapi.testclient import TestClient
|
||||||
|
|
||||||
|
from dashboard.middleware.csrf import CSRFMiddleware, csrf_exempt
|
||||||
|
|
||||||
|
|
||||||
|
class TestCSRFNoSideEffects:
|
||||||
|
"""Verify endpoints are NOT executed when CSRF validation fails."""
|
||||||
|
|
||||||
|
@pytest.fixture(autouse=True)
|
||||||
|
def enable_csrf(self):
|
||||||
|
"""Re-enable CSRF for these tests."""
|
||||||
|
from config import settings
|
||||||
|
|
||||||
|
original = settings.timmy_disable_csrf
|
||||||
|
settings.timmy_disable_csrf = False
|
||||||
|
yield
|
||||||
|
settings.timmy_disable_csrf = original
|
||||||
|
|
||||||
|
def test_protected_endpoint_not_executed_on_csrf_failure(self):
|
||||||
|
"""A protected endpoint must NOT run when CSRF token is missing.
|
||||||
|
|
||||||
|
Before the fix, the middleware called call_next() to resolve the
|
||||||
|
endpoint, executing its side effects before returning 403.
|
||||||
|
"""
|
||||||
|
app = FastAPI()
|
||||||
|
app.add_middleware(CSRFMiddleware)
|
||||||
|
|
||||||
|
side_effect_log = []
|
||||||
|
|
||||||
|
@app.post("/transfer")
|
||||||
|
def transfer_money():
|
||||||
|
side_effect_log.append("money_transferred")
|
||||||
|
return {"message": "transferred"}
|
||||||
|
|
||||||
|
client = TestClient(app)
|
||||||
|
response = client.post("/transfer")
|
||||||
|
|
||||||
|
assert response.status_code == 403
|
||||||
|
assert side_effect_log == [], (
|
||||||
|
"Endpoint was executed despite CSRF failure — side effects occurred!"
|
||||||
|
)
|
||||||
|
|
||||||
|
def test_csrf_exempt_endpoint_still_executes(self):
|
||||||
|
"""A @csrf_exempt endpoint should still execute without a CSRF token."""
|
||||||
|
app = FastAPI()
|
||||||
|
app.add_middleware(CSRFMiddleware)
|
||||||
|
|
||||||
|
side_effect_log = []
|
||||||
|
|
||||||
|
@app.post("/webhook-handler")
|
||||||
|
@csrf_exempt
|
||||||
|
def webhook_handler():
|
||||||
|
side_effect_log.append("webhook_processed")
|
||||||
|
return {"message": "processed"}
|
||||||
|
|
||||||
|
client = TestClient(app)
|
||||||
|
response = client.post("/webhook-handler")
|
||||||
|
|
||||||
|
assert response.status_code == 200
|
||||||
|
assert side_effect_log == ["webhook_processed"]
|
||||||
|
|
||||||
|
def test_exempt_and_protected_no_cross_contamination(self):
|
||||||
|
"""Mixed exempt/protected: only exempt endpoints execute without tokens."""
|
||||||
|
app = FastAPI()
|
||||||
|
app.add_middleware(CSRFMiddleware)
|
||||||
|
|
||||||
|
execution_log = []
|
||||||
|
|
||||||
|
@app.post("/safe-webhook")
|
||||||
|
@csrf_exempt
|
||||||
|
def safe_webhook():
|
||||||
|
execution_log.append("safe")
|
||||||
|
return {"message": "safe"}
|
||||||
|
|
||||||
|
@app.post("/dangerous-action")
|
||||||
|
def dangerous_action():
|
||||||
|
execution_log.append("dangerous")
|
||||||
|
return {"message": "danger"}
|
||||||
|
|
||||||
|
client = TestClient(app)
|
||||||
|
|
||||||
|
# Exempt endpoint runs
|
||||||
|
resp1 = client.post("/safe-webhook")
|
||||||
|
assert resp1.status_code == 200
|
||||||
|
|
||||||
|
# Protected endpoint blocked WITHOUT executing
|
||||||
|
resp2 = client.post("/dangerous-action")
|
||||||
|
assert resp2.status_code == 403
|
||||||
|
|
||||||
|
assert execution_log == ["safe"], (
|
||||||
|
f"Expected only 'safe' execution, got: {execution_log}"
|
||||||
|
)
|
||||||
187
tests/dashboard/test_tower.py
Normal file
187
tests/dashboard/test_tower.py
Normal file
@@ -0,0 +1,187 @@
|
|||||||
|
"""Tests for Tower dashboard route (/tower)."""
|
||||||
|
|
||||||
|
from unittest.mock import MagicMock, patch
|
||||||
|
|
||||||
|
|
||||||
|
def _mock_spark_engine():
|
||||||
|
"""Return a mock spark_engine with realistic return values."""
|
||||||
|
engine = MagicMock()
|
||||||
|
|
||||||
|
engine.status.return_value = {
|
||||||
|
"enabled": True,
|
||||||
|
"events_captured": 5,
|
||||||
|
"memories_stored": 3,
|
||||||
|
"predictions": {"total": 2, "avg_accuracy": 0.85},
|
||||||
|
"event_types": {
|
||||||
|
"task_posted": 2,
|
||||||
|
"bid_submitted": 1,
|
||||||
|
"task_assigned": 1,
|
||||||
|
"task_completed": 1,
|
||||||
|
"task_failed": 0,
|
||||||
|
"agent_joined": 0,
|
||||||
|
"tool_executed": 0,
|
||||||
|
"creative_step": 0,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
event = MagicMock()
|
||||||
|
event.event_type = "task_completed"
|
||||||
|
event.description = "Task finished"
|
||||||
|
event.importance = 0.8
|
||||||
|
event.created_at = "2026-01-01T00:00:00"
|
||||||
|
event.agent_id = "agent-1234-abcd"
|
||||||
|
event.task_id = "task-5678-efgh"
|
||||||
|
event.data = '{"result": "ok"}'
|
||||||
|
engine.get_timeline.return_value = [event]
|
||||||
|
|
||||||
|
pred = MagicMock()
|
||||||
|
pred.task_id = "task-5678-efgh"
|
||||||
|
pred.accuracy = 0.9
|
||||||
|
pred.evaluated_at = "2026-01-01T01:00:00"
|
||||||
|
pred.created_at = "2026-01-01T00:30:00"
|
||||||
|
pred.predicted_value = '{"outcome": "success"}'
|
||||||
|
engine.get_predictions.return_value = [pred]
|
||||||
|
|
||||||
|
advisory = MagicMock()
|
||||||
|
advisory.category = "performance"
|
||||||
|
advisory.priority = "high"
|
||||||
|
advisory.title = "Slow tasks"
|
||||||
|
advisory.detail = "Tasks taking longer than expected"
|
||||||
|
advisory.suggested_action = "Scale up workers"
|
||||||
|
engine.get_advisories.return_value = [advisory]
|
||||||
|
|
||||||
|
return engine
|
||||||
|
|
||||||
|
|
||||||
|
class TestTowerUI:
|
||||||
|
"""Tests for GET /tower endpoint."""
|
||||||
|
|
||||||
|
@patch("dashboard.routes.tower.spark_engine", new_callable=_mock_spark_engine)
|
||||||
|
def test_tower_returns_200(self, mock_engine, client):
|
||||||
|
response = client.get("/tower")
|
||||||
|
assert response.status_code == 200
|
||||||
|
|
||||||
|
@patch("dashboard.routes.tower.spark_engine", new_callable=_mock_spark_engine)
|
||||||
|
def test_tower_returns_html(self, mock_engine, client):
|
||||||
|
response = client.get("/tower")
|
||||||
|
assert "text/html" in response.headers["content-type"]
|
||||||
|
|
||||||
|
@patch("dashboard.routes.tower.spark_engine", new_callable=_mock_spark_engine)
|
||||||
|
def test_tower_contains_dashboard_content(self, mock_engine, client):
|
||||||
|
response = client.get("/tower")
|
||||||
|
body = response.text
|
||||||
|
assert "tower" in body.lower() or "spark" in body.lower()
|
||||||
|
|
||||||
|
|
||||||
|
class TestSparkSnapshot:
|
||||||
|
"""Tests for _spark_snapshot helper."""
|
||||||
|
|
||||||
|
@patch("dashboard.routes.tower.spark_engine", new_callable=_mock_spark_engine)
|
||||||
|
def test_snapshot_structure(self, mock_engine):
|
||||||
|
from dashboard.routes.tower import _spark_snapshot
|
||||||
|
|
||||||
|
snap = _spark_snapshot()
|
||||||
|
assert snap["type"] == "spark_state"
|
||||||
|
assert "status" in snap
|
||||||
|
assert "events" in snap
|
||||||
|
assert "predictions" in snap
|
||||||
|
assert "advisories" in snap
|
||||||
|
|
||||||
|
@patch("dashboard.routes.tower.spark_engine", new_callable=_mock_spark_engine)
|
||||||
|
def test_snapshot_events_parsed(self, mock_engine):
|
||||||
|
from dashboard.routes.tower import _spark_snapshot
|
||||||
|
|
||||||
|
snap = _spark_snapshot()
|
||||||
|
ev = snap["events"][0]
|
||||||
|
assert ev["event_type"] == "task_completed"
|
||||||
|
assert ev["importance"] == 0.8
|
||||||
|
assert ev["agent_id"] == "agent-12"
|
||||||
|
assert ev["task_id"] == "task-567"
|
||||||
|
assert ev["data"] == {"result": "ok"}
|
||||||
|
|
||||||
|
@patch("dashboard.routes.tower.spark_engine", new_callable=_mock_spark_engine)
|
||||||
|
def test_snapshot_predictions_parsed(self, mock_engine):
|
||||||
|
from dashboard.routes.tower import _spark_snapshot
|
||||||
|
|
||||||
|
snap = _spark_snapshot()
|
||||||
|
pred = snap["predictions"][0]
|
||||||
|
assert pred["task_id"] == "task-567"
|
||||||
|
assert pred["accuracy"] == 0.9
|
||||||
|
assert pred["evaluated"] is True
|
||||||
|
assert pred["predicted"] == {"outcome": "success"}
|
||||||
|
|
||||||
|
@patch("dashboard.routes.tower.spark_engine", new_callable=_mock_spark_engine)
|
||||||
|
def test_snapshot_advisories_parsed(self, mock_engine):
|
||||||
|
from dashboard.routes.tower import _spark_snapshot
|
||||||
|
|
||||||
|
snap = _spark_snapshot()
|
||||||
|
adv = snap["advisories"][0]
|
||||||
|
assert adv["category"] == "performance"
|
||||||
|
assert adv["priority"] == "high"
|
||||||
|
assert adv["title"] == "Slow tasks"
|
||||||
|
assert adv["suggested_action"] == "Scale up workers"
|
||||||
|
|
||||||
|
@patch("dashboard.routes.tower.spark_engine")
|
||||||
|
def test_snapshot_handles_empty_state(self, mock_engine):
|
||||||
|
mock_engine.status.return_value = {"enabled": False}
|
||||||
|
mock_engine.get_timeline.return_value = []
|
||||||
|
mock_engine.get_predictions.return_value = []
|
||||||
|
mock_engine.get_advisories.return_value = []
|
||||||
|
|
||||||
|
from dashboard.routes.tower import _spark_snapshot
|
||||||
|
|
||||||
|
snap = _spark_snapshot()
|
||||||
|
assert snap["events"] == []
|
||||||
|
assert snap["predictions"] == []
|
||||||
|
assert snap["advisories"] == []
|
||||||
|
|
||||||
|
@patch("dashboard.routes.tower.spark_engine")
|
||||||
|
def test_snapshot_handles_invalid_json_data(self, mock_engine):
|
||||||
|
mock_engine.status.return_value = {"enabled": True}
|
||||||
|
|
||||||
|
event = MagicMock()
|
||||||
|
event.event_type = "test"
|
||||||
|
event.description = "bad data"
|
||||||
|
event.importance = 0.5
|
||||||
|
event.created_at = "2026-01-01T00:00:00"
|
||||||
|
event.agent_id = None
|
||||||
|
event.task_id = None
|
||||||
|
event.data = "not-json{"
|
||||||
|
mock_engine.get_timeline.return_value = [event]
|
||||||
|
|
||||||
|
pred = MagicMock()
|
||||||
|
pred.task_id = None
|
||||||
|
pred.accuracy = None
|
||||||
|
pred.evaluated_at = None
|
||||||
|
pred.created_at = "2026-01-01T00:00:00"
|
||||||
|
pred.predicted_value = None
|
||||||
|
mock_engine.get_predictions.return_value = [pred]
|
||||||
|
|
||||||
|
mock_engine.get_advisories.return_value = []
|
||||||
|
|
||||||
|
from dashboard.routes.tower import _spark_snapshot
|
||||||
|
|
||||||
|
snap = _spark_snapshot()
|
||||||
|
ev = snap["events"][0]
|
||||||
|
assert ev["data"] == {}
|
||||||
|
assert "agent_id" not in ev
|
||||||
|
assert "task_id" not in ev
|
||||||
|
|
||||||
|
pred = snap["predictions"][0]
|
||||||
|
assert pred["task_id"] == "?"
|
||||||
|
assert pred["predicted"] == {}
|
||||||
|
|
||||||
|
|
||||||
|
class TestTowerWebSocket:
|
||||||
|
"""Tests for WS /tower/ws endpoint."""
|
||||||
|
|
||||||
|
@patch("dashboard.routes.tower.spark_engine", new_callable=_mock_spark_engine)
|
||||||
|
@patch("dashboard.routes.tower._PUSH_INTERVAL", 0)
|
||||||
|
def test_ws_sends_initial_snapshot(self, mock_engine, client):
|
||||||
|
import json
|
||||||
|
|
||||||
|
with client.websocket_connect("/tower/ws") as ws:
|
||||||
|
data = json.loads(ws.receive_text())
|
||||||
|
assert data["type"] == "spark_state"
|
||||||
|
assert "status" in data
|
||||||
|
assert "events" in data
|
||||||
@@ -5,11 +5,13 @@ from datetime import UTC, datetime, timedelta
|
|||||||
from unittest.mock import patch
|
from unittest.mock import patch
|
||||||
|
|
||||||
from infrastructure.error_capture import (
|
from infrastructure.error_capture import (
|
||||||
|
_build_report_description,
|
||||||
_create_bug_report,
|
_create_bug_report,
|
||||||
_dedup_cache,
|
_dedup_cache,
|
||||||
_extract_traceback_info,
|
_extract_traceback_info,
|
||||||
_get_git_context,
|
_get_git_context,
|
||||||
_is_duplicate,
|
_is_duplicate,
|
||||||
|
_log_bug_report_created,
|
||||||
_log_error_event,
|
_log_error_event,
|
||||||
_notify_bug_report,
|
_notify_bug_report,
|
||||||
_record_to_session,
|
_record_to_session,
|
||||||
@@ -231,6 +233,68 @@ class TestLogErrorEvent:
|
|||||||
_log_error_event(e, "test", "abc123", "file.py", 42, {"branch": "main"})
|
_log_error_event(e, "test", "abc123", "file.py", 42, {"branch": "main"})
|
||||||
|
|
||||||
|
|
||||||
|
class TestBuildReportDescription:
|
||||||
|
"""Test _build_report_description helper."""
|
||||||
|
|
||||||
|
def test_includes_error_info(self):
|
||||||
|
try:
|
||||||
|
raise RuntimeError("desc test")
|
||||||
|
except RuntimeError as e:
|
||||||
|
desc = _build_report_description(
|
||||||
|
e,
|
||||||
|
"test_src",
|
||||||
|
None,
|
||||||
|
"hash1",
|
||||||
|
"tb...",
|
||||||
|
"file.py",
|
||||||
|
10,
|
||||||
|
{"branch": "main"},
|
||||||
|
)
|
||||||
|
assert "RuntimeError" in desc
|
||||||
|
assert "test_src" in desc
|
||||||
|
assert "file.py:10" in desc
|
||||||
|
assert "hash1" in desc
|
||||||
|
|
||||||
|
def test_includes_context_when_provided(self):
|
||||||
|
try:
|
||||||
|
raise RuntimeError("ctx desc")
|
||||||
|
except RuntimeError as e:
|
||||||
|
desc = _build_report_description(
|
||||||
|
e,
|
||||||
|
"src",
|
||||||
|
{"path": "/api"},
|
||||||
|
"h",
|
||||||
|
"tb",
|
||||||
|
"f.py",
|
||||||
|
1,
|
||||||
|
{},
|
||||||
|
)
|
||||||
|
assert "path=/api" in desc
|
||||||
|
|
||||||
|
def test_omits_context_when_none(self):
|
||||||
|
try:
|
||||||
|
raise RuntimeError("no ctx")
|
||||||
|
except RuntimeError as e:
|
||||||
|
desc = _build_report_description(
|
||||||
|
e,
|
||||||
|
"src",
|
||||||
|
None,
|
||||||
|
"h",
|
||||||
|
"tb",
|
||||||
|
"f.py",
|
||||||
|
1,
|
||||||
|
{},
|
||||||
|
)
|
||||||
|
assert "**Context:**" not in desc
|
||||||
|
|
||||||
|
|
||||||
|
class TestLogBugReportCreated:
|
||||||
|
"""Test _log_bug_report_created helper."""
|
||||||
|
|
||||||
|
def test_does_not_crash_on_missing_deps(self):
|
||||||
|
_log_bug_report_created("test", "task-1", "hash1", "title")
|
||||||
|
|
||||||
|
|
||||||
class TestCreateBugReport:
|
class TestCreateBugReport:
|
||||||
"""Test _create_bug_report helper."""
|
"""Test _create_bug_report helper."""
|
||||||
|
|
||||||
|
|||||||
149
tests/infrastructure/test_router_history.py
Normal file
149
tests/infrastructure/test_router_history.py
Normal file
@@ -0,0 +1,149 @@
|
|||||||
|
"""Tests for provider health history store and API endpoint."""
|
||||||
|
|
||||||
|
import time
|
||||||
|
from datetime import UTC, datetime, timedelta
|
||||||
|
from unittest.mock import MagicMock
|
||||||
|
|
||||||
|
import pytest
|
||||||
|
from src.infrastructure.router.history import HealthHistoryStore
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture
|
||||||
|
def store():
|
||||||
|
"""In-memory history store for testing."""
|
||||||
|
s = HealthHistoryStore(db_path=":memory:")
|
||||||
|
yield s
|
||||||
|
s.close()
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture
|
||||||
|
def sample_providers():
|
||||||
|
return [
|
||||||
|
{
|
||||||
|
"name": "anthropic",
|
||||||
|
"status": "healthy",
|
||||||
|
"error_rate": 0.01,
|
||||||
|
"avg_latency_ms": 250.5,
|
||||||
|
"circuit_state": "closed",
|
||||||
|
"total_requests": 100,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "local",
|
||||||
|
"status": "degraded",
|
||||||
|
"error_rate": 0.15,
|
||||||
|
"avg_latency_ms": 80.0,
|
||||||
|
"circuit_state": "closed",
|
||||||
|
"total_requests": 50,
|
||||||
|
},
|
||||||
|
]
|
||||||
|
|
||||||
|
|
||||||
|
def test_record_and_retrieve(store, sample_providers):
|
||||||
|
store.record_snapshot(sample_providers)
|
||||||
|
history = store.get_history(hours=1)
|
||||||
|
assert len(history) == 1
|
||||||
|
assert len(history[0]["providers"]) == 2
|
||||||
|
assert history[0]["providers"][0]["name"] == "anthropic"
|
||||||
|
assert history[0]["providers"][1]["name"] == "local"
|
||||||
|
assert "timestamp" in history[0]
|
||||||
|
|
||||||
|
|
||||||
|
def test_multiple_snapshots(store, sample_providers):
|
||||||
|
store.record_snapshot(sample_providers)
|
||||||
|
time.sleep(0.01)
|
||||||
|
store.record_snapshot(sample_providers)
|
||||||
|
history = store.get_history(hours=1)
|
||||||
|
assert len(history) == 2
|
||||||
|
|
||||||
|
|
||||||
|
def test_hours_filtering(store, sample_providers):
|
||||||
|
old_ts = (datetime.now(UTC) - timedelta(hours=48)).isoformat()
|
||||||
|
store._conn.execute(
|
||||||
|
"""INSERT INTO snapshots
|
||||||
|
(timestamp, provider_name, status, error_rate,
|
||||||
|
avg_latency_ms, circuit_state, total_requests)
|
||||||
|
VALUES (?, ?, ?, ?, ?, ?, ?)""",
|
||||||
|
(old_ts, "anthropic", "healthy", 0.0, 100.0, "closed", 10),
|
||||||
|
)
|
||||||
|
store._conn.commit()
|
||||||
|
store.record_snapshot(sample_providers)
|
||||||
|
|
||||||
|
history = store.get_history(hours=24)
|
||||||
|
assert len(history) == 1
|
||||||
|
|
||||||
|
history = store.get_history(hours=72)
|
||||||
|
assert len(history) == 2
|
||||||
|
|
||||||
|
|
||||||
|
def test_prune(store, sample_providers):
|
||||||
|
old_ts = (datetime.now(UTC) - timedelta(hours=200)).isoformat()
|
||||||
|
store._conn.execute(
|
||||||
|
"""INSERT INTO snapshots
|
||||||
|
(timestamp, provider_name, status, error_rate,
|
||||||
|
avg_latency_ms, circuit_state, total_requests)
|
||||||
|
VALUES (?, ?, ?, ?, ?, ?, ?)""",
|
||||||
|
(old_ts, "anthropic", "healthy", 0.0, 100.0, "closed", 10),
|
||||||
|
)
|
||||||
|
store._conn.commit()
|
||||||
|
store.record_snapshot(sample_providers)
|
||||||
|
|
||||||
|
deleted = store.prune(keep_hours=168)
|
||||||
|
assert deleted == 1
|
||||||
|
history = store.get_history(hours=999)
|
||||||
|
assert len(history) == 1
|
||||||
|
|
||||||
|
|
||||||
|
def test_empty_history(store):
|
||||||
|
assert store.get_history(hours=24) == []
|
||||||
|
|
||||||
|
|
||||||
|
def test_capture_snapshot_from_router(store):
|
||||||
|
mock_metrics = MagicMock()
|
||||||
|
mock_metrics.error_rate = 0.05
|
||||||
|
mock_metrics.avg_latency_ms = 200.0
|
||||||
|
mock_metrics.total_requests = 42
|
||||||
|
|
||||||
|
mock_provider = MagicMock()
|
||||||
|
mock_provider.name = "test-provider"
|
||||||
|
mock_provider.status.value = "healthy"
|
||||||
|
mock_provider.metrics = mock_metrics
|
||||||
|
mock_provider.circuit_state.value = "closed"
|
||||||
|
|
||||||
|
mock_router = MagicMock()
|
||||||
|
mock_router.providers = [mock_provider]
|
||||||
|
|
||||||
|
store._capture_snapshot(mock_router)
|
||||||
|
history = store.get_history(hours=1)
|
||||||
|
assert len(history) == 1
|
||||||
|
p = history[0]["providers"][0]
|
||||||
|
assert p["name"] == "test-provider"
|
||||||
|
assert p["status"] == "healthy"
|
||||||
|
assert p["error_rate"] == 0.05
|
||||||
|
assert p["total_requests"] == 42
|
||||||
|
|
||||||
|
|
||||||
|
def test_history_api_endpoint(store, sample_providers):
|
||||||
|
"""GET /api/v1/router/history returns snapshot data."""
|
||||||
|
store.record_snapshot(sample_providers)
|
||||||
|
|
||||||
|
from fastapi import FastAPI
|
||||||
|
from fastapi.testclient import TestClient
|
||||||
|
from src.infrastructure.router.api import get_cascade_router
|
||||||
|
from src.infrastructure.router.api import router as api_router
|
||||||
|
from src.infrastructure.router.history import get_history_store
|
||||||
|
|
||||||
|
app = FastAPI()
|
||||||
|
app.include_router(api_router)
|
||||||
|
|
||||||
|
app.dependency_overrides[get_history_store] = lambda: store
|
||||||
|
app.dependency_overrides[get_cascade_router] = lambda: MagicMock()
|
||||||
|
|
||||||
|
client = TestClient(app)
|
||||||
|
resp = client.get("/api/v1/router/history?hours=1")
|
||||||
|
assert resp.status_code == 200
|
||||||
|
data = resp.json()
|
||||||
|
assert len(data) == 1
|
||||||
|
assert len(data[0]["providers"]) == 2
|
||||||
|
assert data[0]["providers"][0]["name"] == "anthropic"
|
||||||
|
|
||||||
|
app.dependency_overrides.clear()
|
||||||
@@ -361,6 +361,53 @@ class TestRun:
|
|||||||
assert response == "ok"
|
assert response == "ok"
|
||||||
|
|
||||||
|
|
||||||
|
# ── _handle_retry_or_raise ────────────────────────────────────────────────
|
||||||
|
|
||||||
|
|
||||||
|
class TestHandleRetryOrRaise:
|
||||||
|
def test_raises_on_last_attempt(self):
|
||||||
|
BaseAgent = _make_base_class()
|
||||||
|
with pytest.raises(ValueError, match="boom"):
|
||||||
|
BaseAgent._handle_retry_or_raise(
|
||||||
|
ValueError("boom"),
|
||||||
|
attempt=3,
|
||||||
|
max_retries=3,
|
||||||
|
transient=False,
|
||||||
|
)
|
||||||
|
|
||||||
|
def test_raises_on_last_attempt_transient(self):
|
||||||
|
BaseAgent = _make_base_class()
|
||||||
|
exc = httpx.ConnectError("down")
|
||||||
|
with pytest.raises(httpx.ConnectError):
|
||||||
|
BaseAgent._handle_retry_or_raise(
|
||||||
|
exc,
|
||||||
|
attempt=3,
|
||||||
|
max_retries=3,
|
||||||
|
transient=True,
|
||||||
|
)
|
||||||
|
|
||||||
|
def test_no_raise_on_early_attempt(self):
|
||||||
|
BaseAgent = _make_base_class()
|
||||||
|
# Should return None (no raise) on non-final attempt
|
||||||
|
result = BaseAgent._handle_retry_or_raise(
|
||||||
|
ValueError("retry me"),
|
||||||
|
attempt=1,
|
||||||
|
max_retries=3,
|
||||||
|
transient=False,
|
||||||
|
)
|
||||||
|
assert result is None
|
||||||
|
|
||||||
|
def test_no_raise_on_early_transient(self):
|
||||||
|
BaseAgent = _make_base_class()
|
||||||
|
result = BaseAgent._handle_retry_or_raise(
|
||||||
|
httpx.ReadTimeout("busy"),
|
||||||
|
attempt=2,
|
||||||
|
max_retries=3,
|
||||||
|
transient=True,
|
||||||
|
)
|
||||||
|
assert result is None
|
||||||
|
|
||||||
|
|
||||||
# ── get_capabilities / get_status ────────────────────────────────────────────
|
# ── get_capabilities / get_status ────────────────────────────────────────────
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -55,14 +55,14 @@ def test_think_sends_topic_to_agent():
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
def test_think_passes_model_size_option():
|
def test_think_ignores_model_size_option():
|
||||||
"""think --model-size 70b must forward the model size to create_timmy."""
|
"""think --model-size 70b must not forward model_size to create_timmy."""
|
||||||
mock_timmy = MagicMock()
|
mock_timmy = MagicMock()
|
||||||
|
|
||||||
with patch("timmy.cli.create_timmy", return_value=mock_timmy) as mock_create:
|
with patch("timmy.cli.create_timmy", return_value=mock_timmy) as mock_create:
|
||||||
runner.invoke(app, ["think", "topic", "--model-size", "70b"])
|
runner.invoke(app, ["think", "topic", "--model-size", "70b"])
|
||||||
|
|
||||||
mock_create.assert_called_once_with(backend=None, model_size="70b", session_id="cli")
|
mock_create.assert_called_once_with(backend=None, session_id="cli")
|
||||||
|
|
||||||
|
|
||||||
# ---------------------------------------------------------------------------
|
# ---------------------------------------------------------------------------
|
||||||
|
|||||||
@@ -6,6 +6,9 @@ import pytest
|
|||||||
|
|
||||||
from timmy.mcp_tools import (
|
from timmy.mcp_tools import (
|
||||||
_bridge_to_work_order,
|
_bridge_to_work_order,
|
||||||
|
_build_issue_args,
|
||||||
|
_build_issue_body,
|
||||||
|
_category_from_labels,
|
||||||
_generate_avatar_image,
|
_generate_avatar_image,
|
||||||
_parse_command,
|
_parse_command,
|
||||||
close_mcp_sessions,
|
close_mcp_sessions,
|
||||||
@@ -132,6 +135,49 @@ def test_filesystem_mcp_returns_tools():
|
|||||||
assert "/home/user/project" in params_kwargs["args"]
|
assert "/home/user/project" in params_kwargs["args"]
|
||||||
|
|
||||||
|
|
||||||
|
# ---------------------------------------------------------------------------
|
||||||
|
# _build_issue_body / _build_issue_args / _category_from_labels
|
||||||
|
# ---------------------------------------------------------------------------
|
||||||
|
|
||||||
|
|
||||||
|
def test_build_issue_body_appends_signature():
|
||||||
|
"""_build_issue_body appends the auto-filing signature."""
|
||||||
|
result = _build_issue_body("Some description")
|
||||||
|
assert result.startswith("Some description\n\n")
|
||||||
|
assert "Auto-filed by Timmy" in result
|
||||||
|
|
||||||
|
|
||||||
|
def test_build_issue_body_empty():
|
||||||
|
"""_build_issue_body handles empty body."""
|
||||||
|
result = _build_issue_body("")
|
||||||
|
assert result.startswith("---\n")
|
||||||
|
|
||||||
|
|
||||||
|
def test_build_issue_args():
|
||||||
|
"""_build_issue_args returns correct MCP arguments."""
|
||||||
|
with patch("timmy.mcp_tools.settings") as mock_settings:
|
||||||
|
mock_settings.gitea_repo = "owner/repo"
|
||||||
|
result = _build_issue_args("Title", "Body")
|
||||||
|
assert result == {
|
||||||
|
"method": "create",
|
||||||
|
"owner": "owner",
|
||||||
|
"repo": "repo",
|
||||||
|
"title": "Title",
|
||||||
|
"body": "Body",
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
def test_category_from_labels_bug():
|
||||||
|
"""_category_from_labels returns 'bug' when labels contain bug."""
|
||||||
|
assert _category_from_labels("bug, enhancement") == "bug"
|
||||||
|
|
||||||
|
|
||||||
|
def test_category_from_labels_default():
|
||||||
|
"""_category_from_labels returns 'suggestion' by default."""
|
||||||
|
assert _category_from_labels("enhancement") == "suggestion"
|
||||||
|
assert _category_from_labels("") == "suggestion"
|
||||||
|
|
||||||
|
|
||||||
# ---------------------------------------------------------------------------
|
# ---------------------------------------------------------------------------
|
||||||
# create_gitea_issue_via_mcp
|
# create_gitea_issue_via_mcp
|
||||||
# ---------------------------------------------------------------------------
|
# ---------------------------------------------------------------------------
|
||||||
|
|||||||
109
tests/unit/test_lightning.py
Normal file
109
tests/unit/test_lightning.py
Normal file
@@ -0,0 +1,109 @@
|
|||||||
|
"""Unit tests for the lightning package (factory + ledger)."""
|
||||||
|
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
import pytest
|
||||||
|
|
||||||
|
from lightning.factory import Invoice, MockBackend, get_backend
|
||||||
|
from lightning.ledger import (
|
||||||
|
TxStatus,
|
||||||
|
TxType,
|
||||||
|
clear,
|
||||||
|
create_invoice_entry,
|
||||||
|
get_balance,
|
||||||
|
get_transactions,
|
||||||
|
mark_settled,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture(autouse=True)
|
||||||
|
def _clean_ledger():
|
||||||
|
"""Reset the in-memory ledger between tests."""
|
||||||
|
clear()
|
||||||
|
yield
|
||||||
|
clear()
|
||||||
|
|
||||||
|
|
||||||
|
# ── Factory tests ────────────────────────────────────────────────────
|
||||||
|
|
||||||
|
|
||||||
|
class TestMockBackend:
|
||||||
|
def test_create_invoice_returns_invoice(self):
|
||||||
|
backend = MockBackend()
|
||||||
|
inv = backend.create_invoice(100, "test memo")
|
||||||
|
assert isinstance(inv, Invoice)
|
||||||
|
assert inv.amount_sats == 100
|
||||||
|
assert inv.memo == "test memo"
|
||||||
|
assert len(inv.payment_hash) == 64 # SHA-256 hex
|
||||||
|
assert inv.payment_request.startswith("lnbc")
|
||||||
|
|
||||||
|
def test_invoices_have_unique_hashes(self):
|
||||||
|
backend = MockBackend()
|
||||||
|
a = backend.create_invoice(10)
|
||||||
|
b = backend.create_invoice(10)
|
||||||
|
assert a.payment_hash != b.payment_hash
|
||||||
|
|
||||||
|
|
||||||
|
class TestGetBackend:
|
||||||
|
def test_returns_mock_backend(self):
|
||||||
|
backend = get_backend()
|
||||||
|
assert isinstance(backend, MockBackend)
|
||||||
|
|
||||||
|
|
||||||
|
# ── Ledger tests ─────────────────────────────────────────────────────
|
||||||
|
|
||||||
|
|
||||||
|
class TestLedger:
|
||||||
|
def test_create_invoice_entry(self):
|
||||||
|
entry = create_invoice_entry(
|
||||||
|
payment_hash="abc123",
|
||||||
|
amount_sats=500,
|
||||||
|
memo="test",
|
||||||
|
source="unit_test",
|
||||||
|
)
|
||||||
|
assert entry.tx_type == TxType.incoming
|
||||||
|
assert entry.status == TxStatus.pending
|
||||||
|
assert entry.amount_sats == 500
|
||||||
|
|
||||||
|
def test_mark_settled(self):
|
||||||
|
create_invoice_entry(payment_hash="hash1", amount_sats=100)
|
||||||
|
result = mark_settled("hash1", preimage="secret")
|
||||||
|
assert result is not None
|
||||||
|
assert result.status == TxStatus.settled
|
||||||
|
assert result.preimage == "secret"
|
||||||
|
assert result.settled_at != ""
|
||||||
|
|
||||||
|
def test_mark_settled_unknown_hash(self):
|
||||||
|
assert mark_settled("nonexistent") is None
|
||||||
|
|
||||||
|
def test_get_balance_empty(self):
|
||||||
|
bal = get_balance()
|
||||||
|
assert bal["net_sats"] == 0
|
||||||
|
assert bal["available_sats"] == 0
|
||||||
|
|
||||||
|
def test_get_balance_with_settled(self):
|
||||||
|
create_invoice_entry(payment_hash="h1", amount_sats=1000)
|
||||||
|
mark_settled("h1")
|
||||||
|
bal = get_balance()
|
||||||
|
assert bal["incoming_total_sats"] == 1000
|
||||||
|
assert bal["net_sats"] == 1000
|
||||||
|
assert bal["available_sats"] == 1000
|
||||||
|
|
||||||
|
def test_get_balance_pending_not_counted(self):
|
||||||
|
create_invoice_entry(payment_hash="h2", amount_sats=500)
|
||||||
|
bal = get_balance()
|
||||||
|
assert bal["incoming_total_sats"] == 0
|
||||||
|
assert bal["pending_incoming_sats"] == 500
|
||||||
|
|
||||||
|
def test_get_transactions_returns_entries(self):
|
||||||
|
create_invoice_entry(payment_hash="t1", amount_sats=10)
|
||||||
|
create_invoice_entry(payment_hash="t2", amount_sats=20)
|
||||||
|
txs = get_transactions()
|
||||||
|
assert len(txs) == 2
|
||||||
|
|
||||||
|
def test_get_transactions_filter_by_status(self):
|
||||||
|
create_invoice_entry(payment_hash="f1", amount_sats=10)
|
||||||
|
create_invoice_entry(payment_hash="f2", amount_sats=20)
|
||||||
|
mark_settled("f1")
|
||||||
|
assert len(get_transactions(status="settled")) == 1
|
||||||
|
assert len(get_transactions(status="pending")) == 1
|
||||||
Reference in New Issue
Block a user