wulf-pulse/migrations/072_analyzer_cost_audit.sql
lorentz bd3401df1c feat(analyzer): Phase 2 — full stage persistence, fingerprints, aggregate reports, cost guards
Eight sub-phases per docs/ticket-analyzer-phase2-spec.md:

2.1 Schema (migration 070): analyzer_stage_executions table; source_snapshot,
    aggregate_fingerprint, fingerprint_generated_at columns on analyzer_analyses.
    model_traces marked LEGACY (kept for back-compat).
2.2 Every pipeline stage records a row to analyzer_stage_executions, success
    or failure. Worker persists a status='failed' analyzer_analyses row when
    the pipeline throws so partial stage records have a parent. Pipeline
    exposes raw triage/sonnet/opus responses for downstream stages.
2.3 Stage 3 prompt updated with markdown formatting rules + banned filler
    phrases. Added react-markdown + remark-gfm + @tailwindcss/typography.
    New <AnalysisMarkdown> component replaces <ProseText>; coerces stray
    headers to bold paragraphs.
2.4 Stage 6 fingerprint (Haiku) runs after persistence, failure-tolerant.
    scripts/backfill-fingerprints.ts reconstructs Stage 6 input from the
    legacy model_traces blob.
2.5 Browse UI rebuild at /analyzer/tickets: multi-select for client/issue/
    queue/status/priority/assignee, sticky filter bar, active-filter chips,
    bulk selection persisted via localStorage, "Analyze N selected" +
    "Generate aggregate report" actions. New <MultiSelect> primitive.
    Staleness uses last_activity_date > completed_at heuristic per spec C.1.
2.6 Aggregate reports (migration 071): runner is fire-and-forget, persists
    SQL distributions immediately so UI shows partial state during the
    Sonnet reduce call. Three endpoints, three pages (/analyzer/reports[/new
    /:id]). IT Glue context fetcher capped at 200 doc titles.
2.7 Cost guards (migration 072): per-request $5 confirmation, soft-warn at
    $20/day, hard-block at $50/day with ANALYZER_DAILY_COST_OVERRIDE_USERS
    override. Every gating decision audited.
2.8 Runbook + build notes updated.

128 vitest tests passing, tsc clean. Migrations 070/071/072 idempotent
(IF NOT EXISTS). model_traces double-write retained — drop in a future
migration once aggregate reports have soaked.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-04-29 14:00:22 -04:00

26 lines
1.2 KiB
SQL

-- AI Ticket Analyzer — Phase 2.7 cost-guard audit log.
-- See docs/ticket-analyzer-phase2-spec.md → Section D.8.
CREATE TABLE IF NOT EXISTS analyzer_cost_audit (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
user_id TEXT REFERENCES "user"(id) ON DELETE SET NULL,
action TEXT NOT NULL,
-- e.g. 'aggregate_report' | 'analyze_ticket'
estimated_cost NUMERIC(10,4) NOT NULL,
daily_spend_before NUMERIC(10,4) NOT NULL,
decision TEXT NOT NULL
CHECK (decision IN ('approved','requires_confirmation','blocked','overridden')),
decision_reason TEXT,
context JSONB,
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
);
CREATE INDEX IF NOT EXISTS idx_analyzer_cost_audit_user_date
ON analyzer_cost_audit (user_id, created_at DESC);
CREATE INDEX IF NOT EXISTS idx_analyzer_cost_audit_decision
ON analyzer_cost_audit (decision, created_at DESC);
COMMENT ON TABLE analyzer_cost_audit IS
'Audit log of cost-guard decisions for analyzer LLM operations. One row per gated request '
'(approved/requires_confirmation/blocked/overridden), recording the inputs that drove the decision.';