-- 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();