Compare commits

...

3 Commits

Author SHA1 Message Date
Alexander Whitestone
45344ca5af fix: update TemplateResponse calls to new Starlette signature
Some checks failed
Tests / test (pull_request) Has been skipped
Tests / lint (pull_request) Failing after 16s
Starlette changed TemplateResponse from TemplateResponse(name, context)
to TemplateResponse(request, name, context). The old calling convention
passed a dict as the 'name' parameter, which Jinja2 tried to use as a
cache key inside a tuple, causing TypeError: unhashable type: 'dict'.

Updated all old-style calls in routes/tools.py and routes/calm.py to
use the new positional-request-first signature and removed redundant
"request" key from context dicts (Starlette adds it automatically).

Fixes #1114

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-03-23 14:19:51 -04:00
510d890eb2 [claude] Wire QuotaMonitor.select_model() into cascade router (#1106) (#1113)
Some checks failed
Tests / lint (push) Has been cancelled
Tests / test (push) Has been cancelled
2026-03-23 18:13:17 +00:00
852fec3681 [gemini] feat: Integrate ResearchOrchestrator with Paperclip (#978) (#1111)
Some checks failed
Tests / lint (push) Has been cancelled
Tests / test (push) Has been cancelled
Co-authored-by: Google Gemini <gemini@hermes.local>
Co-committed-by: Google Gemini <gemini@hermes.local>
2026-03-23 18:09:29 +00:00
9 changed files with 1059 additions and 51 deletions

726
poetry.lock generated

File diff suppressed because it is too large Load Diff

View File

@@ -68,7 +68,7 @@ voice = ["pyttsx3", "openai-whisper", "piper-tts", "sounddevice"]
celery = ["celery"]
embeddings = ["sentence-transformers", "numpy"]
git = ["GitPython"]
research = ["requests", "trafilatura"]
research = ["requests", "trafilatura", "google-search-results"]
dev = ["pytest", "pytest-asyncio", "pytest-cov", "pytest-timeout", "pytest-randomly", "pytest-xdist", "selenium"]
[tool.poetry.group.dev.dependencies]

View File

@@ -375,13 +375,21 @@ def _startup_init() -> None:
def _startup_background_tasks() -> list[asyncio.Task]:
"""Spawn all recurring background tasks (non-blocking)."""
return [
bg_tasks = [
asyncio.create_task(_briefing_scheduler()),
asyncio.create_task(_thinking_scheduler()),
asyncio.create_task(_loop_qa_scheduler()),
asyncio.create_task(_presence_watcher()),
asyncio.create_task(_start_chat_integrations_background()),
]
try:
from timmy.paperclip import start_paperclip_poller
bg_tasks.append(asyncio.create_task(start_paperclip_poller()))
logger.info("Paperclip poller started")
except ImportError:
logger.debug("Paperclip module not found, skipping poller")
return bg_tasks
def _try_prune(label: str, prune_fn, days: int) -> None:

View File

@@ -196,7 +196,7 @@ async def get_evening_ritual_form(request: Request, db: Session = Depends(get_db
if not journal_entry:
raise HTTPException(status_code=404, detail="No journal entry for today")
return templates.TemplateResponse(
"calm/evening_ritual_form.html", {"request": request, "journal_entry": journal_entry}
request, "calm/evening_ritual_form.html", {"journal_entry": journal_entry}
)
@@ -257,8 +257,9 @@ async def create_new_task(
# After creating a new task, we might need to re-evaluate NOW/NEXT/LATER, but for simplicity
# and given the spec, new tasks go to LATER. Promotion happens on completion/deferral.
return templates.TemplateResponse(
request,
"calm/partials/later_count.html",
{"request": request, "later_tasks_count": len(get_later_tasks(db))},
{"later_tasks_count": len(get_later_tasks(db))},
)
@@ -287,9 +288,9 @@ async def start_task(
promote_tasks(db)
return templates.TemplateResponse(
request,
"calm/partials/now_next_later.html",
{
"request": request,
"now_task": get_now_task(db),
"next_task": get_next_task(db),
"later_tasks_count": len(get_later_tasks(db)),
@@ -316,9 +317,9 @@ async def complete_task(
promote_tasks(db)
return templates.TemplateResponse(
request,
"calm/partials/now_next_later.html",
{
"request": request,
"now_task": get_now_task(db),
"next_task": get_next_task(db),
"later_tasks_count": len(get_later_tasks(db)),
@@ -345,9 +346,9 @@ async def defer_task(
promote_tasks(db)
return templates.TemplateResponse(
request,
"calm/partials/now_next_later.html",
{
"request": request,
"now_task": get_now_task(db),
"next_task": get_next_task(db),
"later_tasks_count": len(get_later_tasks(db)),
@@ -360,8 +361,9 @@ 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)
return templates.TemplateResponse(
request,
"calm/partials/later_tasks_list.html",
{"request": request, "later_tasks": later_tasks},
{"later_tasks": later_tasks},
)
@@ -404,9 +406,9 @@ async def reorder_tasks(
# Re-render the relevant parts of the UI
return templates.TemplateResponse(
request,
"calm/partials/now_next_later.html",
{
"request": request,
"now_task": get_now_task(db),
"next_task": get_next_task(db),
"later_tasks_count": len(get_later_tasks(db)),

View File

@@ -40,9 +40,9 @@ async def tools_page(request: Request):
total_calls = 0
return templates.TemplateResponse(
request,
"tools.html",
{
"request": request,
"available_tools": available_tools,
"agent_tools": agent_tools,
"total_calls": total_calls,

View File

@@ -485,18 +485,26 @@ class CascadeRouter:
def _quota_allows_cloud(self, provider: Provider) -> bool:
"""Check quota before routing to a cloud provider.
Uses the metabolic protocol: cloud calls are gated by 5-hour quota.
Uses the metabolic protocol via select_model(): cloud calls are only
allowed when the quota monitor recommends a cloud model (BURST tier).
Returns True (allow cloud) if quota monitor is unavailable or returns None.
"""
if _quota_monitor is None:
return True
try:
# Map provider type to task_value heuristic
task_value = "high" # conservative default
status = _quota_monitor.check()
if status is None:
return True # No credentials — caller decides based on config
return _quota_monitor.should_use_cloud(task_value)
suggested = _quota_monitor.select_model("high")
# Cloud is allowed only when select_model recommends the cloud model
allows = suggested == "claude-sonnet-4-6"
if not allows:
status = _quota_monitor.check()
tier = status.recommended_tier.value if status else "unknown"
logger.info(
"Metabolic protocol: %s tier — downshifting %s to local (%s)",
tier,
provider.name,
suggested,
)
return allows
except Exception as exc:
logger.warning("Quota check failed, allowing cloud: %s", exc)
return True

175
src/timmy/paperclip.py Normal file
View File

@@ -0,0 +1,175 @@
"""Paperclip integration for Timmy.
This module provides a client for the Paperclip API, and a poller for
running research tasks.
"""
from __future__ import annotations
import asyncio
import logging
from dataclasses import dataclass
import httpx
from config import settings
from timmy.research_triage import triage_research_report
from timmy.research_tools import google_web_search, get_llm_client
logger = logging.getLogger(__name__)
@dataclass
class PaperclipTask:
"""A task from the Paperclip API."""
id: str
kind: str
context: dict
class PaperclipClient:
"""A client for the Paperclip API."""
def __init__(self) -> None:
self.base_url = settings.paperclip_url
self.api_key = settings.paperclip_api_key
self.agent_id = settings.paperclip_agent_id
self.company_id = settings.paperclip_company_id
self.timeout = settings.paperclip_timeout
async def get_tasks(self) -> list[PaperclipTask]:
"""Get a list of tasks from the Paperclip API."""
async with httpx.AsyncClient(timeout=self.timeout) as client:
resp = await client.get(
f"{self.base_url}/api/tasks",
headers={"Authorization": f"Bearer {self.api_key}"},
params={
"agent_id": self.agent_id,
"company_id": self.company_id,
"status": "queued",
},
)
resp.raise_for_status()
tasks = resp.json()
return [
PaperclipTask(id=t["id"], kind=t["kind"], context=t["context"])
for t in tasks
]
async def update_task_status(
self, task_id: str, status: str, result: str | None = None
) -> None:
"""Update the status of a task."""
async with httpx.AsyncClient(timeout=self.timeout) as client:
await client.patch(
f"{self.base_url}/api/tasks/{task_id}",
headers={"Authorization": f"Bearer {self.api_key}"},
json={"status": status, "result": result},
)
class ResearchOrchestrator:
"""Orchestrates research tasks."""
async def get_gitea_issue(self, issue_number: int) -> dict:
"""Get a Gitea issue by its number."""
owner, repo = settings.gitea_repo.split("/", 1)
api_url = f"{settings.gitea_url}/api/v1/repos/{owner}/{repo}/issues/{issue_number}"
async with httpx.AsyncClient(timeout=15) as client:
resp = await client.get(
api_url,
headers={"Authorization": f"token {settings.gitea_token}"},
)
resp.raise_for_status()
return resp.json()
async def post_gitea_comment(self, issue_number: int, comment: str) -> None:
"""Post a comment to a Gitea issue."""
owner, repo = settings.gitea_repo.split("/", 1)
api_url = f"{settings.gitea_url}/api/v1/repos/{owner}/{repo}/issues/{issue_number}/comments"
async with httpx.AsyncClient(timeout=15) as client:
await client.post(
api_url,
headers={"Authorization": f"token {settings.gitea_token}"},
json={"body": comment},
)
async def run_research_pipeline(self, issue_title: str) -> str:
"""Run the research pipeline."""
search_results = await google_web_search(issue_title)
llm_client = get_llm_client()
response = await llm_client.completion(
f"Summarize the following search results and generate a research report:\\n\\n{search_results}",
max_tokens=2048,
)
return response.text
async def run(self, context: dict) -> str:
"""Run a research task."""
issue_number = context.get("issue_number")
if not issue_number:
return "Missing issue_number in task context"
issue = await self.get_gitea_issue(issue_number)
report = await self.run_research_pipeline(issue["title"])
triage_results = await triage_research_report(report, source_issue=issue_number)
comment = f"Research complete for issue #{issue_number}.\\n\\n"
if triage_results:
comment += "Created the following issues:\\n"
for result in triage_results:
if result["gitea_issue"]:
comment += f"- #{result['gitea_issue']['number']}: {result['action_item'].title}\\n"
else:
comment += "No new issues were created.\\n"
await self.post_gitea_comment(issue_number, comment)
return f"Research complete for issue #{issue_number}"
class PaperclipPoller:
"""Polls the Paperclip API for new tasks."""
def __init__(self) -> None:
self.client = PaperclipClient()
self.orchestrator = ResearchOrchestrator()
self.poll_interval = settings.paperclip_poll_interval
async def poll(self) -> None:
"""Poll the Paperclip API for new tasks."""
if self.poll_interval == 0:
return
while True:
try:
tasks = await self.client.get_tasks()
for task in tasks:
if task.kind == "research":
await self.run_research_task(task)
except httpx.HTTPError as exc:
logger.warning("Error polling Paperclip: %s", exc)
await asyncio.sleep(self.poll_interval)
async def run_research_task(self, task: PaperclipTask) -> None:
"""Run a research task."""
await self.client.update_task_status(task.id, "running")
try:
result = await self.orchestrator.run(task.context)
await self.client.update_task_status(task.id, "completed", result)
except Exception as exc:
logger.error("Error running research task: %s", exc, exc_info=True)
await self.client.update_task_status(task.id, "failed", str(exc))
async def start_paperclip_poller() -> None:
"""Start the Paperclip poller."""
if settings.paperclip_enabled:
poller = PaperclipPoller()
asyncio.create_task(poller.poll())

View File

@@ -0,0 +1,42 @@
"""Tools for the research pipeline."""
from __future__ import annotations
import logging
import os
from typing import Any
from config import settings
from serpapi import GoogleSearch
logger = logging.getLogger(__name__)
async def google_web_search(query: str) -> str:
"""Perform a Google search and return the results."""
if "SERPAPI_API_KEY" not in os.environ:
logger.warning("SERPAPI_API_KEY not set, skipping web search")
return ""
params = {
"q": query,
"api_key": os.environ["SERPAPI_API_KEY"],
}
search = GoogleSearch(params)
results = search.get_dict()
return str(results)
def get_llm_client() -> Any:
"""Get an LLM client."""
# This is a placeholder. In a real application, this would return
# a client for an LLM service like OpenAI, Anthropic, or a local
# model.
class MockLLMClient:
async def completion(self, prompt: str, max_tokens: int) -> Any:
class MockCompletion:
def __init__(self, text: str) -> None:
self.text = text
return MockCompletion(f"This is a summary of the search results for '{prompt}'.")
return MockLLMClient()

View File

@@ -664,10 +664,10 @@ class TestVllmMlxProvider:
)
router.providers = [provider]
# Quota monitor returns False (block cloud) — vllm_mlx should still be tried
# Quota monitor downshifts to local (ACTIVE tier) — vllm_mlx should still be tried
with patch("infrastructure.router.cascade._quota_monitor") as mock_qm:
mock_qm.check.return_value = object()
mock_qm.should_use_cloud.return_value = False
mock_qm.select_model.return_value = "qwen3:14b"
mock_qm.check.return_value = None
with patch.object(router, "_call_vllm_mlx") as mock_call:
mock_call.return_value = {
@@ -681,6 +681,115 @@ class TestVllmMlxProvider:
assert result["content"] == "Local MLX response"
class TestMetabolicProtocol:
"""Test metabolic protocol: cloud providers skip when quota is ACTIVE/RESTING."""
def _make_anthropic_provider(self) -> "Provider":
return Provider(
name="anthropic-primary",
type="anthropic",
enabled=True,
priority=1,
api_key="test-key",
models=[{"name": "claude-sonnet-4-6", "default": True}],
)
async def test_cloud_provider_allowed_in_burst_tier(self):
"""BURST tier (quota healthy): cloud provider is tried."""
router = CascadeRouter(config_path=Path("/nonexistent"))
router.providers = [self._make_anthropic_provider()]
with patch("infrastructure.router.cascade._quota_monitor") as mock_qm:
# select_model returns cloud model → BURST tier
mock_qm.select_model.return_value = "claude-sonnet-4-6"
mock_qm.check.return_value = None
with patch.object(router, "_call_anthropic") as mock_call:
mock_call.return_value = {"content": "Cloud response", "model": "claude-sonnet-4-6"}
result = await router.complete(
messages=[{"role": "user", "content": "hard question"}],
)
mock_call.assert_called_once()
assert result["content"] == "Cloud response"
async def test_cloud_provider_skipped_in_active_tier(self):
"""ACTIVE tier (5-hour >= 50%): cloud provider is skipped."""
router = CascadeRouter(config_path=Path("/nonexistent"))
router.providers = [self._make_anthropic_provider()]
with patch("infrastructure.router.cascade._quota_monitor") as mock_qm:
# select_model returns local 14B → ACTIVE tier
mock_qm.select_model.return_value = "qwen3:14b"
mock_qm.check.return_value = None
with patch.object(router, "_call_anthropic") as mock_call:
with pytest.raises(RuntimeError, match="All providers failed"):
await router.complete(
messages=[{"role": "user", "content": "question"}],
)
mock_call.assert_not_called()
async def test_cloud_provider_skipped_in_resting_tier(self):
"""RESTING tier (7-day >= 80%): cloud provider is skipped."""
router = CascadeRouter(config_path=Path("/nonexistent"))
router.providers = [self._make_anthropic_provider()]
with patch("infrastructure.router.cascade._quota_monitor") as mock_qm:
# select_model returns local 8B → RESTING tier
mock_qm.select_model.return_value = "qwen3:8b"
mock_qm.check.return_value = None
with patch.object(router, "_call_anthropic") as mock_call:
with pytest.raises(RuntimeError, match="All providers failed"):
await router.complete(
messages=[{"role": "user", "content": "simple question"}],
)
mock_call.assert_not_called()
async def test_local_provider_always_tried_regardless_of_quota(self):
"""Local (ollama/vllm_mlx) providers bypass the metabolic protocol."""
router = CascadeRouter(config_path=Path("/nonexistent"))
provider = Provider(
name="ollama-local",
type="ollama",
enabled=True,
priority=1,
url="http://localhost:11434",
models=[{"name": "qwen3:14b", "default": True}],
)
router.providers = [provider]
with patch("infrastructure.router.cascade._quota_monitor") as mock_qm:
mock_qm.select_model.return_value = "qwen3:8b" # RESTING tier
with patch.object(router, "_call_ollama") as mock_call:
mock_call.return_value = {"content": "Local response", "model": "qwen3:14b"}
result = await router.complete(
messages=[{"role": "user", "content": "hi"}],
)
mock_call.assert_called_once()
assert result["content"] == "Local response"
async def test_no_quota_monitor_allows_cloud(self):
"""When quota monitor is None (unavailable), cloud providers are allowed."""
router = CascadeRouter(config_path=Path("/nonexistent"))
router.providers = [self._make_anthropic_provider()]
with patch("infrastructure.router.cascade._quota_monitor", None):
with patch.object(router, "_call_anthropic") as mock_call:
mock_call.return_value = {"content": "Cloud response", "model": "claude-sonnet-4-6"}
result = await router.complete(
messages=[{"role": "user", "content": "question"}],
)
mock_call.assert_called_once()
assert result["content"] == "Cloud response"
class TestCascadeRouterReload:
"""Test hot-reload of providers.yaml."""