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>
This commit is contained in:
parent
b20c94ea1a
commit
bd3401df1c
33 changed files with 7132 additions and 554 deletions
76
migrations/070_analyzer_phase2_schema.sql
Normal file
76
migrations/070_analyzer_phase2_schema.sql
Normal file
|
|
@ -0,0 +1,76 @@
|
|||
-- AI Ticket Analyzer — Phase 2 schema additions.
|
||||
-- See docs/ticket-analyzer-phase2-spec.md for the full spec.
|
||||
--
|
||||
-- This migration adds:
|
||||
-- 1. analyzer_stage_executions — per-stage I/O for every pipeline run.
|
||||
-- Replaces analyzer_analyses.model_traces (kept for back-compat for now).
|
||||
-- 2. Three columns on analyzer_analyses:
|
||||
-- source_snapshot — Stage 0 preprocessed events at analysis time
|
||||
-- aggregate_fingerprint — Stage 6 structured fingerprint (added in phase-2.4)
|
||||
-- fingerprint_generated_at — when Stage 6 succeeded (null until then)
|
||||
--
|
||||
-- Phase 2.6 will add analyzer_aggregate_reports + a column + check constraint
|
||||
-- on analyzer_stage_executions linking stage rows to aggregate report runs.
|
||||
|
||||
CREATE EXTENSION IF NOT EXISTS pgcrypto;
|
||||
|
||||
-- =============================================================================
|
||||
-- analyzer_stage_executions
|
||||
-- One row per pipeline-stage attempt. Even failed attempts insert a row.
|
||||
-- =============================================================================
|
||||
CREATE TABLE IF NOT EXISTS analyzer_stage_executions (
|
||||
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
|
||||
analysis_id UUID NOT NULL REFERENCES analyzer_analyses(id) ON DELETE CASCADE,
|
||||
stage TEXT NOT NULL
|
||||
CHECK (stage IN ('preprocess','triage','itglue','analyze','deep_review','fingerprint')),
|
||||
stage_order INT NOT NULL,
|
||||
model_id TEXT,
|
||||
input_payload JSONB NOT NULL,
|
||||
output_payload JSONB NOT NULL,
|
||||
input_tokens INT,
|
||||
output_tokens INT,
|
||||
latency_ms INT,
|
||||
started_at TIMESTAMPTZ NOT NULL,
|
||||
completed_at TIMESTAMPTZ NOT NULL,
|
||||
error_message TEXT
|
||||
);
|
||||
|
||||
CREATE INDEX IF NOT EXISTS idx_analyzer_stage_executions_analysis_order
|
||||
ON analyzer_stage_executions (analysis_id, stage_order);
|
||||
|
||||
CREATE INDEX IF NOT EXISTS idx_analyzer_stage_executions_stage
|
||||
ON analyzer_stage_executions (stage);
|
||||
|
||||
COMMENT ON TABLE analyzer_stage_executions IS
|
||||
'Per-stage I/O for analyzer pipeline runs. One row per stage attempt, including failures. '
|
||||
'Replaces the analyzer_analyses.model_traces JSONB blob (kept for back-compat).';
|
||||
|
||||
COMMENT ON COLUMN analyzer_stage_executions.input_payload IS
|
||||
'Exactly what was sent to the stage. For LLM stages: the system+user prompt payload.';
|
||||
COMMENT ON COLUMN analyzer_stage_executions.output_payload IS
|
||||
'Exactly what came back, pre-merge. For itglue: the redacted docs array. For deep_review: '
|
||||
'the full Opus response including opus_notes (was previously dropped at merge time).';
|
||||
|
||||
-- =============================================================================
|
||||
-- analyzer_analyses — three new nullable columns.
|
||||
-- =============================================================================
|
||||
ALTER TABLE analyzer_analyses
|
||||
ADD COLUMN IF NOT EXISTS source_snapshot JSONB,
|
||||
ADD COLUMN IF NOT EXISTS aggregate_fingerprint JSONB,
|
||||
ADD COLUMN IF NOT EXISTS fingerprint_generated_at TIMESTAMPTZ;
|
||||
|
||||
COMMENT ON COLUMN analyzer_analyses.source_snapshot IS
|
||||
'Stage 0 preprocessed event list at analysis time. Stored canonically so re-analysis or '
|
||||
'aggregate analysis sees consistent input even if the live ticket data changes upstream.';
|
||||
COMMENT ON COLUMN analyzer_analyses.aggregate_fingerprint IS
|
||||
'Stage 6 structured fingerprint (categorization, gaps, recurrence signals) used by '
|
||||
'aggregate cross-ticket reports. Null until Stage 6 succeeds.';
|
||||
COMMENT ON COLUMN analyzer_analyses.fingerprint_generated_at IS
|
||||
'Timestamp when aggregate_fingerprint was written. Null when fingerprint stage skipped or failed.';
|
||||
COMMENT ON COLUMN analyzer_analyses.model_traces IS
|
||||
'LEGACY (phase 1). Superseded by analyzer_stage_executions. Will be dropped once aggregate '
|
||||
'analysis is live and stage_executions has full coverage.';
|
||||
|
||||
CREATE INDEX IF NOT EXISTS idx_analyzer_analyses_fingerprint_present
|
||||
ON analyzer_analyses (id)
|
||||
WHERE aggregate_fingerprint IS NOT NULL;
|
||||
86
migrations/071_analyzer_aggregate_reports.sql
Normal file
86
migrations/071_analyzer_aggregate_reports.sql
Normal file
|
|
@ -0,0 +1,86 @@
|
|||
-- AI Ticket Analyzer — Phase 2.6 aggregate reports.
|
||||
-- See docs/ticket-analyzer-phase2-spec.md → Section D.4.
|
||||
--
|
||||
-- Adds:
|
||||
-- 1. analyzer_aggregate_reports — one row per cross-ticket report
|
||||
-- 2. analyzer_stage_executions:
|
||||
-- a. drop NOT NULL on analysis_id (a stage row may belong to a report instead)
|
||||
-- b. add aggregate_report_id FK
|
||||
-- c. add CHECK constraint enforcing exactly one of analysis_id or aggregate_report_id
|
||||
|
||||
CREATE TABLE IF NOT EXISTS analyzer_aggregate_reports (
|
||||
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
|
||||
generated_by_user_id TEXT REFERENCES "user"(id) ON DELETE SET NULL,
|
||||
generated_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
|
||||
|
||||
-- Inputs
|
||||
filter_criteria JSONB NOT NULL,
|
||||
analysis_ids UUID[] NOT NULL,
|
||||
ticket_count INT NOT NULL,
|
||||
include_itglue_context BOOLEAN NOT NULL DEFAULT true,
|
||||
report_title TEXT,
|
||||
|
||||
-- SQL-derived (computed before LLM call)
|
||||
category_distribution JSONB,
|
||||
client_distribution JSONB,
|
||||
resolution_path_distribution JSONB,
|
||||
root_cause_distribution JSONB,
|
||||
date_range_actual JSONB,
|
||||
|
||||
-- LLM-derived
|
||||
documentation_gaps JSONB,
|
||||
process_gaps JSONB,
|
||||
client_patterns JSONB,
|
||||
recurrence_clusters JSONB,
|
||||
systemic_observations JSONB,
|
||||
recommended_actions JSONB,
|
||||
narrative_summary TEXT,
|
||||
executive_summary TEXT,
|
||||
|
||||
-- Metadata
|
||||
total_input_tokens INT,
|
||||
total_output_tokens INT,
|
||||
estimated_cost_usd NUMERIC(10,4),
|
||||
model_used TEXT,
|
||||
itglue_context_included BOOLEAN,
|
||||
status TEXT NOT NULL DEFAULT 'pending'
|
||||
CHECK (status IN ('pending','running','complete','failed')),
|
||||
error_message TEXT
|
||||
);
|
||||
|
||||
CREATE INDEX IF NOT EXISTS idx_analyzer_aggregate_reports_user_date
|
||||
ON analyzer_aggregate_reports (generated_by_user_id, generated_at DESC);
|
||||
|
||||
CREATE INDEX IF NOT EXISTS idx_analyzer_aggregate_reports_analysis_ids
|
||||
ON analyzer_aggregate_reports USING gin (analysis_ids);
|
||||
|
||||
CREATE INDEX IF NOT EXISTS idx_analyzer_aggregate_reports_pending
|
||||
ON analyzer_aggregate_reports (status, generated_at)
|
||||
WHERE status IN ('pending','running');
|
||||
|
||||
COMMENT ON TABLE analyzer_aggregate_reports IS
|
||||
'Cross-ticket aggregate reports. Inputs (filter_criteria, analysis_ids) are immutable; outputs (LLM-derived fields) are written once when the runner completes.';
|
||||
|
||||
-- analyzer_stage_executions: relax analysis_id, add aggregate_report_id, enforce mutual exclusion.
|
||||
ALTER TABLE analyzer_stage_executions
|
||||
ALTER COLUMN analysis_id DROP NOT NULL;
|
||||
|
||||
ALTER TABLE analyzer_stage_executions
|
||||
ADD COLUMN IF NOT EXISTS aggregate_report_id UUID
|
||||
REFERENCES analyzer_aggregate_reports(id) ON DELETE CASCADE;
|
||||
|
||||
DO $$
|
||||
BEGIN
|
||||
IF NOT EXISTS (
|
||||
SELECT 1 FROM pg_constraint
|
||||
WHERE conname = 'analyzer_stage_executions_parent_check'
|
||||
) THEN
|
||||
ALTER TABLE analyzer_stage_executions
|
||||
ADD CONSTRAINT analyzer_stage_executions_parent_check
|
||||
CHECK ((analysis_id IS NOT NULL) <> (aggregate_report_id IS NOT NULL));
|
||||
END IF;
|
||||
END $$;
|
||||
|
||||
CREATE INDEX IF NOT EXISTS idx_analyzer_stage_executions_aggregate_report
|
||||
ON analyzer_stage_executions (aggregate_report_id, stage_order)
|
||||
WHERE aggregate_report_id IS NOT NULL;
|
||||
26
migrations/072_analyzer_cost_audit.sql
Normal file
26
migrations/072_analyzer_cost_audit.sql
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
-- 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.';
|
||||
Loading…
Add table
Add a link
Reference in a new issue