feat(scouts): deliver_pending drains queue and sends scout_proposal frames
Add ScoutEngine.deliver_pending(user_id, ws) that queries status='queued' rows, fetches metadata via the registered connector, sends scout_proposal WS frames, and flips status to 'delivered'. Add ack_proposal(proposal_id) that flips 'delivered' -> 'acked' (idempotent). Wire both into device_ws.py: deliver_pending fires as a background task after device_hello + register; scout_proposal_ack frames dispatch to ack_proposal in the message loop. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -132,6 +132,61 @@ class ScoutEngine:
|
||||
ref.source_msg_ref,
|
||||
)
|
||||
|
||||
async def deliver_pending(self, user_id: uuid.UUID, ws) -> None:
|
||||
"""Drain status='queued' rows for user, send scout_proposal WS frames, flip to 'delivered'."""
|
||||
from app.scouts.connectors.base import ItemRef # noqa: PLC0415
|
||||
async with self._session_factory() as session:
|
||||
rows = (await session.execute(
|
||||
select(ScoutTriageQueue).where(
|
||||
ScoutTriageQueue.user_id == str(user_id),
|
||||
ScoutTriageQueue.status == "queued",
|
||||
)
|
||||
)).scalars().all()
|
||||
|
||||
for row in rows:
|
||||
try:
|
||||
connector = get_connector(row.source_type)
|
||||
except KeyError:
|
||||
logger.warning("deliver_pending: no connector for %s", row.source_type)
|
||||
continue
|
||||
scout = await session.get(CloudScoutConfig, row.scout_id)
|
||||
if scout is None:
|
||||
continue
|
||||
try:
|
||||
meta = await connector.fetch_metadata(scout, ItemRef(source_msg_ref=row.source_msg_ref))
|
||||
except Exception:
|
||||
logger.exception("deliver_pending: fetch_metadata failed")
|
||||
continue
|
||||
|
||||
payload = {
|
||||
"type": "scout_proposal",
|
||||
"proposal": {
|
||||
"id": row.id,
|
||||
"scout_id": row.scout_id,
|
||||
"source_type": row.source_type,
|
||||
"source_msg_ref": row.source_msg_ref,
|
||||
"raw_subject": meta.subject,
|
||||
"raw_snippet": meta.snippet,
|
||||
"category": "unprocessed",
|
||||
"payload": None,
|
||||
},
|
||||
}
|
||||
await ws.send_json(payload)
|
||||
row.status = "delivered"
|
||||
row.delivered_at = datetime.now(tz=timezone.utc)
|
||||
|
||||
await session.commit()
|
||||
|
||||
async def ack_proposal(self, proposal_id: str) -> None:
|
||||
"""Flip a delivered proposal to acked. Idempotent — no-op if already acked."""
|
||||
async with self._session_factory() as session:
|
||||
row = await session.get(ScoutTriageQueue, proposal_id)
|
||||
if row is None:
|
||||
return
|
||||
row.status = "acked"
|
||||
row.acked_at = datetime.now(tz=timezone.utc)
|
||||
await session.commit()
|
||||
|
||||
async def _triage_llm(self, scout: CloudScoutConfig, content: ItemContent) -> TriageVerdict:
|
||||
"""Stub — real implementation in Task 24."""
|
||||
raise NotImplementedError("Real triage LLM call lands in Task 24")
|
||||
|
||||
Reference in New Issue
Block a user