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
+4
View File
@@ -0,0 +1,4 @@
// Rebuild when a migration is added or changed (`sqlx::migrate!` embeds them).
fn main() {
println!("cargo:rerun-if-changed=migrations");
}
@@ -0,0 +1,28 @@
-- Text analyses written by the worker: retries with backoff, the model that
-- actually answered (e.g. behind 'openrouter/auto'), cost, a notification for
-- new requests, and at most one refund per analysis.
ALTER TABLE analyses
ADD COLUMN attempts integer NOT NULL DEFAULT 0,
ADD COLUMN next_attempt_at timestamptz NOT NULL DEFAULT now(),
ADD COLUMN model_used text,
ADD COLUMN cost_usd double precision;
COMMENT ON COLUMN analyses.content IS
'Structured result: {title, summary, sections: {key: {title, body}}, strengths: [], challenges: [], advice}';
DROP INDEX analyses_pending_idx;
CREATE INDEX analyses_due_idx ON analyses (next_attempt_at) WHERE status = 'pending';
CREATE UNIQUE INDEX credit_ledger_refund_once ON credit_ledger (analysis_id) WHERE reason = 'refund';
CREATE FUNCTION notify_analyses() RETURNS trigger LANGUAGE plpgsql AS $$
BEGIN
PERFORM pg_notify('analyses', '');
RETURN NULL;
END;
$$;
CREATE TRIGGER analyses_notify
AFTER INSERT ON analyses
FOR EACH STATEMENT EXECUTE FUNCTION notify_analyses();