feat(knowledge-flywheel): add Phase 3 Knowledge Flywheel — AI analysis, review queue, analytics

Phase 3 implementation:
- AI session analysis service that generates flow proposals from resolved sessions
- APScheduler job for batch processing pending analyses (max_instances=1)
- Knowledge gap detection (weak options, high escalation signals)
- Flow proposals CRUD with team admin review workflow (approve/edit/dismiss/reject)
- FlowPilot analytics dashboard with confidence tiers, PSA metrics, knowledge gaps
- In-session script generator component
- Review queue page with filtering and proposal detail panel

Bug fixes from review (12 total):
- Fix "Edit & Publish" navigating to non-existent /editor/new route
- Hide Approve button for enhancement proposals (require Edit & Publish)
- Add max_instances=1 to scheduler to prevent TOCTOU race
- Fix eventual_success case() double-counting failed retries
- Add tree_structure validation before creating tree from proposal
- Simplify script generator rendering condition
- Add severity style fallback, toFixed on rates, Link instead of <a href>
- Add toast.warning on dismiss failure, fix dedup for domain-less sessions
- Cast Decimal to int in knowledge gap evidence dicts

Also updates CLAUDE.md with lessons 67-71 and Phase 3 project structure.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-03-19 05:12:10 +00:00
parent ce118b51d8
commit 9bad49d568
42 changed files with 5427 additions and 13 deletions

View File

@@ -141,6 +141,7 @@ export function FlowPilotSession({
step={step}
isCurrentStep={currentStep?.step_id === step.step_id}
isProcessing={isProcessing && currentStep?.step_id === step.step_id}
sessionId={session.id}
onRespond={onRespond}
/>
))}

View File

