/** * Types for the self-expanding formal verification system (Phase 1). * * When the LLM verifier finds a bug and it's confirmed correct, * the system extracts the pattern and codifies it as a deterministic rule. * Future scans use the formal rule instead of the LLM. */ /** The kind of detection pattern extracted from a confirmed finding. */ export type PatternKind = 'regex' | 'ast_pattern' | 'type_constraint'; /** A generalizable pattern extracted from a confirmed LLM finding. */ export interface ExtractedPattern { /** Unique pattern ID (e.g., "lp_001"). */ readonly id: string; /** Human-readable description of what this pattern catches. */ readonly description: string; /** The kind of detection used. */ readonly kind: PatternKind; /** Languages this pattern applies to. */ readonly languages: readonly string[]; /** The regex pattern string for detection (kind='regex'). */ readonly regexPattern?: string; /** Glob for which files to scan. */ readonly fileGlob: string; /** Whether the pattern should be PRESENT (and it's bad) or ABSENT (and it's bad if present). */ readonly matchBehavior: 'presence_is_bad' | 'absence_is_bad'; /** The original claim category that triggered this pattern. */ readonly claimCategory: string; /** Severity when this pattern fires. */ readonly severity: 'critical' | 'high' | 'medium'; /** Evidence template using {match}, {file}, etc. */ readonly evidenceTemplate: string; } /** Status lifecycle of a learned rule. */ export type LearnedRuleStatus = 'candidate' | 'validated' | 'promoted' | 'rejected' | 'deprecated'; /** A rule generated from a confirmed LLM finding. */ export interface LearnedRule { /** Unique rule ID (e.g., "lr_001"). */ readonly id: string; /** The pattern this rule detects. */ readonly pattern: ExtractedPattern; /** Current lifecycle status. */ readonly status: LearnedRuleStatus; /** When the rule was first created. */ readonly createdAt: string; /** When the rule was last updated. */ readonly updatedAt: string; /** How many times this rule has fired across all scans. */ readonly fireCount: number; /** How many times a human confirmed a fire was correct. */ readonly truePositiveCount: number; /** How many times a human marked a fire as false positive. */ readonly falsePositiveCount: number; /** The original finding that spawned this rule. */ readonly sourceFindings: readonly SourceFinding[]; /** Validation results from the harness. */ readonly validationResults?: ValidationResult; /** Whether this rule was discovered via code scanning, PR review mining, or ships bundled. */ readonly source?: 'scan' | 'pr' | 'cve' | 'bundled'; /** Human-readable description of how to fix the detected issue. */ readonly fixDescription?: string; /** Regex replacement pattern for auto-fix suggestions. */ readonly fixPattern?: string; /** Number of distinct PRs/repos where this pattern was independently confirmed. */ readonly confirmationCount?: number; } /** Reference to the original LLM finding that spawned a learned rule. */ export interface SourceFinding { /** The claim ID from the original verification. */ readonly claimId: string; /** Description of the claim. */ readonly claimDescription: string; /** The code snippet that contained the bug. */ readonly codeSnippet: string; /** The file path where the bug was found. */ readonly filePath: string; /** The language of the code. */ readonly language: string; /** When this finding was recorded. */ readonly timestamp: string; } /** Results from running a rule through the validation harness. */ export interface ValidationResult { /** Whether the rule passed validation. */ readonly passed: boolean; /** True positives: correctly flagged known-bad code. */ readonly truePositives: number; /** False positives: incorrectly flagged known-good code. */ readonly falsePositives: number; /** True negatives: correctly passed known-good code. */ readonly trueNegatives: number; /** False negatives: missed known-bad code. */ readonly falseNegatives: number; /** Precision = TP / (TP + FP). */ readonly precision: number; /** Recall = TP / (TP + FN). */ readonly recall: number; /** Validation timestamp. */ readonly validatedAt: string; /** Synthetic test cases used. */ readonly testCases: readonly TestCase[]; } /** A test case for validating a learned rule. */ export interface TestCase { /** Description of what this test case checks. */ readonly description: string; /** The code to test. */ readonly code: string; /** Whether this code SHOULD trigger the rule. */ readonly shouldMatch: boolean; /** Whether the rule DID match. */ readonly didMatch: boolean; } /** Input to the pattern extractor. */ export interface PatternExtractionInput { /** The verified claim (LLM found a real bug). */ readonly claim: { readonly id: string; readonly category: string; readonly severity: 'critical' | 'high' | 'medium' | 'low'; readonly description: string; readonly assertion: string; }; /** The verification result (confirmed FAIL). */ readonly verification: { readonly verdict: 'FAIL'; readonly reasoning: string; readonly evidence?: string; }; /** The code that was verified. */ readonly code: string; /** The language of the code. */ readonly language: string; /** The file path of the code. */ readonly filePath: string; } /** Output of the pattern extractor. */ export interface PatternExtractionResult { /** Whether extraction succeeded. */ readonly success: boolean; /** The extracted pattern, if successful. */ readonly pattern?: ExtractedPattern; /** Why extraction failed, if it did. */ readonly failureReason?: string; } /** A review comment from a PR code review. */ export interface PRReviewComment { readonly body: string; readonly path: string; readonly line: number | null; } /** A merged PR discovered for rule mining. */ export interface DiscoveredPR { readonly repo: string; readonly prNumber: number; readonly title: string; readonly labels: string[]; readonly mergeCommit: string; readonly reviewComments: PRReviewComment[]; readonly files: string[]; } /** A single hunk from a parsed PR diff. */ export interface DiffHunk { readonly file: string; readonly removedLines: string[]; readonly addedLines: string[]; readonly context: string[]; readonly startLine: number; } /** A PR with its parsed diff hunks. */ export interface ParsedPRDiff { readonly pr: DiscoveredPR; readonly hunks: DiffHunk[]; } /** A generalized rule extracted from PR review patterns. */ export interface GeneralizedRule { readonly category: string; readonly severity: 'low' | 'medium' | 'high' | 'critical'; readonly description: string; readonly detection: { readonly pattern: string; readonly language: string; }; readonly fix: { readonly description: string; readonly pattern: string; }; readonly fileGlob: string; readonly matchBehavior: 'presence_is_bad' | 'absence_is_bad'; readonly evidenceTemplate: string; readonly provenance: { readonly repo: string; readonly pr: number; readonly file: string; readonly reviewComment?: string; }; }