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:
@@ -180,3 +180,304 @@ async fn cleanup_removes_only_expired_data(db: PgPool) {
|
||||
sqlx::query_as("SELECT array_agg(id ORDER BY id) FROM email_outbox").fetch_one(&db).await.unwrap();
|
||||
assert_eq!(left, vec![recent, unsent]);
|
||||
}
|
||||
|
||||
// --- analyses ----------------------------------------------------------------------------
|
||||
|
||||
mod analyses {
|
||||
use std::sync::{Arc, Mutex};
|
||||
|
||||
use axum::Json;
|
||||
use axum::extract::State;
|
||||
use axum::http::StatusCode;
|
||||
use serde_json::{Value, json};
|
||||
use sqlx::PgPool;
|
||||
use uuid::Uuid;
|
||||
|
||||
use crate::analysis::openrouter::OpenRouter;
|
||||
use crate::analysis::prompt::{Kind, schema, validate};
|
||||
use crate::analysis::{Analyzer, backoff};
|
||||
|
||||
/// Scripted replies (then the last one forever) and the requests received.
|
||||
#[derive(Clone, Default)]
|
||||
struct Fake {
|
||||
replies: Arc<Mutex<Vec<(u16, Value)>>>,
|
||||
requests: Arc<Mutex<Vec<Value>>>,
|
||||
}
|
||||
|
||||
async fn completions(State(fake): State<Fake>, Json(body): Json<Value>) -> (StatusCode, Json<Value>) {
|
||||
fake.requests.lock().unwrap().push(body);
|
||||
let mut replies = fake.replies.lock().unwrap();
|
||||
let (status, reply) = if replies.len() > 1 { replies.remove(0) } else { replies[0].clone() };
|
||||
(StatusCode::from_u16(status).unwrap(), Json(reply))
|
||||
}
|
||||
|
||||
async fn fake_openrouter(replies: Vec<(u16, Value)>) -> (Fake, String) {
|
||||
let fake = Fake { replies: Arc::new(Mutex::new(replies)), ..Default::default() };
|
||||
let app =
|
||||
axum::Router::new().route("/chat/completions", axum::routing::post(completions)).with_state(fake.clone());
|
||||
let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
|
||||
let addr = listener.local_addr().unwrap();
|
||||
tokio::spawn(async move { axum::serve(listener, app).await.unwrap() });
|
||||
(fake, format!("http://{addr}"))
|
||||
}
|
||||
|
||||
fn content(kind: Kind) -> Value {
|
||||
let schema = schema(kind);
|
||||
let keys: Vec<&str> = schema["properties"]["sections"]["required"]
|
||||
.as_array()
|
||||
.unwrap()
|
||||
.iter()
|
||||
.map(|k| k.as_str().unwrap())
|
||||
.collect();
|
||||
let sections: serde_json::Map<String, Value> = keys
|
||||
.iter()
|
||||
.map(|k| ((*k).to_owned(), json!({"title": format!("Cím {k}"), "body": "Első bekezdés.\n\nMásodik."})))
|
||||
.collect();
|
||||
json!({
|
||||
"title": "Csendes erő", "summary": "Összefoglaló.", "sections": sections,
|
||||
"strengths": ["Kitartás.", "Empátia."], "challenges": ["Túlgondolás.", "Visszahúzódás."],
|
||||
"advice": "Bízz magadban."
|
||||
})
|
||||
}
|
||||
|
||||
fn reply(content: &Value) -> (u16, Value) {
|
||||
(
|
||||
200,
|
||||
json!({
|
||||
"model": "test/model-2026",
|
||||
"choices": [{"finish_reason": "stop", "message": {"role": "assistant", "content": content.to_string()}}],
|
||||
"usage": {"prompt_tokens": 2100, "completion_tokens": 4800, "cost": 0.0123}
|
||||
}),
|
||||
)
|
||||
}
|
||||
|
||||
fn fixture(name: &str) -> Value {
|
||||
let path = format!("{}/testdata/{name}", env!("CARGO_MANIFEST_DIR"));
|
||||
serde_json::from_str(&std::fs::read_to_string(path).unwrap()).unwrap()
|
||||
}
|
||||
|
||||
struct Setup {
|
||||
user: Uuid,
|
||||
chart: Uuid,
|
||||
}
|
||||
|
||||
async fn setup(db: &PgPool) -> Setup {
|
||||
let (user, chart) = (Uuid::now_v7(), Uuid::now_v7());
|
||||
sqlx::query("INSERT INTO users (id, email) VALUES ($1, 'anna@example.com')")
|
||||
.bind(user)
|
||||
.execute(db)
|
||||
.await
|
||||
.unwrap();
|
||||
sqlx::query(
|
||||
"INSERT INTO charts (id, owner_id, label, is_primary, birth_data, result, chart_hash) \
|
||||
VALUES ($1, $2, 'Saját', true, '{}', $3, 'hash1')",
|
||||
)
|
||||
.bind(chart)
|
||||
.bind(user)
|
||||
.bind(fixture("natal-1990-03-15.json"))
|
||||
.execute(db)
|
||||
.await
|
||||
.unwrap();
|
||||
Setup { user, chart }
|
||||
}
|
||||
|
||||
async fn request_analysis(db: &PgPool, s: &Setup) -> Uuid {
|
||||
let id = Uuid::now_v7();
|
||||
sqlx::query(
|
||||
"INSERT INTO analyses (id, kind, chart_id, requested_by, language, model, prompt_version, cache_key, credits_charged) \
|
||||
VALUES ($1, 'natal', $2, $3, 'hu', 'test/model', 'v1', 'key', 1)",
|
||||
)
|
||||
.bind(id)
|
||||
.bind(s.chart)
|
||||
.bind(s.user)
|
||||
.execute(db)
|
||||
.await
|
||||
.unwrap();
|
||||
id
|
||||
}
|
||||
|
||||
fn analyzer(db: &PgPool, base_url: &str) -> Analyzer {
|
||||
Analyzer::new(db.clone(), OpenRouter::new(base_url, "test-key", None), 3, 2)
|
||||
}
|
||||
|
||||
async fn run_once(a: &Analyzer) -> bool {
|
||||
match a.claim().await.unwrap() {
|
||||
Some(job) => {
|
||||
a.handle(job).await;
|
||||
true
|
||||
}
|
||||
None => false,
|
||||
}
|
||||
}
|
||||
|
||||
type Row = (String, i32, Option<String>, Option<String>, Option<i32>, Option<f64>, bool);
|
||||
|
||||
async fn row(db: &PgPool, id: Uuid) -> Row {
|
||||
sqlx::query_as(
|
||||
"SELECT status, attempts, error, model_used, output_tokens, cost_usd, next_attempt_at > now() \
|
||||
FROM analyses WHERE id = $1",
|
||||
)
|
||||
.bind(id)
|
||||
.fetch_one(db)
|
||||
.await
|
||||
.unwrap()
|
||||
}
|
||||
|
||||
async fn refunds(db: &PgPool, user: Uuid) -> Vec<i32> {
|
||||
sqlx::query_scalar("SELECT delta FROM credit_ledger WHERE user_id = $1 AND reason = 'refund'")
|
||||
.bind(user)
|
||||
.fetch_all(db)
|
||||
.await
|
||||
.unwrap()
|
||||
}
|
||||
|
||||
async fn make_due(db: &PgPool, id: Uuid) {
|
||||
sqlx::query("UPDATE analyses SET next_attempt_at = now() WHERE id = $1").bind(id).execute(db).await.unwrap();
|
||||
}
|
||||
|
||||
#[sqlx::test(migrator = "db::MIGRATOR")]
|
||||
async fn writes_the_analysis(db: PgPool) {
|
||||
let s = setup(&db).await;
|
||||
let id = request_analysis(&db, &s).await;
|
||||
let (fake, url) = fake_openrouter(vec![reply(&content(Kind::Natal))]).await;
|
||||
let a = analyzer(&db, &url);
|
||||
|
||||
assert!(run_once(&a).await);
|
||||
assert!(!run_once(&a).await, "nothing else is due");
|
||||
assert_eq!(
|
||||
row(&db, id).await,
|
||||
("done".into(), 1, None, Some("test/model-2026".into()), Some(4800), Some(0.0123), false)
|
||||
);
|
||||
let (stored,): (Value,) =
|
||||
sqlx::query_as("SELECT content FROM analyses WHERE id = $1").bind(id).fetch_one(&db).await.unwrap();
|
||||
assert_eq!(stored["title"], "Csendes erő");
|
||||
|
||||
let requests = fake.requests.lock().unwrap();
|
||||
let body = &requests[0];
|
||||
assert_eq!(body["model"], "test/model");
|
||||
assert_eq!(body["response_format"]["json_schema"]["strict"], true);
|
||||
assert_eq!(body["provider"]["require_parameters"], true);
|
||||
let system = body["messages"][0]["content"].as_str().unwrap();
|
||||
let user = body["messages"][1]["content"].as_str().unwrap();
|
||||
assert!(system.contains("Write in Hungarian.") && system.contains("Szaturnusz"));
|
||||
assert!(user.contains("- Sun: Pisces 24°42', house 8") && user.contains("- personality: "));
|
||||
// No birth data in the prompt.
|
||||
for secret in ["1990", "14:30", "Szeged", "Hungary"] {
|
||||
assert!(!user.contains(secret) && !system.contains(secret), "{secret} leaked");
|
||||
}
|
||||
}
|
||||
|
||||
#[sqlx::test(migrator = "db::MIGRATOR")]
|
||||
async fn retries_bad_answers_then_fails_and_refunds(db: PgPool) {
|
||||
let s = setup(&db).await;
|
||||
let id = request_analysis(&db, &s).await;
|
||||
let broken = json!({"title": "Csak cím"});
|
||||
let (_, url) = fake_openrouter(vec![reply(&broken)]).await;
|
||||
let a = analyzer(&db, &url);
|
||||
|
||||
assert!(run_once(&a).await);
|
||||
let (status, attempts, error, _, _, _, later) = row(&db, id).await;
|
||||
assert_eq!((status.as_str(), attempts, later), ("pending", 1, true));
|
||||
assert!(error.unwrap().contains("does not match the schema"));
|
||||
assert!(!run_once(&a).await, "the retry waits");
|
||||
assert!(refunds(&db, s.user).await.is_empty());
|
||||
|
||||
for _ in 0..2 {
|
||||
make_due(&db, id).await;
|
||||
assert!(run_once(&a).await);
|
||||
}
|
||||
let (status, attempts, ..) = row(&db, id).await;
|
||||
assert_eq!((status.as_str(), attempts), ("failed", 3));
|
||||
assert_eq!(refunds(&db, s.user).await, vec![1]);
|
||||
}
|
||||
|
||||
#[sqlx::test(migrator = "db::MIGRATOR")]
|
||||
async fn permanent_errors_fail_at_once_and_retry_after_outage(db: PgPool) {
|
||||
let s = setup(&db).await;
|
||||
let bad_model = request_analysis(&db, &s).await;
|
||||
let (_, url) = fake_openrouter(vec![(400, json!({"error": {"message": "unknown model"}}))]).await;
|
||||
assert!(run_once(&analyzer(&db, &url)).await);
|
||||
let (status, attempts, error, ..) = row(&db, bad_model).await;
|
||||
assert_eq!((status.as_str(), attempts), ("failed", 1));
|
||||
assert_eq!(error.as_deref(), Some("HTTP 400: unknown model"));
|
||||
assert_eq!(refunds(&db, s.user).await, vec![1]);
|
||||
|
||||
// A provider outage (5xx) is retried and then succeeds.
|
||||
let outage = request_analysis(&db, &s).await;
|
||||
let (_, url) =
|
||||
fake_openrouter(vec![(503, json!({"error": {"message": "overloaded"}})), reply(&content(Kind::Natal))])
|
||||
.await;
|
||||
let a = analyzer(&db, &url);
|
||||
assert!(run_once(&a).await);
|
||||
assert_eq!(row(&db, outage).await.0, "pending");
|
||||
make_due(&db, outage).await;
|
||||
assert!(run_once(&a).await);
|
||||
assert_eq!(row(&db, outage).await.0, "done");
|
||||
assert_eq!(refunds(&db, s.user).await, vec![1], "no refund for the successful one");
|
||||
}
|
||||
|
||||
#[sqlx::test(migrator = "db::MIGRATOR")]
|
||||
async fn synastry_prompt_and_stuck_jobs(db: PgPool) {
|
||||
let s = setup(&db).await;
|
||||
let response = fixture("synastry-response.json");
|
||||
let (chart2, synastry) = (Uuid::now_v7(), Uuid::now_v7());
|
||||
sqlx::query(
|
||||
"INSERT INTO charts (id, owner_id, label, is_primary, birth_data, result, chart_hash) \
|
||||
VALUES ($1, $2, 'Partner', false, '{}', $3, 'hash2')",
|
||||
)
|
||||
.bind(chart2)
|
||||
.bind(s.user)
|
||||
.bind(&response["person2"])
|
||||
.execute(&db)
|
||||
.await
|
||||
.unwrap();
|
||||
sqlx::query(
|
||||
"INSERT INTO synastries (id, owner_id, chart1_id, chart2_id, result, synastry_hash) VALUES ($1, $2, $3, $4, $5, 'sh')",
|
||||
)
|
||||
.bind(synastry)
|
||||
.bind(s.user)
|
||||
.bind(s.chart)
|
||||
.bind(chart2)
|
||||
.bind(&response)
|
||||
.execute(&db)
|
||||
.await
|
||||
.unwrap();
|
||||
let id = Uuid::now_v7();
|
||||
sqlx::query(
|
||||
"INSERT INTO analyses (id, kind, synastry_id, requested_by, language, model, prompt_version, cache_key, \
|
||||
status, started_at) VALUES ($1, 'synastry', $2, $3, 'en', 'test/model', 'v1', 'k2', 'running', now() - interval '20 minutes')",
|
||||
)
|
||||
.bind(id)
|
||||
.bind(synastry)
|
||||
.bind(s.user)
|
||||
.execute(&db)
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
let (fake, url) = fake_openrouter(vec![reply(&content(Kind::Synastry))]).await;
|
||||
let a = analyzer(&db, &url);
|
||||
assert!(!run_once(&a).await, "running jobs are not claimed");
|
||||
assert_eq!(a.requeue_stuck().await.unwrap(), 1);
|
||||
assert!(run_once(&a).await);
|
||||
assert_eq!(row(&db, id).await.0, "done");
|
||||
|
||||
let requests = fake.requests.lock().unwrap();
|
||||
let user = requests[0]["messages"][1]["content"].as_str().unwrap();
|
||||
assert!(user.starts_with("Chart data:\n\nSYNASTRY") && user.contains("- attraction: "));
|
||||
assert!(requests[0]["messages"][0]["content"].as_str().unwrap().contains("Write in English."));
|
||||
assert!(!user.contains("Szeged") && !user.contains("Pécs"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn schema_validation_and_backoff() {
|
||||
let schema = schema(Kind::Natal);
|
||||
assert_eq!(validate(&schema, &content(Kind::Natal)), Ok(()));
|
||||
let mut c = content(Kind::Natal);
|
||||
c["sections"]["love"]["body"] = json!(" ");
|
||||
assert_eq!(validate(&schema, &c), Err("$.sections.love.body: expected a non-empty string".into()));
|
||||
c["sections"]["love"]["body"] = json!("Szöveg.");
|
||||
c["sections"].as_object_mut().unwrap().remove("work");
|
||||
assert!(validate(&schema, &c).unwrap_err().contains("work: missing"));
|
||||
assert_eq!([1, 2, 3, 9].map(|n| backoff(n).as_secs()), [30, 120, 480, 3600]);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user