Questions about an analysis or forecast, answered by the model that wrote it
CI / test (push) Successful in 1m5s
CI / build (push) Successful in 4m56s
CI / deploy (push) Skipped

- 3 questions in every package, +5 questions add-on (1 000 Ft); question units, refund on failure
- The answer sees the chart data, the analysis and earlier questions; at most 250 words
- Editable question prompt (Promptok → Kérdés-válasz); admins regenerate answers
- Package texts and FAQ updated

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-18 18:44:20 +00:00
co-authored by Claude Opus 5.5
parent 22292f533b
commit 182d71fe65
33 changed files with 1233 additions and 59 deletions
+36
View File
@@ -12,6 +12,7 @@
pub mod compact;
pub mod openrouter;
pub mod prompt;
pub mod questions;
use std::collections::HashSet;
use std::sync::{Arc, Mutex};
@@ -98,6 +99,7 @@ impl Analyzer {
Kind::Natal | Kind::Summary => self.natal_text(job.chart_id).await?,
Kind::Synastry => self.synastry_text(job.synastry_id).await?,
Kind::Forecast => self.forecast_text(job.forecast_id).await?,
Kind::Question => return Err(LlmError::Permanent("a question is not an analysis".into())),
};
let (base, revision) = prompt::split_version(&job.prompt_version);
if base != PROMPT_VERSION {
@@ -180,6 +182,7 @@ impl Analyzer {
Kind::Synastry => self.synastry_text(job.synastry_id).await?,
Kind::Forecast => self.forecast_text(job.forecast_id).await?,
Kind::Natal | Kind::Summary => self.natal_text(job.chart_id).await?,
Kind::Question => return Err(LlmError::Permanent("a question has no test run".into())),
};
let built = prompt::build(kind, &job.language, &text, Some(&job.system));
let mut c = self.llm.complete(&job.model, &built).await?;
@@ -324,6 +327,13 @@ impl Analyzer {
.bind(STUCK_AFTER_MINUTES)
.execute(&self.db)
.await?;
sqlx::query(
"UPDATE questions SET status = 'pending', error = 'interrupted', next_attempt_at = now() \
WHERE status = 'running' AND started_at < now() - make_interval(mins => $1)",
)
.bind(STUCK_AFTER_MINUTES)
.execute(&self.db)
.await?;
// A test run is not retried: the admin can start it again.
sqlx::query(
"UPDATE prompt_previews SET status = 'failed', error = 'interrupted', completed_at = now() \
@@ -352,6 +362,16 @@ impl Analyzer {
Ok(r) => tracing::info!(released = r.rows_affected(), "unfinished analyses put back in the queue"),
Err(e) => tracing::error!(error = %e, "releasing unfinished analyses"),
}
let result = sqlx::query(
"UPDATE questions SET status = 'pending', error = 'interrupted', next_attempt_at = now(), \
attempts = greatest(attempts - 1, 0) WHERE id = ANY($1) AND status = 'running'",
)
.bind(&ids)
.execute(&self.db)
.await;
if let Err(e) = result {
tracing::error!(error = %e, "releasing unfinished questions");
}
let result = sqlx::query(
"UPDATE prompt_previews SET status = 'failed', error = 'interrupted', completed_at = now() \
WHERE id = ANY($1) AND status = 'running'",
@@ -384,6 +404,22 @@ pub async fn run(analyzer: Arc<Analyzer>, poll: Duration, stop: watch::Receiver<
}
// Fill the free slots.
while let Ok(permit) = slots.clone().try_acquire_owned() {
// Questions first: someone is waiting for a short answer.
match analyzer.claim_question().await {
Ok(Some(job)) => {
let analyzer = analyzer.clone();
tasks.spawn(async move {
analyzer.handle_question(job).await;
drop(permit);
});
continue;
}
Ok(None) => {}
Err(e) => {
tracing::error!(error = %e, "claiming a question");
break;
}
}
match analyzer.claim().await {
Ok(Some(job)) => {
let analyzer = analyzer.clone();
+179
View File
@@ -0,0 +1,179 @@
//! Answers to questions about a finished analysis (`questions` rows). The
//! model of the analysis answers, seeing the chart data the analysis was
//! written from, the analysis itself and the earlier questions about it.
//! Retries and refunds work as for analyses.
use std::time::{Duration, Instant};
use serde_json::Value;
use uuid::Uuid;
use super::openrouter::{Completion, LlmError};
use super::prompt::{self, Kind};
use super::{Analyzer, backoff};
/// The earlier questions and answers given to the model, at most this many.
const HISTORY: i64 = 6;
#[derive(Debug, sqlx::FromRow)]
pub struct QuestionJob {
pub id: Uuid,
pub analysis_id: Uuid,
pub question: String,
pub language: String,
pub model: String,
pub prompt_version: String,
pub attempts: i32,
pub created_at: time::OffsetDateTime,
}
impl Analyzer {
/// Takes the next due question, if any.
pub async fn claim_question(&self) -> Result<Option<QuestionJob>, sqlx::Error> {
let job: Option<QuestionJob> = sqlx::query_as(
"UPDATE questions SET status = 'running', started_at = now(), attempts = attempts + 1 \
WHERE id = (SELECT id FROM questions WHERE status = 'pending' AND next_attempt_at <= now() \
ORDER BY next_attempt_at LIMIT 1 FOR UPDATE SKIP LOCKED) \
RETURNING id, analysis_id, question, language, model, prompt_version, attempts, created_at",
)
.fetch_optional(&self.db)
.await?;
if let Some(job) = &job {
self.running.lock().unwrap().insert(job.id);
}
Ok(job)
}
async fn question_prompt(&self, job: &QuestionJob) -> Result<prompt::Prompt, LlmError> {
let db_err = |e: sqlx::Error| LlmError::Transient(format!("database: {e}"));
let (kind, chart_id, forecast_id, content): (String, Option<Uuid>, Option<Uuid>, Option<Value>) =
sqlx::query_as("SELECT kind, chart_id, forecast_id, content FROM analyses WHERE id = $1")
.bind(job.analysis_id)
.fetch_optional(&self.db)
.await
.map_err(db_err)?
.ok_or_else(|| LlmError::Permanent("the analysis was deleted".into()))?;
let kind = Kind::parse(&kind).ok_or_else(|| LlmError::Permanent(format!("unknown kind '{kind}'")))?;
let content = content.ok_or_else(|| LlmError::Permanent("the analysis has no text".into()))?;
let chart_text = match kind {
Kind::Forecast => self.forecast_text(forecast_id).await?,
_ => self.natal_text(chart_id).await?,
};
let analysis = prompt::to_markdown(kind, &job.language, &content);
let mut history: Vec<(String, String)> = sqlx::query_as(
"SELECT question, answer FROM questions \
WHERE analysis_id = $1 AND status = 'done' AND answer IS NOT NULL AND created_at < $2 \
ORDER BY created_at DESC LIMIT $3",
)
.bind(job.analysis_id)
.bind(job.created_at)
.bind(HISTORY)
.fetch_all(&self.db)
.await
.map_err(db_err)?;
history.reverse();
let (_, revision) = prompt::split_version(&job.prompt_version);
let system: Option<String> = match revision {
Some(id) => sqlx::query_scalar("SELECT system FROM prompt_revisions WHERE id = $1")
.bind(id)
.fetch_optional(&self.db)
.await
.map_err(db_err)?
.flatten(),
None => None,
};
Ok(prompt::build_question(&job.language, &chart_text, &analysis, &history, &job.question, system.as_deref()))
}
/// Answers one claimed question and records the outcome.
pub async fn handle_question(&self, job: QuestionJob) {
let started = Instant::now();
let result = match self.question_prompt(&job).await {
Ok(p) => self.llm.complete(&job.model, &p).await.map(|mut c| {
prompt::polish(&job.language, &mut c.content);
c
}),
Err(e) => Err(e),
};
if let Err(e) = self.finish_question(&job, result, started.elapsed()).await {
tracing::error!(id = %job.id, error = %e, "saving the answer failed");
}
self.running.lock().unwrap().remove(&job.id);
}
async fn finish_question(
&self,
job: &QuestionJob,
result: Result<Completion, LlmError>,
took: Duration,
) -> Result<(), sqlx::Error> {
match result {
Ok(c) => {
let answer = c.content["answer"].as_str().unwrap_or_default().trim().to_string();
sqlx::query(
"UPDATE questions SET status = 'done', answer = $2, model_used = $3, input_tokens = $4, \
output_tokens = $5, cost_usd = $6, error = NULL, completed_at = now() \
WHERE id = $1 AND status = 'running'",
)
.bind(job.id)
.bind(&answer)
.bind(&c.model)
.bind(c.input_tokens)
.bind(c.output_tokens)
.bind(c.cost_usd)
.execute(&self.db)
.await?;
tracing::info!(
id = %job.id, model = %c.model, cost_usd = c.cost_usd, words = answer.split_whitespace().count(),
secs = took.as_secs(), "answer done"
);
}
Err(LlmError::Transient(message)) if job.attempts < self.max_attempts => {
let wait = backoff(job.attempts);
sqlx::query(
"UPDATE questions SET status = 'pending', error = $2, \
next_attempt_at = now() + make_interval(secs => $3) WHERE id = $1 AND status = 'running'",
)
.bind(job.id)
.bind(&message)
.bind(wait.as_secs_f64())
.execute(&self.db)
.await?;
tracing::warn!(id = %job.id, attempts = job.attempts, error = %message, "answer will be retried");
}
Err(e) => {
self.fail_question(job.id, e.message()).await?;
tracing::error!(id = %job.id, attempts = job.attempts, error = %e.message(), "answer failed");
}
}
Ok(())
}
/// Marks the question failed and gives its unit back (once).
async fn fail_question(&self, id: Uuid, message: &str) -> Result<(), sqlx::Error> {
let mut tx = self.db.begin().await?;
let row: Option<(Option<Uuid>, i32)> = sqlx::query_as(
"UPDATE questions SET status = 'failed', error = $2, completed_at = now() \
WHERE id = $1 AND status = 'running' RETURNING asked_by, credits_charged",
)
.bind(id)
.bind(message)
.fetch_optional(&mut *tx)
.await?;
if let Some((Some(user), units)) = row
&& units > 0
{
sqlx::query(
"INSERT INTO credit_ledger (id, user_id, unit, delta, reason, question_id, note) \
VALUES ($1, $2, 'question', $3, 'refund', $4, 'answer failed') ON CONFLICT DO NOTHING",
)
.bind(Uuid::now_v7())
.bind(user)
.bind(units)
.bind(id)
.execute(&mut *tx)
.await?;
}
tx.commit().await
}
}
+92
View File
@@ -644,4 +644,96 @@ mod analyses {
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]);
}
async fn ask(db: &PgPool, analysis: Uuid, user: Uuid, question: &str, status: &str, answer: Option<&str>) -> Uuid {
let id = Uuid::now_v7();
sqlx::query(
"INSERT INTO questions (id, analysis_id, asked_by, question, language, model, prompt_version, status, \
answer, credits_charged) \
VALUES ($1, $2, $3, $4, 'hu', 'test/model', 'v1', $5, $6, 1)",
)
.bind(id)
.bind(analysis)
.bind(user)
.bind(question)
.bind(status)
.bind(answer)
.execute(db)
.await
.unwrap();
id
}
async fn finished_analysis(db: &PgPool, s: &Setup) -> Uuid {
let id = request_analysis(db, s).await;
sqlx::query("UPDATE analyses SET status = 'done', content = $2 WHERE id = $1")
.bind(id)
.bind(content(Kind::Natal))
.execute(db)
.await
.unwrap();
id
}
async fn answer_once(a: &Analyzer) -> bool {
match a.claim_question().await.unwrap() {
Some(job) => {
a.handle_question(job).await;
true
}
None => false,
}
}
#[sqlx::test(migrator = "db::MIGRATOR")]
async fn answers_a_question_with_the_analysis_and_earlier_answers(db: PgPool) {
let s = setup(&db).await;
let analysis = finished_analysis(&db, &s).await;
ask(&db, analysis, s.user, "Mi az erősségem?", "done", Some("A kitartásod.")).await;
let id = ask(&db, analysis, s.user, "És a munkában?", "pending", None).await;
let (fake, url) =
fake_openrouter(vec![reply(&json!({"answer": "A munkában a \"lassú víz\" a te utad."}))]).await;
let a = analyzer(&db, &url);
assert!(answer_once(&a).await);
assert!(!answer_once(&a).await, "nothing else is due");
let (status, answer): (String, Option<String>) =
sqlx::query_as("SELECT status, answer FROM questions WHERE id = $1").bind(id).fetch_one(&db).await.unwrap();
assert_eq!((status.as_str(), answer.as_deref()), ("done", Some("A munkában a „lassú víz” a te utad.")));
let requests = fake.requests.lock().unwrap();
let body = &requests[0];
assert_eq!(body["model"], "test/model", "the model of the analysis answers");
assert_eq!(body["response_format"]["json_schema"]["name"], "question_answer");
let user = body["messages"][1]["content"].as_str().unwrap();
assert!(user.contains("- Sun: Pisces") && user.contains("# Csendes erő"), "chart data and analysis");
assert!(user.contains("Kérdés: Mi az erősségem?\nVálasz: A kitartásod."), "earlier answers");
assert!(user.contains("És a munkában?") && user.contains("legfeljebb 250 szóban"));
for secret in ["1990", "14:30", "Szeged"] {
assert!(!user.contains(secret), "{secret} leaked");
}
}
#[sqlx::test(migrator = "db::MIGRATOR")]
async fn a_failed_answer_gives_the_question_back(db: PgPool) {
let s = setup(&db).await;
let analysis = finished_analysis(&db, &s).await;
let id = ask(&db, analysis, s.user, "Mikor váltsak?", "pending", None).await;
let (_, url) = fake_openrouter(vec![reply(&json!({"answer": ""}))]).await;
let a = analyzer(&db, &url);
for _ in 0..3 {
sqlx::query("UPDATE questions SET next_attempt_at = now()").execute(&db).await.unwrap();
assert!(answer_once(&a).await);
}
let status: String =
sqlx::query_scalar("SELECT status FROM questions WHERE id = $1").bind(id).fetch_one(&db).await.unwrap();
assert_eq!(status, "failed");
let refunds: Vec<(String, i32)> =
sqlx::query_as("SELECT unit, delta FROM credit_ledger WHERE question_id = $1 AND reason = 'refund'")
.bind(id)
.fetch_all(&db)
.await
.unwrap();
assert_eq!(refunds, vec![("question".to_string(), 1)]);
}
}