Write text analyses with OpenRouter in the worker

- analysis job: claims pending analyses (SKIP LOCKED, NOTIFY wake-up),
  builds a compact chart text without birth data, asks OpenRouter with a
  strict JSON schema, validates and stores the answer, model used, tokens
  and cost; retries transient errors, refunds credits on final failure,
  requeues interrupted jobs
- prompt v1 for natal and synastry analyses, Hungarian style guide
- compare-models subcommand for blind model comparison
- migration 0003: attempts, next_attempt_at, model_used, cost_usd,
  notify trigger, one refund per analysis
- db: rebuild when migrations change
- compose: worker reads OPENROUTER_API_KEY from .env.secrets

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012vLHzh5qUhFtmJhzkcTbXL
This commit is contained in:
feco
2026-11-15 12:18:40 +00:00
co-authored by Claude Opus 5.5
parent 771ece41b1
commit 812b2d4f73
19 changed files with 1615 additions and 35 deletions
+2 -25
View File
@@ -10,7 +10,6 @@ use lettre::message::{Mailbox, MultiPart};
use lettre::{AsyncSmtpTransport, AsyncTransport, Message, Tokio1Executor};
use serde_json::Value;
use sqlx::PgPool;
use sqlx::postgres::PgListener;
use tokio::sync::watch;
use uuid::Uuid;
@@ -174,18 +173,8 @@ impl Delivery {
}
}
async fn listen(db: &PgPool) -> Option<PgListener> {
let result = async {
let mut listener = PgListener::connect_with(db).await?;
listener.listen(CHANNEL).await?;
Ok::<_, sqlx::Error>(listener)
}
.await;
result.map_err(|e| tracing::warn!(error = %e, "LISTEN failed; polling only")).ok()
}
pub async fn run(delivery: Delivery, poll: Duration, stop: watch::Receiver<bool>) {
let mut listener = listen(&delivery.db).await;
let mut listener = crate::listen(&delivery.db, CHANNEL).await;
loop {
if *stop.borrow() {
break;
@@ -195,20 +184,8 @@ pub async fn run(delivery: Delivery, poll: Duration, stop: watch::Receiver<bool>
Ok(_) => {}
Err(e) => tracing::error!(error = %e, "e-mail delivery"),
}
let notified = async {
match listener.as_mut() {
// Reconnects by itself; an error means the connection was lost for now.
Some(l) => {
if let Err(e) = l.recv().await {
tracing::warn!(error = %e, "LISTEN connection lost");
tokio::time::sleep(Duration::from_secs(5)).await;
}
}
None => std::future::pending().await,
}
};
tokio::select! {
() = notified => {}
() = crate::notified(listener.as_mut()) => {}
() = tokio::time::sleep(poll) => {}
() = crate::stopped(stop.clone()) => break,
}