Paid detailed analyses with per-kind units; example analysis page

- Credits become analysis units (migration 0006): a natal analysis uses
  one 'natal' unit, a synastry analysis one 'synastry' unit. No monthly
  free credits, no model tiers: ANALYSIS_MODEL (default Opus 5.5) writes
  every paid analysis. Admins grant units per kind; the worker refunds
  the right unit on failure.
- Chart page: the detailed analysis card (offer with units left and a
  confirmation, progress while writing, the finished six sections).
- Public /pelda page: a full Opus analysis of a made-up chart, with the
  free summary and a login call to action.
- The worker sends the JSON schema with properties in 'required' order.
  serde_json sorted them, so the title came after ~9000 tokens of text
  and Opus sometimes left it empty (3 failed attempts in a live test);
  with the order fixed the same analysis succeeded at the first try.

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-16 17:39:15 +00:00
co-authored by Claude Opus 5.5
parent 450174c273
commit ce3d3804a7
29 changed files with 1309 additions and 349 deletions
+11 -9
View File
@@ -4,7 +4,7 @@
//! (`running`), builds the prompt from the stored chart data, asks the model
//! through OpenRouter and stores the structured answer (`done`). Temporary
//! failures are retried with backoff; after the last attempt, or on a
//! permanent error, the analysis is `failed` and the credits are refunded.
//! permanent error, the analysis is `failed` and the unit is refunded.
pub mod compact;
pub mod openrouter;
@@ -165,27 +165,29 @@ impl Analyzer {
Ok(())
}
/// Marks the analysis failed and gives the credits back (once).
/// Marks the analysis failed and gives its unit back (once).
async fn fail(&self, id: Uuid, message: &str) -> Result<(), sqlx::Error> {
let mut tx = self.db.begin().await?;
let row: Option<(Option<Uuid>, i32)> = sqlx::query_as(
let row: Option<(Option<Uuid>, i32, String)> = sqlx::query_as(
"UPDATE analyses SET status = 'failed', error = $2, completed_at = now() \
WHERE id = $1 AND status = 'running' RETURNING requested_by, credits_charged",
WHERE id = $1 AND status = 'running' RETURNING requested_by, credits_charged, kind",
)
.bind(id)
.bind(message)
.fetch_optional(&mut *tx)
.await?;
if let Some((Some(user), credits)) = row
&& credits > 0
if let Some((Some(user), units, kind)) = row
&& units > 0
{
let unit = if kind == "synastry" { "synastry" } else { "natal" };
sqlx::query(
"INSERT INTO credit_ledger (id, user_id, delta, reason, analysis_id, note) \
VALUES ($1, $2, $3, 'refund', $4, 'analysis failed') ON CONFLICT DO NOTHING",
"INSERT INTO credit_ledger (id, user_id, unit, delta, reason, analysis_id, note) \
VALUES ($1, $2, $3, $4, 'refund', $5, 'analysis failed') ON CONFLICT DO NOTHING",
)
.bind(Uuid::now_v7())
.bind(user)
.bind(credits)
.bind(unit)
.bind(units)
.bind(id)
.execute(&mut *tx)
.await?;