2026-11-18 18:44:20 +00:00
|
|
|
//! Answers to questions about a finished analysis (`questions` rows). The
|
2026-11-18 21:11:08 +00:00
|
|
|
//! question model answers, seeing the chart data the analysis was written
|
|
|
|
|
//! from, the analysis itself and the earlier questions about it. Retries,
|
|
|
|
|
//! fallback and refunds work as for analyses.
|
2026-11-18 18:44:20 +00:00
|
|
|
|
|
|
|
|
use std::time::{Duration, Instant};
|
|
|
|
|
|
|
|
|
|
use serde_json::Value;
|
|
|
|
|
use uuid::Uuid;
|
|
|
|
|
|
|
|
|
|
use super::openrouter::{Completion, LlmError};
|
|
|
|
|
use super::prompt::{self, Kind};
|
2026-11-18 21:11:08 +00:00
|
|
|
use super::{Analyzer, Next, active_model, next_step};
|
2026-11-18 18:44:20 +00:00
|
|
|
|
|
|
|
|
/// 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,
|
2026-11-18 21:11:08 +00:00
|
|
|
pub fallback_model: Option<String>,
|
|
|
|
|
pub on_fallback: bool,
|
2026-11-18 21:31:09 +00:00
|
|
|
/// The token of this attempt (see `Job::started_at`).
|
|
|
|
|
pub started_at: time::OffsetDateTime,
|
2026-11-18 18:44:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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) \
|
2026-11-18 21:11:08 +00:00
|
|
|
RETURNING id, analysis_id, question, language, model, prompt_version, attempts, created_at, \
|
2026-11-18 21:31:09 +00:00
|
|
|
fallback_model, on_fallback, started_at",
|
2026-11-18 18:44:20 +00:00
|
|
|
)
|
|
|
|
|
.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();
|
2026-11-18 21:31:09 +00:00
|
|
|
let work = async {
|
|
|
|
|
match self.question_prompt(&job).await {
|
|
|
|
|
Ok(p) => {
|
|
|
|
|
let model = active_model(&job.model, job.fallback_model.as_deref(), job.on_fallback);
|
|
|
|
|
self.llm.complete(model, &p).await.map(|mut c| {
|
|
|
|
|
prompt::polish(&job.language, &mut c.content);
|
|
|
|
|
c
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
Err(e) => Err(e),
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
let result = tokio::select! {
|
|
|
|
|
result = work => result,
|
|
|
|
|
() = self.superseded("questions", job.id, job.started_at) => {
|
|
|
|
|
tracing::info!(id = %job.id, "answer restarted by an admin; this attempt is dropped");
|
|
|
|
|
return;
|
2026-11-18 21:11:08 +00:00
|
|
|
}
|
2026-11-18 18:44:20 +00:00
|
|
|
};
|
|
|
|
|
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() \
|
2026-11-18 21:31:09 +00:00
|
|
|
WHERE id = $1 AND status = 'running' AND started_at = $7",
|
2026-11-18 18:44:20 +00:00
|
|
|
)
|
|
|
|
|
.bind(job.id)
|
|
|
|
|
.bind(&answer)
|
|
|
|
|
.bind(&c.model)
|
|
|
|
|
.bind(c.input_tokens)
|
|
|
|
|
.bind(c.output_tokens)
|
|
|
|
|
.bind(c.cost_usd)
|
2026-11-18 21:31:09 +00:00
|
|
|
.bind(job.started_at)
|
2026-11-18 18:44:20 +00:00
|
|
|
.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(e) => {
|
2026-11-18 21:11:08 +00:00
|
|
|
let can_fall_back = !job.on_fallback && job.fallback_model.is_some();
|
|
|
|
|
match next_step(&e, job.attempts, self.max_attempts, can_fall_back) {
|
|
|
|
|
Next::Retry(wait) => {
|
|
|
|
|
sqlx::query(
|
|
|
|
|
"UPDATE questions SET status = 'pending', error = $2, \
|
2026-11-18 21:31:09 +00:00
|
|
|
next_attempt_at = now() + make_interval(secs => $3) \
|
|
|
|
|
WHERE id = $1 AND status = 'running' AND started_at = $4",
|
2026-11-18 21:11:08 +00:00
|
|
|
)
|
|
|
|
|
.bind(job.id)
|
|
|
|
|
.bind(e.message())
|
|
|
|
|
.bind(wait.as_secs_f64())
|
2026-11-18 21:31:09 +00:00
|
|
|
.bind(job.started_at)
|
2026-11-18 21:11:08 +00:00
|
|
|
.execute(&self.db)
|
|
|
|
|
.await?;
|
|
|
|
|
tracing::warn!(id = %job.id, attempts = job.attempts, error = %e.message(), "answer will be retried");
|
|
|
|
|
}
|
|
|
|
|
Next::Fallback => {
|
|
|
|
|
sqlx::query(
|
|
|
|
|
"UPDATE questions SET status = 'pending', error = $2, on_fallback = true, attempts = 0, \
|
2026-11-18 21:31:09 +00:00
|
|
|
next_attempt_at = now() WHERE id = $1 AND status = 'running' AND started_at = $3",
|
2026-11-18 21:11:08 +00:00
|
|
|
)
|
|
|
|
|
.bind(job.id)
|
|
|
|
|
.bind(e.message())
|
2026-11-18 21:31:09 +00:00
|
|
|
.bind(job.started_at)
|
2026-11-18 21:11:08 +00:00
|
|
|
.execute(&self.db)
|
|
|
|
|
.await?;
|
|
|
|
|
tracing::warn!(
|
|
|
|
|
id = %job.id, model = %job.model, error = %e.message(),
|
|
|
|
|
"the chosen model failed, the default model answers"
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
Next::Fail => {
|
2026-11-18 21:31:09 +00:00
|
|
|
self.fail_question(job.id, job.started_at, e.message()).await?;
|
2026-11-18 21:11:08 +00:00
|
|
|
tracing::error!(id = %job.id, attempts = job.attempts, error = %e.message(), "answer failed");
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-11-18 18:44:20 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Marks the question failed and gives its unit back (once).
|
2026-11-18 21:31:09 +00:00
|
|
|
async fn fail_question(
|
|
|
|
|
&self,
|
|
|
|
|
id: Uuid,
|
|
|
|
|
started_at: time::OffsetDateTime,
|
|
|
|
|
message: &str,
|
|
|
|
|
) -> Result<(), sqlx::Error> {
|
2026-11-18 18:44:20 +00:00
|
|
|
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() \
|
2026-11-18 21:31:09 +00:00
|
|
|
WHERE id = $1 AND status = 'running' AND started_at = $3 RETURNING asked_by, credits_charged",
|
2026-11-18 18:44:20 +00:00
|
|
|
)
|
|
|
|
|
.bind(id)
|
|
|
|
|
.bind(message)
|
2026-11-18 21:31:09 +00:00
|
|
|
.bind(started_at)
|
2026-11-18 18:44:20 +00:00
|
|
|
.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
|
|
|
|
|
}
|
|
|
|
|
}
|