@@ -4,11 +4,13 @@ import { cn } from '@/lib/utils'
import type { AISessionStepResponse, StepResponseRequest } from '@/types/ai-session'
import { MarkdownContent } from '@/components/ui/MarkdownContent'
import { FlowPilotOptions } from './FlowPilotOptions'
import { InSessionScriptGenerator } from './InSessionScriptGenerator'
interface FlowPilotStepCardProps {
step: AISessionStepResponse
isCurrentStep: boolean
isProcessing: boolean
sessionId?: string
onRespond: (response: StepResponseRequest) => void
}
@@ -22,7 +24,7 @@ const STEP_TYPE_ICONS = {
note: MessageSquare,
} as const
export function FlowPilotStepCard({ step, isCurrentStep, isProcessing, onRespond }: FlowPilotStepCardProps) {
export function FlowPilotStepCard({ step, isCurrentStep, isProcessing, sessionId, onRespond }: FlowPilotStepCardProps) {
const [freeText, setFreeText] = useState('')
const [showFreeText, setShowFreeText] = useState(false)
const [isCollapsed, setIsCollapsed] = useState(!isCurrentStep)
@@ -163,8 +165,19 @@ export function FlowPilotStepCard({ step, isCurrentStep, isProcessing, onRespond
/>
)}
{/* In-session script generator */}
{!isResolutionSuggestion && (content.action_type as string) === 'script_generation' && sessionId && (
<InSessionScriptGenerator
templateId={(content.template_id as string) || ''}
preFilledParams={(content.pre_filled_params as Record<string, string>) || {}}
instructions={(content.instructions as string) || stepText}
sessionId={sessionId}
onRespond={onRespond}
/>
)}
{/* Action step buttons */}
{!isResolutionSuggestion && step.step_type === 'action' && (
{!isResolutionSuggestion && step.step_type === 'action' && (content.action_type as string) !== 'script_generation' && (
<div className="flex gap-2">
<button
onClick={() => handleActionComplete(true)}

View File

@@ -0,0 +1,159 @@
import { useState } from 'react'
import { Terminal, Copy, Check, Loader2, Play, ChevronDown, ChevronRight } from 'lucide-react'
import { scriptsApi } from '@/api'
import { PowerShellHighlighter } from '@/components/scripts/PowerShellHighlighter'
import { toast } from '@/lib/toast'
import type { StepResponseRequest } from '@/types/ai-session'
interface InSessionScriptGeneratorProps {
templateId: string
preFilledParams: Record<string, string>
instructions: string
sessionId: string
onRespond: (response: StepResponseRequest) => void
}
export function InSessionScriptGenerator({
templateId,
preFilledParams,
instructions,
sessionId,
onRespond,
}: InSessionScriptGeneratorProps) {
const [params, setParams] = useState<Record<string, string>>(preFilledParams)
const [generatedScript, setGeneratedScript] = useState<string | null>(null)
const [isGenerating, setIsGenerating] = useState(false)
const [copied, setCopied] = useState(false)
const [showParams, setShowParams] = useState(true)
const handleGenerate = async () => {
setIsGenerating(true)
try {
const result = await scriptsApi.generate({
template_id: templateId,
parameters: params,
ai_session_id: sessionId,
})
setGeneratedScript(result.script)
setShowParams(false)
toast.success('Script generated')
} catch {
toast.error('Failed to generate script')
} finally {
setIsGenerating(false)
}
}
const handleCopy = async () => {
if (!generatedScript) return
await navigator.clipboard.writeText(generatedScript)
setCopied(true)
setTimeout(() => setCopied(false), 2000)
}
const handleContinue = (success: boolean) => {
onRespond({
action_result: {
success,
details: success
? 'Script generated and executed'
: 'Script generated but action did not resolve the issue',
},
})
}
return (
<div className="mt-3 rounded-xl border border-primary/20 bg-primary/5 p-4 space-y-3">
<div className="flex items-center gap-2">
<Terminal size={14} className="text-primary" />
<span className="font-label text-[0.625rem] uppercase tracking-wider text-primary">
Script Generator
</span>
</div>
{instructions && (
<p className="text-xs text-muted-foreground">{instructions}</p>
)}
{/* Parameter editing */}
{!generatedScript && (
<>
<button
onClick={() => setShowParams(!showParams)}
className="flex items-center gap-1.5 text-xs text-muted-foreground hover:text-foreground transition-colors"
>
{showParams ? <ChevronDown size={12} /> : <ChevronRight size={12} />}
Parameters ({Object.keys(params).length})
</button>
{showParams && Object.keys(params).length > 0 && (
<div className="space-y-2">
{Object.entries(params).map(([key, value]) => (
<div key={key}>
<label className="block text-xs font-medium text-muted-foreground mb-1">
{key.replace(/_/g, ' ')}
</label>
<input
value={value}
onChange={(e) => setParams(prev => ({ ...prev, [key]: e.target.value }))}
className="w-full rounded-lg border border-border bg-card px-3 py-1.5 text-sm text-foreground placeholder:text-muted-foreground focus:border-[rgba(6,182,212,0.3)] focus:outline-none"
/>
</div>
))}
</div>
)}
<button
onClick={handleGenerate}
disabled={isGenerating}
className="w-full flex items-center justify-center gap-2 rounded-lg bg-gradient-brand px-4 py-2 text-sm font-semibold text-[#101114] shadow-lg shadow-primary/20 hover:opacity-90 active:scale-[0.97] disabled:opacity-40 transition-all"
>
{isGenerating ? (
<Loader2 size={14} className="animate-spin" />
) : (
<Play size={14} />
)}
Generate Script
</button>
</>
)}
{/* Generated script display */}
{generatedScript && (
<>
<div className="relative rounded-lg bg-card/80 border border-border overflow-hidden">
<div className="flex items-center justify-between px-3 py-1.5 border-b border-border/50">
<span className="text-xs text-muted-foreground font-mono">PowerShell</span>
<button
onClick={handleCopy}
className="flex items-center gap-1 text-xs text-muted-foreground hover:text-foreground transition-colors"
>
{copied ? <Check size={12} className="text-emerald-400" /> : <Copy size={12} />}
{copied ? 'Copied' : 'Copy'}
</button>
</div>
<div className="p-3 overflow-x-auto max-h-[300px] overflow-y-auto">
<PowerShellHighlighter script={generatedScript} />
</div>
</div>
{/* Continue buttons */}
<div className="flex gap-2 pt-1">
<button
onClick={() => handleContinue(true)}
className="flex-1 rounded-lg bg-primary/10 border border-primary/20 px-4 py-2 text-sm font-medium text-primary hover:bg-primary/20 transition-colors"
>
Script worked continue
</button>
<button
onClick={() => handleContinue(false)}
className="flex-1 rounded-lg bg-card/50 border border-border px-4 py-2 text-sm font-medium text-foreground hover:bg-card transition-colors"
>
Didn't resolve it
</button>
</div>
</>
)}
</div>
)
}

View File

@@ -0,0 +1,62 @@
import { GitBranch, ArrowUpRight, Sparkles, Clock, Hash } from 'lucide-react'
import type { FlowProposalSummary } from '@/types/flow-proposal'
const TYPE_CONFIG = {
new_flow: { label: 'New Flow', color: 'text-emerald-400 bg-emerald-400/10 border-emerald-400/20', icon: Sparkles },
enhancement: { label: 'Enhancement', color: 'text-amber-400 bg-amber-400/10 border-amber-400/20', icon: ArrowUpRight },
branch_addition: { label: 'Branch', color: 'text-blue-400 bg-blue-400/10 border-blue-400/20', icon: GitBranch },
auto_reinforced: { label: 'Reinforced', color: 'text-muted-foreground bg-card border-border', icon: Sparkles },
} as const
interface ProposalCardProps {
proposal: FlowProposalSummary
isSelected: boolean
onClick: () => void
}
export function ProposalCard({ proposal, isSelected, onClick }: ProposalCardProps) {
const typeConfig = TYPE_CONFIG[proposal.proposal_type] || TYPE_CONFIG.new_flow
const TypeIcon = typeConfig.icon
return (
<button
onClick={onClick}
className={`w-full text-left rounded-xl border p-3 space-y-2 transition-all ${
isSelected
? 'border-primary/30 bg-primary/5'
: 'border-[rgba(255,255,255,0.06)] bg-[rgba(24,26,31,0.55)] hover:border-[rgba(255,255,255,0.12)]'
}`}
>
<div className="flex items-start justify-between gap-2">
<p className="text-sm font-semibold text-foreground line-clamp-2">{proposal.title}</p>
<span className={`shrink-0 flex items-center gap-1 rounded-md border px-1.5 py-0.5 font-label text-[0.5625rem] uppercase tracking-wider ${typeConfig.color}`}>
<TypeIcon size={10} />
{typeConfig.label}
</span>
</div>
{proposal.description && (
<p className="text-xs text-muted-foreground line-clamp-2">{proposal.description}</p>
)}
<div className="flex flex-wrap items-center gap-x-3 gap-y-1 text-xs text-muted-foreground">
{proposal.problem_domain && (
<span className="font-label rounded-md bg-primary/10 px-1.5 py-0.5 text-[0.5625rem] uppercase tracking-wider text-primary">
{proposal.problem_domain}
</span>
)}
<span className="flex items-center gap-1">
<Hash size={10} />
{proposal.supporting_session_count} session{proposal.supporting_session_count !== 1 ? 's' : ''}
</span>
<span className="flex items-center gap-1">
<Clock size={10} />
{new Date(proposal.created_at).toLocaleDateString()}
</span>
<span className="text-primary">
{Math.round(proposal.confidence_score * 100)}%
</span>
</div>
</button>
)
}

View File

@@ -0,0 +1,239 @@
import { useState } from 'react'
import { useNavigate, Link } from 'react-router-dom'
import { toast } from '@/lib/toast'
import {
CheckCircle2, XCircle, Pencil, EyeOff, ChevronDown, ChevronRight,
ExternalLink, Clock, Hash, Sparkles, Loader2,
} from 'lucide-react'
import type { FlowProposalDetail as ProposalDetailType } from '@/types/flow-proposal'
interface ProposalDetailProps {
proposal: ProposalDetailType
onReview: (action: 'approve' | 'reject' | 'modify' | 'dismiss', notes?: string) => Promise<void>
}
export function ProposalDetail({ proposal, onReview }: ProposalDetailProps) {
const navigate = useNavigate()
const [reviewNotes, setReviewNotes] = useState('')
const [showFlowData, setShowFlowData] = useState(false)
const [isSubmitting, setIsSubmitting] = useState(false)
const [showRejectConfirm, setShowRejectConfirm] = useState(false)
const canReview = proposal.status === 'pending' || proposal.status === 'dismissed'
const handleAction = async (action: 'approve' | 'reject' | 'modify' | 'dismiss') => {
setIsSubmitting(true)
try {
await onReview(action, reviewNotes || undefined)
} finally {
setIsSubmitting(false)
setShowRejectConfirm(false)
}
}
const handleEditAndPublish = async () => {
// Mark proposal as dismissed so it doesn't stay in the pending queue.
// The tree editor will create the flow independently.
// TODO: Wire tree editor to call review API with action='modify' on save
// when it detects proposalId in location.state, linking the published tree
// back to the proposal.
setIsSubmitting(true)
try {
await onReview('dismiss', 'Opened in Flow Editor for manual editing')
} catch {
// Warn but continue — the editor will still open
toast.warning('Could not update proposal status — it may still appear in the queue')
} finally {
setIsSubmitting(false)
}
const flowData = proposal.proposed_flow_data
navigate('/trees/new', {
state: {
preloadedStructure: flowData.tree_structure || flowData,
proposalId: proposal.id,
proposalTitle: proposal.title,
},
})
}
return (
<div className="flex h-full flex-col">
{/* Header */}
<div className="border-b px-6 py-4" style={{ borderColor: 'var(--glass-border)' }}>
<h2 className="font-heading text-lg font-semibold text-foreground">{proposal.title}</h2>
{proposal.description && (
<p className="mt-1 text-sm text-muted-foreground">{proposal.description}</p>
)}
<div className="mt-3 flex flex-wrap items-center gap-3 text-xs text-muted-foreground">
{proposal.problem_domain && (
<span className="font-label rounded-md bg-primary/10 px-2 py-0.5 text-[0.625rem] uppercase tracking-wider text-primary">
{proposal.problem_domain}
</span>
)}
<span className="flex items-center gap-1">
<Sparkles size={12} />
{Math.round(proposal.confidence_score * 100)}% confidence
</span>
<span className="flex items-center gap-1">
<Hash size={12} />
{proposal.supporting_session_count} supporting session{proposal.supporting_session_count !== 1 ? 's' : ''}
</span>
<span className="flex items-center gap-1">
<Clock size={12} />
{new Date(proposal.created_at).toLocaleString()}
</span>
</div>
</div>
{/* Content */}
<div className="flex-1 overflow-y-auto p-6 space-y-5">
{/* Source session link */}
<div className="glass-card-static p-4">
<h4 className="font-label text-[0.625rem] uppercase tracking-wider text-[#5a6170] mb-2">Source Session</h4>
<Link
to={`/pilot/${proposal.source_session_id}`}
target="_blank"
className="flex items-center gap-2 text-sm text-primary hover:underline"
>
<ExternalLink size={12} />
View session that generated this proposal
</Link>
</div>
{/* Proposed diff (for enhancements) */}
{proposal.proposed_diff && (() => {
const diff = proposal.proposed_diff as { diff_description?: string; new_nodes?: Array<{ title?: string; question?: string; description?: string }> }
return (
<div className="glass-card-static border-l-2 border-l-amber-500 p-4">
<h4 className="font-label text-[0.625rem] uppercase tracking-wider text-[#5a6170] mb-2">Proposed Changes</h4>
{diff.diff_description && (
<p className="text-sm text-foreground">{diff.diff_description}</p>
)}
{diff.new_nodes && diff.new_nodes.length > 0 && (
<div className="mt-3 space-y-1.5">
<p className="text-xs font-medium text-muted-foreground">New nodes:</p>
{diff.new_nodes.map((node, i) => (
<div key={i} className="rounded-lg bg-emerald-500/5 border border-emerald-500/10 px-3 py-2 text-xs text-foreground">
<span className="text-emerald-400">+ </span>
{node.title || node.question || node.description || 'New node'}
</div>
))}
</div>
)}
</div>
)
})()}
{/* Flow data preview */}
<div className="glass-card-static p-4">
<button
onClick={() => setShowFlowData(!showFlowData)}
className="flex items-center gap-1.5 font-label text-[0.625rem] uppercase tracking-wider text-[#5a6170] hover:text-foreground transition-colors"
>
{showFlowData ? <ChevronDown size={12} /> : <ChevronRight size={12} />}
Flow Data (JSON)
</button>
{showFlowData && (
<pre className="mt-3 max-h-[400px] overflow-auto rounded-lg bg-card/80 p-3 text-xs text-muted-foreground font-mono">
{JSON.stringify(proposal.proposed_flow_data, null, 2)}
</pre>
)}
</div>
{/* Supporting sessions */}
{proposal.supporting_session_ids.length > 1 && (
<div className="glass-card-static p-4">
<h4 className="font-label text-[0.625rem] uppercase tracking-wider text-[#5a6170] mb-2">
Supporting Sessions ({proposal.supporting_session_ids.length})
</h4>
<div className="space-y-1">
{proposal.supporting_session_ids.map((sid) => (
<Link
key={sid}
to={`/pilot/${sid}`}
target="_blank"
className="block text-xs text-primary hover:underline truncate"
>
{sid}
</Link>
))}
</div>
</div>
)}
{/* Review info (for already-reviewed proposals) */}
{proposal.reviewed_at && (
<div className="glass-card-static p-4">
<h4 className="font-label text-[0.625rem] uppercase tracking-wider text-[#5a6170] mb-2">Review</h4>
<p className="text-sm text-foreground">
<span className="capitalize">{proposal.status}</span> on{' '}
{new Date(proposal.reviewed_at).toLocaleString()}
</p>
{proposal.reviewer_notes && (
<p className="mt-1 text-xs text-muted-foreground">{proposal.reviewer_notes}</p>
)}
</div>
)}
</div>
{/* Review actions bar */}
{canReview && (
<div
className="border-t px-5 py-3 space-y-3"
style={{ borderColor: 'var(--glass-border)', background: 'rgba(16, 17, 20, 0.8)', backdropFilter: 'blur(12px)' }}
>
{/* Notes input */}
<input
value={reviewNotes}
onChange={(e) => setReviewNotes(e.target.value)}
placeholder="Reviewer notes (optional)"
className="w-full rounded-lg border border-border bg-card px-3 py-2 text-sm text-foreground placeholder:text-muted-foreground focus:border-[rgba(6,182,212,0.3)] focus:outline-none"
/>
{/* Action buttons */}
<div className="flex items-center gap-2">
{proposal.proposal_type === 'new_flow' ? (
<button
onClick={() => handleAction('approve')}
disabled={isSubmitting}
className="flex items-center gap-2 rounded-lg bg-emerald-500/10 border border-emerald-500/20 px-4 py-2 text-sm font-medium text-emerald-400 hover:bg-emerald-500/20 disabled:opacity-40 transition-colors"
>
{isSubmitting ? <Loader2 size={14} className="animate-spin" /> : <CheckCircle2 size={14} />}
Approve & Publish
</button>
) : (
<span className="text-xs text-muted-foreground italic px-2">
Enhancement proposals require Edit & Publish
</span>
)}
<button
onClick={handleEditAndPublish}
disabled={isSubmitting}
className="flex items-center gap-2 rounded-lg bg-[rgba(255,255,255,0.04)] border border-[rgba(255,255,255,0.06)] px-4 py-2 text-sm font-medium text-foreground hover:border-[rgba(255,255,255,0.12)] disabled:opacity-40 transition-colors"
>
<Pencil size={14} />
Edit & Publish
</button>
<button
onClick={() => handleAction('dismiss')}
disabled={isSubmitting}
className="flex items-center gap-2 rounded-lg bg-[rgba(255,255,255,0.04)] border border-[rgba(255,255,255,0.06)] px-4 py-2 text-sm font-medium text-muted-foreground hover:text-foreground hover:border-[rgba(255,255,255,0.12)] disabled:opacity-40 transition-colors ml-auto"
>
<EyeOff size={14} />
Dismiss
</button>
<button
onClick={() => showRejectConfirm ? handleAction('reject') : setShowRejectConfirm(true)}
disabled={isSubmitting}
className="flex items-center gap-2 rounded-lg bg-rose-500/10 border border-rose-500/20 px-4 py-2 text-sm font-medium text-rose-500 hover:bg-rose-500/20 disabled:opacity-40 transition-colors"
>
<XCircle size={14} />
{showRejectConfirm ? 'Confirm Reject' : 'Reject'}
</button>
</div>
</div>
)}
</div>
)
}

View File

@@ -10,3 +10,6 @@ export { SessionTicketCard } from './SessionTicketCard'
export { EscalateModal } from './EscalateModal'
export { EscalationQueue } from './EscalationQueue'
export { SessionBriefing } from './SessionBriefing'
export { ProposalCard } from './ProposalCard'
export { ProposalDetail } from './ProposalDetail'
export { InSessionScriptGenerator } from './InSessionScriptGenerator'

View File

@@ -1,8 +1,8 @@
import { useCallback, useEffect, useState } from 'react'
import { useLocation } from 'react-router-dom'
import {
LayoutGrid, Network, Wrench, Clock, FileOutput, BarChart3,
Settings, PanelLeftClose, PanelLeftOpen, MessageSquareText,
LayoutGrid, Network, Wrench, Clock, FileOutput, BarChart3, TrendingUp,
Settings, PanelLeftClose, PanelLeftOpen, MessageSquareText, ListChecks,
BookOpen, Lightbulb, Code2, Library, Brain, WandSparkles, Sparkles, AlertTriangle,
} from 'lucide-react'
import { cn } from '@/lib/utils'
@@ -94,8 +94,10 @@ export function Sidebar() {
<NavItem href="/flow-assist" icon={WandSparkles} label="Flow Assist" iconColor={NAV_COLORS.flowAssist} collapsed />
<NavItem href="/step-library" icon={Library} label="Step Library" iconColor={NAV_COLORS.stepLib} collapsed />
<NavItem href="/kb-accelerator" icon={Lightbulb} label="KB Accelerator" iconColor={NAV_COLORS.kb} collapsed />
<NavItem href="/review-queue" icon={ListChecks} label="Review Queue" iconColor="#fbbf24" collapsed />
<NavItem href="/shares" icon={FileOutput} label="Exports" iconColor={NAV_COLORS.exports} collapsed />
<NavItem href="/analytics" icon={BarChart3} label="Analytics" iconColor={NAV_COLORS.analytics} collapsed />
<NavItem href="/analytics/flowpilot" icon={TrendingUp} label="FlowPilot Analytics" iconColor="#2dd4bf" collapsed />
<NavItem href="/guides" icon={BookOpen} label="User Guides" iconColor={NAV_COLORS.guides} collapsed />
<NavItem href="/feedback" icon={MessageSquareText} label="Feedback" iconColor={NAV_COLORS.feedback} collapsed />
</div>
@@ -161,6 +163,7 @@ export function Sidebar() {
<NavItem href="/flow-assist" icon={WandSparkles} label="Flow Assist" iconColor={NAV_COLORS.flowAssist} />
<NavItem href="/step-library" icon={Library} label="Step Library" iconColor={NAV_COLORS.stepLib} />
<NavItem href="/kb-accelerator" icon={Lightbulb} label="KB Accelerator" iconColor={NAV_COLORS.kb} />
<NavItem href="/review-queue" icon={ListChecks} label="Review Queue" iconColor="#fbbf24" />
{/* Insights */}
<div className="font-label text-[0.5625rem] uppercase tracking-[0.12em] text-[#5a6170] px-3 pt-3 pb-1">
@@ -168,6 +171,7 @@ export function Sidebar() {
</div>
<NavItem href="/shares" icon={FileOutput} label="Exports" iconColor={NAV_COLORS.exports} />
<NavItem href="/analytics" icon={BarChart3} label="Analytics" iconColor={NAV_COLORS.analytics} />
<NavItem href="/analytics/flowpilot" icon={TrendingUp} label="FlowPilot Analytics" iconColor="#2dd4bf" />
</div>
</>
)}