grounding

package
v1.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 11, 2026 License: AGPL-3.0 Imports: 18 Imported by: 0

Documentation

Overview

Package grounding provides anti-hallucination validation for LLM responses.

This package implements an 8-layer defense system to detect and prevent hallucinations in LLM-generated responses about code. The layers are:

PREVENTION LAYERS (before generation):

  • Layer 1: Prompt Engineering - Citation requirements, grounding instructions
  • Layer 2: Structured Output - Force JSON with evidence links

DETECTION LAYERS (after generation):

  • Layer 3: Citation Validation - Verify [file:line] references
  • Layer 4: Tool Result Grounding - EvidenceIndex, claim extraction
  • Layer 5: Language/Pattern Detection - Wrong-language detection

VERIFICATION LAYERS (second pass):

  • Layer 6: TMS Integration - Truth Maintenance System for belief tracking
  • Layer 7: Multi-Sample Consistency - Generate multiple responses, find consensus
  • Layer 8: Chain-of-Verification - Self-verification step

The package follows the SafetyGate pattern from services/trace/agent/safety/ for consistency with existing validation infrastructure.

Thread Safety:

All types in this package are designed for concurrent use.

Index

Constants

This section is empty.

Variables

View Source
var DefaultCodeExtensions = map[string]bool{
	".go":    true,
	".py":    true,
	".js":    true,
	".ts":    true,
	".jsx":   true,
	".tsx":   true,
	".java":  true,
	".rs":    true,
	".c":     true,
	".cpp":   true,
	".h":     true,
	".hpp":   true,
	".rb":    true,
	".php":   true,
	".swift": true,
	".kt":    true,
	".scala": true,
	".md":    true,
	".yaml":  true,
	".yml":   true,
	".json":  true,
	".toml":  true,
	".sh":    true,
}

DefaultCodeExtensions are common code file extensions to track.

Functions

func AddCheckerEvent

func AddCheckerEvent(span trace.Span, checker string, violationCount int, duration time.Duration)

AddCheckerEvent adds an event to the span for checker execution.

Inputs:

  • span: The span to add the event to.
  • checker: Name of the checker.
  • violationCount: Number of violations found.
  • duration: Time taken for the check.

Thread Safety: Safe for concurrent use.

func AddConsensusEvent

func AddConsensusEvent(span trace.Span, result *ConsensusResult)

AddConsensusEvent adds an event to the span for multi-sample consensus.

Inputs:

  • span: The span to add the event to.
  • result: The consensus result.

Thread Safety: Safe for concurrent use.

func AddViolationEvent

func AddViolationEvent(span trace.Span, v Violation)

AddViolationEvent adds an event to the span for a violation.

Inputs:

  • span: The span to add the event to.
  • v: The violation.

Thread Safety: Safe for concurrent use.

func CountViolationsByPriority

func CountViolationsByPriority(violations []Violation) map[ViolationPriority]int

CountViolationsByPriority counts violations at each priority level.

Description:

Returns a map from priority to count of violations at that priority.
Useful for metrics and deciding on retry strategies.

Inputs:

  • violations: Slice of violations to count.

Outputs:

  • map[ViolationPriority]int: Count of violations per priority.

Thread Safety: Safe for concurrent use.

func DerivePackagesFromFiles

func DerivePackagesFromFiles(files map[string]bool) map[string]bool

DerivePackagesFromFiles extracts unique package paths from file paths.

Description:

Takes a map of file paths and extracts unique directory paths as packages.
Also includes parent packages for nested paths (pkg/api/v1 → pkg/api, pkg).

Inputs:

  • files: Map of file paths (e.g., "cmd/orchestrator/main.go")

Outputs:

  • map[string]bool: Unique package paths (e.g., "cmd/orchestrator")

Thread Safety: Safe for concurrent use (pure function).

func DetectLanguageFromPath

func DetectLanguageFromPath(filePath string) string

DetectLanguageFromPath returns the language based on file extension.

Inputs:

filePath - The file path to analyze.

Outputs:

string - The detected language (e.g., "go", "python"), or empty if unknown.

func DetectProjectLanguage

func DetectProjectLanguage(counts map[string]int) string

DetectProjectLanguage determines the primary language from extension counts.

Description:

Analyzes extension counts to determine the dominant programming language.
Returns the language with the most files, excluding config/docs extensions.

Inputs:

  • counts: Map of extensions to file counts (from FileManifest.ExtensionCounts).

Outputs:

  • string: The detected language (e.g., "go", "python"), or "" if undetermined.

Thread Safety: Safe for concurrent use (pure function).

func ExtractUserQuestion

func ExtractUserQuestion(assembledCtx *agent.AssembledContext) string

ExtractUserQuestion finds the user's original question from context.

Description:

Searches the conversation history for the first user message,
which typically contains the original question.

Inputs:

assembledCtx - The context with conversation history.

Outputs:

string - The user's original question, or empty string if not found.

func GetBlockedFrameworks

func GetBlockedFrameworks(projectLang string) map[string]bool

GetBlockedFrameworks returns frameworks that shouldn't appear for a project language.

Description:

Returns the set of framework/tool names that indicate hallucination
if mentioned in a response for the given project language.

Inputs:

  • projectLang: The project's primary language (e.g., "go", "python").

Outputs:

  • map[string]bool: Set of blocked framework names (lowercase).

Thread Safety: Safe for concurrent use (returns reference to package-level map).

func GetLanguageSuggestion

func GetLanguageSuggestion(projectLang, wrongPattern string) string

GetLanguageSuggestion provides a helpful suggestion for a wrong-language pattern.

Description:

Returns a suggestion for what to use instead of a wrong-language pattern.
For example, if Flask is mentioned in a Go project, suggests using net/http.

Inputs:

  • projectLang: The correct project language.
  • wrongPattern: The pattern that was incorrectly mentioned.

Outputs:

  • string: A helpful suggestion, or "" if no specific suggestion available.

Thread Safety: Safe for concurrent use (pure function).

func HasHighPriorityViolations

func HasHighPriorityViolations(violations []Violation) bool

HasHighPriorityViolations returns true if any violations are P0, P1, or P2.

Description:

Quick check for critical violations that should trigger special handling
(e.g., immediate retry or feedback loop).

Inputs:

  • violations: Slice of violations to check.

Outputs:

  • bool: True if any high-priority violations exist (P0-P2).

Thread Safety: Safe for concurrent use.

func IsConsistent

func IsConsistent(result *ConsensusResult, minRate float64) bool

IsConsistent checks if a consensus result indicates consistency.

Inputs:

result - The consensus result.
minRate - Minimum consensus rate to be considered consistent (0.0-1.0).

Outputs:

bool - True if the result is sufficiently consistent.

func RecordAPIHallucination

func RecordAPIHallucination(ctx context.Context, claimType string, claimedLibrary string, actualLibrary string)

RecordAPIHallucination records an API/library hallucination detection.

Inputs:

  • ctx: Context for metric recording.
  • claimType: Type of claim (library_missing, library_confusion, api_not_found).
  • claimedLibrary: The library that was claimed.
  • actualLibrary: The library actually found in evidence (if confusion detected).

Thread Safety: Safe for concurrent use.

func RecordAttributeHallucination

func RecordAttributeHallucination(ctx context.Context, claimType string, symbolKind string)

RecordAttributeHallucination records an attribute hallucination detection.

Inputs:

  • ctx: Context for metric recording.
  • claimType: Type of attribute claim (return_type, parameter_count, field_count, etc.).
  • symbolKind: Kind of symbol (function, struct, interface).

Thread Safety: Safe for concurrent use.

func RecordBehavioralHallucination

func RecordBehavioralHallucination(ctx context.Context, category string, subject string, claimedBehavior string)

RecordBehavioralHallucination records a behavioral hallucination detection.

Inputs:

  • ctx: Context for metric recording.
  • category: Category of behavioral claim (error_handling, validation, security).
  • subject: The function/component the claim is about.
  • claimedBehavior: The behavior that was claimed.

Thread Safety: Safe for concurrent use.

func RecordCheck

func RecordCheck(ctx context.Context, checker string, violationCount int, duration time.Duration)

RecordCheck records metrics for a single checker execution.

Inputs:

  • ctx: Context for metric recording.
  • checker: Name of the checker.
  • violationCount: Number of violations found.
  • duration: Time taken for the check.

Thread Safety: Safe for concurrent use.

func RecordCircuitBreakerState

func RecordCircuitBreakerState(ctx context.Context, state CircuitBreakerStateValue)

RecordCircuitBreakerState records the circuit breaker state change.

Uses delta approach: subtracts old state value and adds new state value so the metric reflects current state, not cumulative.

Inputs:

  • ctx: Context for metric recording.
  • state: New circuit breaker state.

Thread Safety: Safe for concurrent use.

func RecordConfidence

func RecordConfidence(ctx context.Context, confidence float64)

RecordConfidence records the response confidence score.

Inputs:

  • ctx: Context for metric recording.
  • confidence: Confidence score (0.0-1.0).

Thread Safety: Safe for concurrent use.

func RecordConfidenceFabrication

func RecordConfidenceFabrication(ctx context.Context, claimType string, evidenceStrength string, severity Severity)

RecordConfidenceFabrication records a confidence fabrication detection.

Inputs:

  • ctx: Context for metric recording.
  • claimType: Type of claim (absolute, negative_absolute, universal, exhaustive).
  • evidenceStrength: Strength of evidence (absent, partial, strong).
  • severity: The violation severity.

Thread Safety: Safe for concurrent use.

func RecordConsensusResult

func RecordConsensusResult(ctx context.Context, result *ConsensusResult)

RecordConsensusResult records multi-sample consensus metrics.

Inputs:

  • ctx: Context for metric recording.
  • result: The consensus result to record.

Thread Safety: Safe for concurrent use.

func RecordCountCircuitBreaker

func RecordCountCircuitBreaker(toolName, path string)

RecordCountCircuitBreaker records when the count-based circuit breaker fires.

Description:

GR-39b: Tracks when a tool call is blocked because it exceeded the
count threshold (DefaultCircuitBreakerThreshold). Labels include the
tool name and the path (router or llm) where the breaker fired.

Inputs:

toolName - Name of the tool that was blocked.
path - The execution path where breaker fired ("router" or "llm").

Thread Safety: Safe for concurrent use.

func RecordCrossContextConfusion

func RecordCrossContextConfusion(ctx context.Context, confusionType string, symbol string, claimedLocation string, actualLocation string)

RecordCrossContextConfusion records a cross-context confusion detection.

Inputs:

  • ctx: Context for metric recording.
  • confusionType: Type of confusion (attribute_confusion, location_mismatch, ambiguous_reference).
  • symbol: The symbol name that was confused.
  • claimedLocation: The location claimed in the response.
  • actualLocation: The actual location in evidence (if applicable).

Thread Safety: Safe for concurrent use.

func RecordEvidenceRelevanceScore

func RecordEvidenceRelevanceScore(ctx context.Context, score float64)

RecordEvidenceRelevanceScore records a single evidence relevance score.

Inputs:

  • ctx: Context for metric recording.
  • score: The relevance score (0.0-1.0).

Thread Safety: Safe for concurrent use.

func RecordFabricatedCode

func RecordFabricatedCode(ctx context.Context, classification string, similarity float64, snippetLen int)

RecordFabricatedCode records a fabricated code snippet detection.

Inputs:

  • ctx: Context for metric recording.
  • classification: Classification result (verbatim, modified, fabricated).
  • similarity: The similarity score (0.0-1.0).
  • snippetLen: Length of the code snippet in characters.

Thread Safety: Safe for concurrent use.

func RecordFeedbackLoopTriggered

func RecordFeedbackLoopTriggered(ctx context.Context, questionCount int)

RecordFeedbackLoopTriggered records when a feedback loop is triggered after retry exhaustion.

Inputs:

  • ctx: Context for metric recording.
  • questionCount: Number of feedback questions generated.

Thread Safety: Safe for concurrent use.

func RecordLineFabrication

func RecordLineFabrication(ctx context.Context, fabricationType string, filePath string)

RecordLineFabrication records a line number fabrication detection.

Inputs:

  • ctx: Context for metric recording.
  • fabricationType: Type of fabrication (beyond_file_length, symbol_mismatch, invalid_range).
  • filePath: The file path involved in the fabrication.

Thread Safety: Safe for concurrent use.

func RecordPhantomPackage

func RecordPhantomPackage(ctx context.Context, packagePath string)

RecordPhantomPackage records a phantom package path detection.

Inputs:

  • ctx: Context for metric recording.
  • packagePath: The phantom package path that was detected.

Thread Safety: Safe for concurrent use.

func RecordPhantomSymbol

func RecordPhantomSymbol(ctx context.Context, symbolKind string, hasFileContext bool)

RecordPhantomSymbol records a phantom symbol reference detection.

Inputs:

  • ctx: Context for metric recording.
  • symbolKind: Kind of symbol (function, type, etc.).
  • hasFileContext: Whether the symbol had file association.

Thread Safety: Safe for concurrent use.

func RecordPostSynthesisViolation

func RecordPostSynthesisViolation(ctx context.Context, violationType ViolationType, severity Severity, retryCount int)

RecordPostSynthesisViolation records a violation found during post-synthesis verification.

Inputs:

  • ctx: Context for metric recording.
  • violationType: Type of violation.
  • severity: Violation severity.
  • retryCount: Current retry attempt.

Thread Safety: Safe for concurrent use.

func RecordQuantitativeHallucination

func RecordQuantitativeHallucination(ctx context.Context, claimType string, claimed int, actual int)

RecordQuantitativeHallucination records a quantitative hallucination detection.

Inputs:

  • ctx: Context for metric recording.
  • claimType: Type of quantitative claim (file_count, line_count, symbol_count).
  • claimed: The claimed number.
  • actual: The actual number from evidence.

Thread Safety: Safe for concurrent use.

func RecordRejection

func RecordRejection(ctx context.Context, reason string)

RecordRejection records a rejected response.

Inputs:

  • ctx: Context for metric recording.
  • reason: Why the response was rejected.

Thread Safety: Safe for concurrent use.

func RecordRelationshipHallucination

func RecordRelationshipHallucination(ctx context.Context, relationshipKind string, subject string, object string)

RecordRelationshipHallucination records a relationship hallucination detection.

Inputs:

  • ctx: Context for metric recording.
  • relationshipKind: Kind of relationship (import, call, implements).
  • subject: The subject of the relationship claim.
  • object: The object of the relationship claim.

Thread Safety: Safe for concurrent use.

func RecordRepetitionDetectorSuggestion

func RecordRepetitionDetectorSuggestion(currentTool, suggestion string)

RecordRepetitionDetectorSuggestion records when the detector suggests an alternative.

Description:

Tracks when the RepetitionDetector suggests an alternative approach,
e.g., using find_callers instead of repeated Grep calls.

Inputs:

currentTool - The tool that was being considered.
suggestion - The suggestion provided.

Thread Safety: Safe for concurrent use.

func RecordReprompt

func RecordReprompt(ctx context.Context, attempt int, reason string)

RecordReprompt records a re-prompt attempt.

Inputs:

  • ctx: Context for metric recording.
  • attempt: Current attempt number.
  • reason: Reason for the re-prompt.

Thread Safety: Safe for concurrent use.

func RecordRetriesExhausted

func RecordRetriesExhausted(ctx context.Context)

RecordRetriesExhausted records when hallucination retries are exhausted.

Thread Safety: Safe for concurrent use.

func RecordRouterFallback

func RecordRouterFallback(toolName, reason string)

RecordRouterFallback records when router hard-forcing falls back to Main LLM.

Description

This metric tracks when the router selects a tool for hard forcing, but the direct execution fails (either parameter extraction or tool execution), requiring fallback to the Main LLM. High rates indicate parameter extraction or tool execution issues that need investigation.

Inputs

  • toolName: Name of the tool that failed hard forcing (e.g., "explore_package").
  • reason: Reason for fallback (e.g., "param_extraction_failed", "execution_failed").

Thread Safety

Safe for concurrent use.

func RecordRouterHardForced

func RecordRouterHardForced(toolName string, success bool)

RecordRouterHardForced records a router hard-forced tool execution.

Description

This metric tracks when the router selects a tool and the Execute phase bypasses the Main LLM entirely, executing the tool directly. This is the "hard forcing" mechanism introduced in CB-31d to prevent Split-Brain failures.

Inputs

  • toolName: Name of the tool that was hard-forced (e.g., "list_packages").
  • success: Whether the direct execution succeeded or required fallback.

Thread Safety

Safe for concurrent use.

func RecordSemanticCircuitBreakerTrigger

func RecordSemanticCircuitBreakerTrigger(toolName, reason string)

RecordSemanticCircuitBreakerTrigger records when the semantic circuit breaker fires.

Description:

Tracks when the circuit breaker blocks a tool call due to semantic grouping
(semantically similar queries exceeding threshold).

Inputs:

toolName - The tool that was blocked.
reason - The reason for blocking (tool_limit or semantic_group).

Thread Safety: Safe for concurrent use.

func RecordSemanticDrift

func RecordSemanticDrift(ctx context.Context, driftScore float64, questionType string, severity Severity)

RecordSemanticDrift records a semantic drift detection.

Inputs:

  • ctx: Context for metric recording.
  • driftScore: The calculated drift score (0.0-1.0).
  • questionType: The detected question type (list, how, where, etc.).
  • severity: The violation severity.

Thread Safety: Safe for concurrent use.

func RecordSemanticRepetition

func RecordSemanticRepetition(toolName string, similarity float64, comparedTo string)

RecordSemanticRepetition records when a semantic repetition is detected.

Description:

This metric tracks when the agent detects that a proposed tool call is
semantically similar to a recent tool call, indicating potential repetitive
reasoning. CB-30c: Prevents repeated similar queries like Grep("parseConfig")
followed by Grep("parse_config").

Inputs:

toolName - Name of the tool that triggered semantic repetition.
similarity - The Jaccard similarity score that triggered detection (0.0-1.0).
comparedTo - The tool name from history that was similar.

Thread Safety: Safe for concurrent use.

func RecordStructuralClaimNoCitation

func RecordStructuralClaimNoCitation(ctx context.Context, claimType string)

RecordStructuralClaimNoCitation records a structural claim without tool evidence.

Inputs:

  • ctx: Context for metric recording.
  • claimType: Type of structural claim (e.g., "directory", "file_tree").

Thread Safety: Safe for concurrent use.

func RecordSynthesisPromptWithEvidence

func RecordSynthesisPromptWithEvidence(ctx context.Context, evidenceCount int, projectLang string)

RecordSynthesisPromptWithEvidence records that a synthesis prompt was built with evidence.

Inputs:

  • ctx: Context for metric recording.
  • evidenceCount: Number of evidence items included.
  • projectLang: The detected project language.

Thread Safety: Safe for concurrent use.

func RecordTemporalHallucination

func RecordTemporalHallucination(ctx context.Context, claimType string, hasGitEvidence bool)

RecordTemporalHallucination records a temporal hallucination detection.

Inputs:

  • ctx: Context for metric recording.
  • claimType: Type of temporal claim (recency, historical, version, reason).
  • hasGitEvidence: Whether git evidence was available.

Thread Safety: Safe for concurrent use.

func RecordTokenBudgetUsage

func RecordTokenBudgetUsage(sessionID string, usagePercent float64)

RecordTokenBudgetUsage records the current token budget usage.

Description:

Tracks what percentage of the token budget has been used. Useful for
monitoring sessions approaching their limits and triggering synthesis.

Inputs:

sessionID - Session identifier for labeling.
usagePercent - Usage as a fraction (0.0-1.0).

Thread Safety: Safe for concurrent use.

func RecordValidation

func RecordValidation(ctx context.Context, stats ValidationStats)

RecordValidation records aggregate metrics for a validation run.

Inputs:

  • ctx: Context for metric recording.
  • stats: Validation statistics.

Thread Safety: Safe for concurrent use.

func RecordViolation

func RecordViolation(ctx context.Context, v Violation)

RecordViolation records a single violation.

Inputs:

  • ctx: Context for metric recording.
  • v: The violation to record.

Thread Safety: Safe for concurrent use.

func RecordWarningFootnote

func RecordWarningFootnote(ctx context.Context)

RecordWarningFootnote records that a warning footnote was added.

Thread Safety: Safe for concurrent use.

func SetGroundingSpanResult

func SetGroundingSpanResult(span trace.Span, result *Result)

SetGroundingSpanResult sets result attributes on a grounding span.

Inputs:

  • span: The span to update.
  • result: The validation result.

Thread Safety: Safe for concurrent use.

func StartGroundingSpan

func StartGroundingSpan(ctx context.Context, operation string, responseLen int) (context.Context, trace.Span)

StartGroundingSpan creates a span for grounding validation.

Inputs:

  • ctx: Parent context.
  • operation: Operation name.
  • responseLen: Length of response being validated.

Outputs:

  • context.Context: Context with span.
  • trace.Span: The created span.

Thread Safety: Safe for concurrent use.

func StripCodeBlocks

func StripCodeBlocks(text string) string

stripCodeBlocks removes code blocks from text. Exported for testing.

func StructuredOutputSystemPrompt

func StructuredOutputSystemPrompt() string

StructuredOutputSystemPrompt returns the system prompt addition for structured output.

This prompt instructs the LLM to respond in the StructuredResponse JSON format.

Types

type APILibraryChecker

type APILibraryChecker struct {
	// contains filtered or unexported fields
}

APILibraryChecker validates library/API claims in responses.

This checker detects: - Claims about libraries not in project imports - Library confusion (similar libraries mixed up) - API call patterns not found in evidence

Thread Safety: Safe for concurrent use (stateless after construction).

func NewAPILibraryChecker

func NewAPILibraryChecker(config *APILibraryCheckerConfig) *APILibraryChecker

NewAPILibraryChecker creates a new API/library checker.

Inputs:

config - Configuration for the checker (nil uses defaults).

Outputs:

*APILibraryChecker - The configured checker.

func (*APILibraryChecker) Check

func (c *APILibraryChecker) Check(ctx context.Context, input *CheckInput) []Violation

Check implements Checker.

Description:

Extracts library claims from the response and validates them against
the EvidenceIndex.Imports. Detects library hallucinations and confusions.

Thread Safety: Safe for concurrent use.

func (*APILibraryChecker) Name

func (c *APILibraryChecker) Name() string

Name implements Checker.

type APILibraryCheckerConfig

type APILibraryCheckerConfig struct {
	// Enabled determines if API/library checking is active.
	Enabled bool

	// CheckLibraryExists validates that claimed libraries appear in imports.
	// E.g., "uses gorm" but gorm is not in any import statements.
	CheckLibraryExists bool

	// CheckLibraryConfusion detects similar library mixups.
	// E.g., claims "gorm" but evidence shows "sqlx" is imported.
	CheckLibraryConfusion bool

	// CheckAPIUsageInEvidence validates that claimed API patterns exist.
	// E.g., "gorm.Open()" but this call pattern doesn't appear in code.
	CheckAPIUsageInEvidence bool

	// MaxClaimsToCheck limits how many library claims to check per response.
	// Prevents excessive checking on large responses.
	MaxClaimsToCheck int

	// MinLibraryNameLength filters out short names that may be false positives.
	// Library names shorter than this are ignored.
	MinLibraryNameLength int
}

APILibraryCheckerConfig configures the API/library hallucination checker.

func DefaultAPILibraryCheckerConfig

func DefaultAPILibraryCheckerConfig() *APILibraryCheckerConfig

DefaultAPILibraryCheckerConfig returns default API library checker config.

type AbsoluteClaim

type AbsoluteClaim struct {
	// Sentence is the full sentence containing the absolute language.
	Sentence string

	// ClaimType categorizes the absolute (absolute, negative_absolute, universal, exhaustive).
	ClaimType string

	// Keywords are the extracted topic keywords from the claim.
	Keywords []string

	// Position is the character offset in the response.
	Position int

	// MatchedPattern is the pattern that matched.
	MatchedPattern string
}

AbsoluteClaim represents an extracted absolute claim from the response.

type AnchoredSynthesisBuilder

type AnchoredSynthesisBuilder interface {
	// BuildAnchoredSynthesisPrompt creates a synthesis prompt grounded in evidence.
	//
	// Inputs:
	//   ctx - Context for cancellation and metrics.
	//   assembledCtx - The context with tool results and code context.
	//   userQuestion - The user's original question.
	//   projectLang - The detected project language (e.g., "go", "python").
	//
	// Outputs:
	//   string - The anchored synthesis prompt.
	BuildAnchoredSynthesisPrompt(ctx context.Context, assembledCtx *agent.AssembledContext, userQuestion string, projectLang string) string

	// ScoreEvidence scores all tool results for relevance and recency.
	//
	// Inputs:
	//   toolResults - The tool results to score.
	//   userQuestion - The user's question for relevance scoring.
	//
	// Outputs:
	//   []ScoredEvidence - Scored evidence sorted by total score (descending).
	ScoreEvidence(toolResults []agent.ToolResult, userQuestion string) []ScoredEvidence

	// SelectTopEvidence selects the best evidence within token budget.
	//
	// Inputs:
	//   scored - Scored evidence (already sorted).
	//   maxTokens - Maximum token budget.
	//
	// Outputs:
	//   []ScoredEvidence - Selected evidence within budget.
	SelectTopEvidence(scored []ScoredEvidence, maxTokens int) []ScoredEvidence
}

AnchoredSynthesisBuilder builds tool-anchored synthesis prompts.

This interface allows phases to use anchored synthesis without depending on the concrete implementation.

Thread Safety: Implementations must be safe for concurrent use.

type AnchoredSynthesisConfig

type AnchoredSynthesisConfig struct {
	// Enabled determines if anchored synthesis is active.
	Enabled bool

	// MaxEvidenceTokens is the maximum token budget for evidence in prompt.
	MaxEvidenceTokens int

	// MaxResultLength is the maximum length per individual result.
	MaxResultLength int

	// MinResultsToInclude ensures at least this many results are included.
	MinResultsToInclude int

	// RecencyWeight scales the recency score contribution (0.0-1.0).
	RecencyWeight float64

	// RelevanceWeight scales the relevance score contribution (0.0-1.0).
	RelevanceWeight float64

	// IncludeNegativeExamples adds "DO NOT" instructions to the prompt.
	IncludeNegativeExamples bool

	// EnforceLanguage adds language-specific guidance to the prompt.
	EnforceLanguage bool
}

AnchoredSynthesisConfig configures tool-anchored synthesis prompt building.

func DefaultAnchoredSynthesisConfig

func DefaultAnchoredSynthesisConfig() *AnchoredSynthesisConfig

DefaultAnchoredSynthesisConfig returns sensible defaults.

Outputs:

*AnchoredSynthesisConfig - The default configuration.

type AttributeChecker

type AttributeChecker struct {
	// contains filtered or unexported fields
}

AttributeChecker validates attribute claims about code symbols.

This checker detects when the model makes incorrect claims about real code elements: wrong return types, wrong parameter counts, wrong field names, etc.

Thread Safety: Safe for concurrent use after construction.

func NewAttributeChecker

func NewAttributeChecker(config *AttributeCheckerConfig) *AttributeChecker

NewAttributeChecker creates a new attribute checker.

Inputs:

config - Configuration for the checker. If nil, defaults are used.

Outputs:

*AttributeChecker - The configured checker.

func (*AttributeChecker) Check

func (c *AttributeChecker) Check(ctx context.Context, input *CheckInput) []Violation

Check runs the attribute hallucination check.

Description:

Extracts attribute claims from the response and validates them
against symbol details in the evidence index.

Inputs:

ctx - Context for cancellation.
input - The input data for checking.

Outputs:

[]Violation - Any violations found.

Thread Safety: Safe for concurrent use.

func (*AttributeChecker) Name

func (c *AttributeChecker) Name() string

Name returns the checker name for logging and metrics.

type AttributeCheckerConfig

type AttributeCheckerConfig struct {
	// Enabled determines if attribute hallucination checking is active.
	Enabled bool

	// MaxClaimsToCheck limits how many attribute claims to check per response.
	// Prevents excessive checking on large responses.
	MaxClaimsToCheck int

	// IgnorePartialClaims if true, only validates exact count claims.
	// If false, also validates claims like "has fields X and Y".
	IgnorePartialClaims bool

	// MinSymbolLength filters out short symbol names that may be false positives.
	MinSymbolLength int
}

AttributeCheckerConfig configures the attribute hallucination checker.

func DefaultAttributeCheckerConfig

func DefaultAttributeCheckerConfig() *AttributeCheckerConfig

DefaultAttributeCheckerConfig returns default attribute checker config.

type AttributeClaim

type AttributeClaim struct {
	Kind       AttributeClaimKind
	SymbolName string
	Value      string   // The claimed value (e.g., "3" for count, "error" for type)
	Values     []string // For list-based claims (e.g., field names)
	RawText    string   // Original text containing the claim
	Position   int      // Position in response
}

AttributeClaim represents an extracted attribute claim from the response.

type AttributeClaimKind

type AttributeClaimKind int

AttributeClaimKind categorizes the type of attribute claim.

const (
	// ClaimReturnType is a claim about function return type.
	ClaimReturnType AttributeClaimKind = iota
	// ClaimParamCount is a claim about parameter count.
	ClaimParamCount
	// ClaimParamTypes is a claim about parameter types.
	ClaimParamTypes
	// ClaimFieldCount is a claim about struct field count.
	ClaimFieldCount
	// ClaimFieldNames is a claim about specific field names.
	ClaimFieldNames
	// ClaimMethodCount is a claim about interface method count.
	ClaimMethodCount
)

type BehavioralChecker

type BehavioralChecker struct {
	// contains filtered or unexported fields
}

BehavioralChecker validates behavioral claims in responses.

This checker detects fabricated behavioral claims including: - Error handling claims with counter-evidence (swallowed errors) - Validation claims with counter-evidence (no validation visible) - Security claims with counter-evidence (plaintext storage)

Thread Safety: Safe for concurrent use (stateless after construction).

func NewBehavioralChecker

func NewBehavioralChecker(config *BehavioralCheckerConfig) *BehavioralChecker

NewBehavioralChecker creates a new behavioral checker.

Inputs:

config - Configuration for the checker (nil uses defaults).

Outputs:

*BehavioralChecker - The configured checker.

func (*BehavioralChecker) Check

func (c *BehavioralChecker) Check(ctx context.Context, input *CheckInput) []Violation

Check implements Checker.

Description:

Extracts behavioral claims from the response and validates them against
the EvidenceIndex. Detects fabricated behavioral claims by looking for
counter-evidence patterns in the code.

Thread Safety: Safe for concurrent use.

func (*BehavioralChecker) Name

func (c *BehavioralChecker) Name() string

Name implements Checker.

type BehavioralCheckerConfig

type BehavioralCheckerConfig struct {
	// Enabled determines if behavioral hallucination checking is active.
	Enabled bool

	// CheckErrorHandling enables checking claims about error handling.
	// E.g., "logs errors", "returns error to caller", "handles failures gracefully".
	CheckErrorHandling bool

	// CheckValidation enables checking claims about input validation.
	// E.g., "validates input", "sanitizes data", "checks parameters".
	CheckValidation bool

	// CheckSecurity enables checking claims about security behavior.
	// E.g., "encrypts data", "authenticates users", "authorizes access".
	// NOTE: Security claims use RequireCounterEvidence to avoid false positives.
	CheckSecurity bool

	// RequireCounterEvidence if true, only flags claims with explicit counter-evidence.
	// If false, also flags claims with no supporting evidence (more false positives).
	// Default: true (conservative, fewer false positives).
	RequireCounterEvidence bool

	// MaxClaimsToCheck limits how many behavioral claims to check per response.
	// Prevents excessive checking on large responses.
	MaxClaimsToCheck int
}

BehavioralCheckerConfig configures the behavioral hallucination checker.

func DefaultBehavioralCheckerConfig

func DefaultBehavioralCheckerConfig() *BehavioralCheckerConfig

DefaultBehavioralCheckerConfig returns default behavioral checker config.

type BehavioralClaim

type BehavioralClaim struct {
	// Subject is the function/component the claim is about.
	Subject string

	// Behavior is the claimed behavior.
	Behavior string

	// Category is the type of behavioral claim.
	Category BehavioralClaimCategory

	// Position is the character offset in the response.
	Position int

	// Raw is the original matched text.
	Raw string
}

BehavioralClaim represents a parsed behavioral claim from response text.

type BehavioralClaimCategory

type BehavioralClaimCategory int

BehavioralClaimCategory categorizes types of behavioral claims.

const (
	// ClaimErrorHandling is a claim about error handling behavior.
	ClaimErrorHandling BehavioralClaimCategory = iota

	// ClaimValidation is a claim about input validation behavior.
	ClaimValidation

	// ClaimSecurity is a claim about security behavior.
	ClaimSecurity
)

func (BehavioralClaimCategory) String

func (c BehavioralClaimCategory) String() string

String returns the string representation of a BehavioralClaimCategory.

type ChainOfVerification

type ChainOfVerification struct {
	// contains filtered or unexported fields
}

ChainOfVerification performs self-verification on LLM responses.

This is Layer 8 of the anti-hallucination defense system. After the LLM generates a response, it asks the LLM to verify its own claims against the context it was shown.

The verification step helps catch claims the LLM "knows" it made up, since LLMs are often better at verification than generation.

Thread Safety: Safe for concurrent use after construction.

func NewChainOfVerification

func NewChainOfVerification(config *ChainOfVerificationConfig) *ChainOfVerification

NewChainOfVerification creates a new chain-of-verification verifier.

Inputs:

config - Configuration for the verifier. If nil, defaults are used.

Outputs:

*ChainOfVerification - The configured verifier.

func (*ChainOfVerification) BuildVerificationPrompt

func (v *ChainOfVerification) BuildVerificationPrompt(claims []ExtractedClaim) string

BuildVerificationPrompt creates the prompt to ask the LLM to verify claims.

Inputs:

claims - The claims extracted from the response.

Outputs:

string - The verification prompt to send to the LLM.

func (*ChainOfVerification) Check

func (v *ChainOfVerification) Check(ctx context.Context, input *CheckInput) []Violation

Check implements Checker interface for integration with grounding pipeline.

Note: This checker requires an LLM client to perform verification. If no LLM client is configured, it returns nil (no violations). The LLM call is handled externally via BuildVerificationPrompt and ParseVerificationResponse.

Thread Safety: Safe for concurrent use.

func (*ChainOfVerification) ConvertToViolations

func (v *ChainOfVerification) ConvertToViolations(result *VerificationResult) []Violation

ConvertToViolations converts unverified claims to grounding violations.

Inputs:

result - The verification result.

Outputs:

[]Violation - Violations for unverified claims.

func (*ChainOfVerification) Name

func (v *ChainOfVerification) Name() string

Name returns the verifier name for logging and metrics.

func (*ChainOfVerification) ParseVerificationResponse

func (v *ChainOfVerification) ParseVerificationResponse(
	response string,
	originalClaims []ExtractedClaim,
) (*VerificationResult, error)

ParseVerificationResponse parses the LLM's verification response.

Inputs:

response - The raw LLM response text.
originalClaims - The original claims being verified.

Outputs:

*VerificationResult - The parsed verification result.
error - Non-nil if parsing fails.

func (*ChainOfVerification) VerificationSystemPrompt

func (v *ChainOfVerification) VerificationSystemPrompt() string

VerificationSystemPrompt returns the system prompt for verification calls.

type ChainOfVerificationConfig

type ChainOfVerificationConfig struct {
	// Enabled determines if chain-of-verification is active.
	Enabled bool

	// VerifyThreshold is the minimum confidence to keep a claim.
	// Values: "HIGH", "MEDIUM", "LOW"
	VerifyThreshold string

	// MaxClaimsToVerify limits how many claims to verify per response.
	MaxClaimsToVerify int
}

ChainOfVerificationConfig configures the chain-of-verification verifier.

func DefaultChainOfVerificationConfig

func DefaultChainOfVerificationConfig() *ChainOfVerificationConfig

DefaultChainOfVerificationConfig returns sensible defaults.

type CheckInput

type CheckInput struct {
	// Response is the LLM response text.
	Response string

	// UserQuestion is the original user question being answered.
	// Used by SemanticDriftChecker to verify response addresses the question.
	UserQuestion string

	// ProjectRoot is the absolute path to the project root.
	ProjectRoot string

	// ProjectLang is the primary language of the project (e.g., "go", "python").
	ProjectLang string

	// KnownFiles maps file paths that exist in the project to true.
	KnownFiles map[string]bool

	// KnownSymbols maps symbol names that exist in the graph to true.
	KnownSymbols map[string]bool

	// KnownPackages maps package paths that exist in the project to true.
	// Derived from graph file paths (e.g., "pkg/calcs", "cmd/orchestrator").
	// Used by PhantomPackageChecker to validate package path references.
	KnownPackages map[string]bool

	// CodeContext is the code that was shown to the LLM.
	CodeContext []agent.CodeEntry

	// ToolResults are the tool outputs the LLM saw.
	ToolResults []ToolResult

	// EvidenceIndex is the pre-built evidence index (optional).
	EvidenceIndex *EvidenceIndex
}

CheckInput provides all data needed for a grounding check.

type Checker

type Checker interface {
	// Name returns the checker name for logging and metrics.
	Name() string

	// Check runs the grounding check.
	//
	// Inputs:
	//   ctx - Context for cancellation.
	//   input - The input data for checking.
	//
	// Outputs:
	//   []Violation - Any violations found.
	Check(ctx context.Context, input *CheckInput) []Violation
}

Checker is a single grounding check.

Each checker focuses on one aspect of grounding validation. Multiple checkers are composed to form the complete validation pipeline.

Thread Safety: Implementations must be safe for concurrent use.

type CircuitBreakerStateValue

type CircuitBreakerStateValue int

CircuitBreakerStateValue represents circuit breaker states as integers.

const (
	CircuitBreakerClosed   CircuitBreakerStateValue = 0
	CircuitBreakerHalfOpen CircuitBreakerStateValue = 1
	CircuitBreakerOpen     CircuitBreakerStateValue = 2
)

type Citation

type Citation struct {
	// Raw is the original text (e.g., "[main.go:45]").
	Raw string

	// FilePath is the extracted file path.
	FilePath string

	// StartLine is the starting line number.
	StartLine int

	// EndLine is the ending line number (same as StartLine for single line).
	EndLine int

	// Position is the character position in the response.
	Position int
}

Citation represents a parsed citation from LLM response.

type CitationChecker

type CitationChecker struct {
	// contains filtered or unexported fields
}

CitationChecker validates [file:line] citations in responses.

This checker extracts citations from the LLM response and validates: - File exists in the project - File was shown in context - Line number is within valid range

Thread Safety: Safe for concurrent use (stateless after construction).

func NewCitationChecker

func NewCitationChecker(config *CitationCheckerConfig) *CitationChecker

NewCitationChecker creates a new citation checker.

Inputs:

config - Configuration for the checker (nil uses defaults).

Outputs:

*CitationChecker - The configured checker.

func (*CitationChecker) Check

func (c *CitationChecker) Check(ctx context.Context, input *CheckInput) []Violation

Check implements Checker.

Description:

Extracts and validates all [file:line] citations in the response.
Also detects if the response makes claims without citations.

Thread Safety: Safe for concurrent use.

func (*CitationChecker) Name

func (c *CitationChecker) Name() string

Name implements Checker.

type CitationCheckerConfig

type CitationCheckerConfig struct {
	// RequireCitations requires citations for claims.
	RequireCitations bool

	// ValidateFileExists checks that cited files exist.
	ValidateFileExists bool

	// ValidateInContext checks that cited files were in context.
	ValidateInContext bool

	// ValidateLineRange checks that line numbers are valid.
	ValidateLineRange bool
}

CitationCheckerConfig configures the citation checker.

func DefaultCitationCheckerConfig

func DefaultCitationCheckerConfig() *CitationCheckerConfig

DefaultCitationCheckerConfig returns default citation checker config.

type Claim

type Claim struct {
	// Type is the kind of claim.
	Type ClaimType

	// Value is the claimed value (file path, symbol name, etc.).
	Value string

	// RawText is the original text that contained this claim.
	RawText string

	// Position is where in the response this claim was found.
	Position int
}

Claim represents a factual claim extracted from the response.

type ClaimType

type ClaimType int

ClaimType categorizes claims.

const (
	// ClaimFile is a claim about a file.
	ClaimFile ClaimType = iota

	// ClaimSymbol is a claim about a symbol (function, type, etc.).
	ClaimSymbol

	// ClaimFramework is a claim about a framework or library.
	ClaimFramework

	// ClaimLanguage is a claim about a programming language.
	ClaimLanguage
)

type ClaimVerification

type ClaimVerification struct {
	// ClaimID is the ID of the claim being verified.
	ClaimID int `json:"claim_id"`

	// Verified is true if the LLM could verify the claim.
	Verified bool `json:"verified"`

	// Confidence is HIGH, MEDIUM, LOW, or CANNOT_VERIFY.
	Confidence string `json:"confidence"`

	// Citation is the file:line reference (if verified).
	Citation string `json:"citation,omitempty"`

	// Reason explains why the claim couldn't be verified.
	Reason string `json:"reason,omitempty"`
}

ClaimVerification contains the verification result for a single claim.

type CodeBlock

type CodeBlock struct {
	// Content is the code content without the fences.
	Content string

	// Language is the language specifier (e.g., "go", "python"), may be empty.
	Language string

	// Position is the character offset in the response where this block starts.
	Position int

	// EndPosition is the character offset where this block ends.
	EndPosition int

	// ContextBefore is the text immediately before the code block.
	ContextBefore string

	// IsInline indicates if this is inline code (single backticks) vs fenced.
	IsInline bool
}

CodeBlock represents an extracted code block from response.

type CodeBlockMatch

type CodeBlockMatch struct {
	// Block is the original code block.
	Block CodeBlock

	// Classification is how the code was classified.
	Classification CodeClassification

	// Similarity is the best similarity score found (0.0-1.0).
	Similarity float64

	// MatchedFile is the file path where the best match was found.
	MatchedFile string

	// SkipReason explains why the block was skipped (if Classification == Skipped).
	SkipReason string
}

CodeBlockMatch represents the result of matching a code block against evidence.

type CodeClassification

type CodeClassification int

CodeClassification categorizes how a code snippet matches evidence.

const (
	// ClassificationVerbatim means the code is an exact or near-exact match.
	// Similarity >= VerbatimThreshold (default 90%).
	ClassificationVerbatim CodeClassification = iota

	// ClassificationModified means similar code was found but differs.
	// ModifiedThreshold <= Similarity < VerbatimThreshold (default 50-90%).
	ClassificationModified

	// ClassificationFabricated means no matching code in evidence.
	// Similarity < ModifiedThreshold (default < 50%).
	ClassificationFabricated

	// ClassificationSkipped means the code was skipped (suggestion phrase or too short).
	ClassificationSkipped
)

func (CodeClassification) String

func (c CodeClassification) String() string

String returns the string representation of a CodeClassification.

type CodeSnippetChecker

type CodeSnippetChecker struct {
	// contains filtered or unexported fields
}

CodeSnippetChecker validates code blocks in responses against actual file contents.

This checker detects fabricated code snippets: - Code blocks that don't exist in the codebase - "Improved" code shown as original - Plausible-looking code that was never written

Thread Safety: Safe for concurrent use (stateless after construction).

func NewCodeSnippetChecker

func NewCodeSnippetChecker(config *CodeSnippetCheckerConfig) *CodeSnippetChecker

NewCodeSnippetChecker creates a new code snippet checker.

Inputs:

config - Configuration for the checker (nil uses defaults).

Outputs:

*CodeSnippetChecker - The configured checker.

func (*CodeSnippetChecker) Check

func (c *CodeSnippetChecker) Check(ctx context.Context, input *CheckInput) []Violation

Check implements Checker.

Description:

Extracts code blocks from the response and validates them against
the EvidenceIndex.FileContents. Detects fabricated code by comparing
snippets against actual file contents using fuzzy matching.

Thread Safety: Safe for concurrent use.

func (*CodeSnippetChecker) Name

func (c *CodeSnippetChecker) Name() string

Name implements Checker.

type CodeSnippetCheckerConfig

type CodeSnippetCheckerConfig struct {
	// Enabled determines if code snippet checking is active.
	Enabled bool

	// VerbatimThreshold is the minimum similarity for VERBATIM classification.
	// Code above this threshold is considered an exact or near-exact match.
	// Default 0.9 (90% similar = verbatim).
	VerbatimThreshold float64

	// ModifiedThreshold is the minimum similarity for MODIFIED classification.
	// Code between ModifiedThreshold and VerbatimThreshold is similar but differs.
	// Code below this threshold is classified as FABRICATED.
	// Default 0.5 (50-90% = modified, <50% = fabricated).
	ModifiedThreshold float64

	// MinSnippetLength is the minimum code length to validate.
	// Shorter snippets are skipped (too easy to match by chance).
	// Default 20 characters.
	MinSnippetLength int

	// MaxSnippetLength is the maximum code length to validate.
	// Longer snippets are truncated for performance.
	// Default 5000 characters.
	MaxSnippetLength int

	// MaxCodeBlocksToCheck limits how many code blocks to validate per response.
	// Prevents excessive checking on responses with many code blocks.
	// Default 10.
	MaxCodeBlocksToCheck int

	// NormalizationLevel specifies how to normalize code before comparison.
	// Default NormWhitespace (preserve comments).
	NormalizationLevel NormalizationLevel

	// SuggestionContextLines is how many lines before a code block to check
	// for suggestion phrases (e.g., "you could", "for example").
	// Default 5 lines.
	SuggestionContextLines int

	// CheckInlineCode enables checking inline code (single backticks).
	// Default false - inline code is often too short to validate reliably.
	CheckInlineCode bool
}

CodeSnippetCheckerConfig configures the fabricated code snippet checker.

func DefaultCodeSnippetCheckerConfig

func DefaultCodeSnippetCheckerConfig() *CodeSnippetCheckerConfig

DefaultCodeSnippetCheckerConfig returns default code snippet checker config.

type ConfidenceChecker

type ConfidenceChecker struct {
	// contains filtered or unexported fields
}

ConfidenceChecker validates that confident claims are supported by evidence.

This checker detects: - Absolute claims ("always", "never", "all", "none") without evidence - Negative claims ("there is no X") without comprehensive search - Exhaustive claims ("the only way") without full exploration

Thread Safety: Safe for concurrent use (stateless after construction).

func NewConfidenceChecker

func NewConfidenceChecker(config *ConfidenceCheckerConfig) *ConfidenceChecker

NewConfidenceChecker creates a new confidence fabrication checker.

Inputs:

config - Configuration for the checker (nil uses defaults).

Outputs:

*ConfidenceChecker - The configured checker.

func (*ConfidenceChecker) Check

func (c *ConfidenceChecker) Check(ctx context.Context, input *CheckInput) []Violation

Check implements Checker.

Description:

Extracts absolute/confident claims from the response and validates them
against the evidence strength in tool results. Claims with insufficient
evidence generate violations.

Thread Safety: Safe for concurrent use.

func (*ConfidenceChecker) Name

func (c *ConfidenceChecker) Name() string

Name implements Checker.

type ConfidenceCheckerConfig

type ConfidenceCheckerConfig struct {
	// Enabled determines if confidence fabrication checking is active.
	Enabled bool

	// AbsentEvidenceSeverity is the severity for claims with no supporting evidence.
	// Default SeverityCritical - making absolute claims with no search is very problematic.
	AbsentEvidenceSeverity Severity

	// PartialEvidenceSeverity is the severity for claims with partial/truncated evidence.
	// Default SeverityHigh - absolute claims with limited search should be flagged.
	PartialEvidenceSeverity Severity

	// AllowHedgedAbsolutes skips claims that have hedging language nearby.
	// E.g., "it appears that all..." - the hedge negates the absolute.
	// Default true.
	AllowHedgedAbsolutes bool

	// SkipTautologies skips claims that are definitionally true.
	// E.g., "all .go files have .go extension" - always true.
	// Default true.
	SkipTautologies bool

	// SkipCodeBlocks skips absolute language inside code blocks/quotes.
	// Avoids flagging quoted documentation or code comments.
	// Default true.
	SkipCodeBlocks bool

	// MaxClaimsToCheck limits how many absolute claims to check per response.
	// Prevents excessive checking on responses with many absolute words.
	MaxClaimsToCheck int
}

ConfidenceCheckerConfig configures the confidence fabrication checker.

func DefaultConfidenceCheckerConfig

func DefaultConfidenceCheckerConfig() *ConfidenceCheckerConfig

DefaultConfidenceCheckerConfig returns default confidence checker config.

type Config

type Config struct {
	// Enabled determines if grounding checks are enabled.
	Enabled bool

	// RejectOnCritical rejects responses with critical violations.
	RejectOnCritical bool

	// AddFootnoteOnWarning adds warning footnotes to responses.
	AddFootnoteOnWarning bool

	// MaxViolationsBeforeReject triggers rejection at this threshold.
	MaxViolationsBeforeReject int

	// MinConfidence is the minimum confidence to be considered grounded.
	MinConfidence float64

	// MaxResponseScanLength limits how much of the response to scan.
	MaxResponseScanLength int

	// Timeout is the maximum time for grounding checks.
	Timeout time.Duration

	// ShortCircuitOnCritical stops checking after first critical violation.
	ShortCircuitOnCritical bool

	// MaxHallucinationRetries is the circuit breaker limit.
	MaxHallucinationRetries int

	// LanguageCheckerConfig configures the language consistency checker.
	LanguageCheckerConfig *LanguageCheckerConfig

	// CitationCheckerConfig configures the citation checker.
	CitationCheckerConfig *CitationCheckerConfig

	// GroundingCheckerConfig configures the grounding (claim validation) checker.
	GroundingCheckerConfig *GroundingCheckerConfig

	// TMSVerifierConfig configures the TMS-based claim verifier.
	TMSVerifierConfig *TMSVerifierConfig

	// StructuredOutputConfig configures the structured output validator.
	StructuredOutputConfig *StructuredOutputConfig

	// ChainOfVerificationConfig configures the chain-of-verification verifier.
	ChainOfVerificationConfig *ChainOfVerificationConfig

	// MultiSampleConfig configures the multi-sample consistency verifier.
	MultiSampleConfig *MultiSampleConfig

	// StructuralClaimCheckerConfig configures the structural claim checker.
	StructuralClaimCheckerConfig *StructuralClaimCheckerConfig

	// PhantomCheckerConfig configures the phantom file checker.
	PhantomCheckerConfig *PhantomCheckerConfig

	// PostSynthesisConfig configures post-synthesis verification.
	PostSynthesisConfig *PostSynthesisConfig

	// AnchoredSynthesisConfig configures tool-anchored synthesis prompts.
	AnchoredSynthesisConfig *AnchoredSynthesisConfig

	// PhantomSymbolCheckerConfig configures the phantom symbol checker.
	PhantomSymbolCheckerConfig *PhantomSymbolCheckerConfig

	// SemanticDriftCheckerConfig configures the semantic drift checker.
	SemanticDriftCheckerConfig *SemanticDriftCheckerConfig

	// AttributeCheckerConfig configures the attribute hallucination checker.
	AttributeCheckerConfig *AttributeCheckerConfig

	// LineNumberCheckerConfig configures the line number fabrication checker.
	LineNumberCheckerConfig *LineNumberCheckerConfig

	// RelationshipCheckerConfig configures the relationship hallucination checker.
	RelationshipCheckerConfig *RelationshipCheckerConfig

	// BehavioralCheckerConfig configures the behavioral hallucination checker.
	BehavioralCheckerConfig *BehavioralCheckerConfig

	// QuantitativeCheckerConfig configures the quantitative hallucination checker.
	QuantitativeCheckerConfig *QuantitativeCheckerConfig

	// CodeSnippetCheckerConfig configures the fabricated code snippet checker.
	CodeSnippetCheckerConfig *CodeSnippetCheckerConfig

	// APILibraryCheckerConfig configures the API/library hallucination checker.
	APILibraryCheckerConfig *APILibraryCheckerConfig

	// TemporalCheckerConfig configures the temporal hallucination checker.
	TemporalCheckerConfig *TemporalCheckerConfig

	// CrossContextCheckerConfig configures the cross-context confusion checker.
	CrossContextCheckerConfig *CrossContextCheckerConfig

	// ConfidenceCheckerConfig configures the confidence fabrication checker.
	ConfidenceCheckerConfig *ConfidenceCheckerConfig

	// PhantomPackageCheckerConfig configures the phantom package checker.
	PhantomPackageCheckerConfig *PhantomPackageCheckerConfig
}

Config configures the grounding validation behavior.

func DefaultConfig

func DefaultConfig() Config

DefaultConfig returns sensible defaults for grounding configuration.

Outputs:

Config - The default configuration.

type ConsensusResult

type ConsensusResult struct {
	// ConsistentClaims are claims that appeared in threshold+ samples.
	ConsistentClaims []NormalizedClaim

	// InconsistentClaims are claims that appeared in fewer samples.
	InconsistentClaims []NormalizedClaim

	// TotalSamples is the number of samples analyzed.
	TotalSamples int

	// ConsensusRate is the percentage of claims that are consistent.
	ConsensusRate float64
}

ConsensusResult contains the outcome of multi-sample analysis.

type CrossContextChecker

type CrossContextChecker struct {
	// contains filtered or unexported fields
}

CrossContextChecker validates that claims don't mix information from different code locations.

This checker detects: - Attribute confusion: fields from one struct attributed to another - Location mismatch: symbol described with wrong location - Ambiguous references: symbol exists in multiple locations without disambiguation

Thread Safety: Safe for concurrent use (stateless after construction).

func NewCrossContextChecker

func NewCrossContextChecker(config *CrossContextCheckerConfig) *CrossContextChecker

NewCrossContextChecker creates a new cross-context confusion checker.

Inputs:

config - Configuration for the checker (nil uses defaults).

Outputs:

*CrossContextChecker - The configured checker.

func (*CrossContextChecker) Check

func (c *CrossContextChecker) Check(ctx context.Context, input *CheckInput) []Violation

Check implements Checker.

Description:

Extracts location-qualified claims from the response and validates them
against EvidenceIndex.SymbolDetails. Detects when attributes from one
location are incorrectly applied to a symbol at a different location.

Thread Safety: Safe for concurrent use.

func (*CrossContextChecker) Name

func (c *CrossContextChecker) Name() string

Name implements Checker.

type CrossContextCheckerConfig

type CrossContextCheckerConfig struct {
	// Enabled determines if cross-context confusion checking is active.
	Enabled bool

	// CheckAttributeConfusion enables checking for attributes applied to wrong location.
	// E.g., field from pkg/server/Config attributed to pkg/client/Config.
	CheckAttributeConfusion bool

	// CheckLocationClaims enables checking explicit location claims.
	// E.g., "ProcessData in utils" validated against actual location.
	CheckLocationClaims bool

	// FlagAmbiguousReferences flags references to multi-location symbols without disambiguation.
	// E.g., "Config has..." when Config exists in multiple packages.
	FlagAmbiguousReferences bool

	// AmbiguityThreshold is the minimum number of locations before flagging ambiguity.
	// When a symbol exists in fewer than this many locations, no ambiguity warning.
	// Default 2 - flag when 2+ locations exist.
	AmbiguityThreshold int

	// MaxClaimsToCheck limits how many location claims to check per response.
	// Prevents excessive checking on large responses.
	MaxClaimsToCheck int
}

CrossContextCheckerConfig configures the cross-context confusion checker.

func DefaultCrossContextCheckerConfig

func DefaultCrossContextCheckerConfig() *CrossContextCheckerConfig

DefaultCrossContextCheckerConfig returns default cross-context checker config.

type DefaultAnchoredSynthesisBuilder

type DefaultAnchoredSynthesisBuilder struct {
	// contains filtered or unexported fields
}

DefaultAnchoredSynthesisBuilder implements AnchoredSynthesisBuilder.

func NewAnchoredSynthesisBuilder

func NewAnchoredSynthesisBuilder(config *AnchoredSynthesisConfig) *DefaultAnchoredSynthesisBuilder

NewAnchoredSynthesisBuilder creates a new anchored synthesis builder.

Inputs:

config - Configuration for the builder. Uses defaults if nil.

Outputs:

*DefaultAnchoredSynthesisBuilder - The configured builder.

func (*DefaultAnchoredSynthesisBuilder) BuildAnchoredSynthesisPrompt

func (b *DefaultAnchoredSynthesisBuilder) BuildAnchoredSynthesisPrompt(ctx context.Context, assembledCtx *agent.AssembledContext, userQuestion string, projectLang string) string

BuildAnchoredSynthesisPrompt implements AnchoredSynthesisBuilder.

Description:

Creates a synthesis prompt that explicitly grounds the LLM in the
tool evidence, includes negative examples to prevent hallucination,
and enforces the project language.

Inputs:

ctx - Context for cancellation and metrics.
assembledCtx - The context with tool results and code context.
userQuestion - The user's original question.
projectLang - The detected project language (e.g., "go", "python").

Outputs:

string - The anchored synthesis prompt.

Thread Safety: Safe for concurrent use (stateless function).

func (*DefaultAnchoredSynthesisBuilder) ScoreEvidence

func (b *DefaultAnchoredSynthesisBuilder) ScoreEvidence(toolResults []agent.ToolResult, userQuestion string) []ScoredEvidence

ScoreEvidence implements AnchoredSynthesisBuilder.

Description:

Scores tool results based on recency (position in slice) and relevance
(keyword match with user question, presence of file paths, code snippets).

Inputs:

toolResults - The tool results to score.
userQuestion - The user's question for relevance scoring.

Outputs:

[]ScoredEvidence - Scored evidence sorted by total score (descending).

Thread Safety: Safe for concurrent use (stateless function).

func (*DefaultAnchoredSynthesisBuilder) SelectTopEvidence

func (b *DefaultAnchoredSynthesisBuilder) SelectTopEvidence(scored []ScoredEvidence, maxTokens int) []ScoredEvidence

SelectTopEvidence implements AnchoredSynthesisBuilder.

Description:

Selects evidence within the token budget, ensuring at least
MinResultsToInclude results are included.

Inputs:

scored - Scored evidence (already sorted by score descending).
maxTokens - Maximum token budget.

Outputs:

[]ScoredEvidence - Selected evidence within budget.

Thread Safety: Safe for concurrent use (stateless function).

type DefaultGrounder

type DefaultGrounder struct {
	// contains filtered or unexported fields
}

DefaultGrounder orchestrates multiple grounding checks.

This is the main entry point for response validation. It coordinates all registered Checkers and aggregates their violations into a single Result.

Thread Safety: Safe for concurrent use after construction.

func NewDefaultGrounder

func NewDefaultGrounder(config Config, checkers ...Checker) *DefaultGrounder

NewDefaultGrounder creates a new DefaultGrounder with the given checkers.

Inputs:

config - Configuration for grounding behavior.
checkers - The checkers to run (executed in order).

Outputs:

*DefaultGrounder - The configured grounder.

func (*DefaultGrounder) GenerateFootnote

func (g *DefaultGrounder) GenerateFootnote(result *Result) string

GenerateFootnote implements Grounder.

Description:

Creates a warning footnote for responses with warnings but no critical
violations. Returns empty string if no footnote is needed.

Thread Safety: Safe for concurrent use.

func (*DefaultGrounder) GenerateStricterPrompt

func (g *DefaultGrounder) GenerateStricterPrompt(basePrompt string, violations []Violation, level StrictnessLevel) string

GenerateStricterPrompt creates a prompt with increased strictness.

Description:

Modifies the base synthesis prompt to include violation feedback
and strictness guidance based on the current strictness level.

Inputs:

basePrompt - The original synthesis prompt.
violations - The violations from the previous attempt.
level - The strictness level to apply.

Outputs:

string - The enhanced prompt with strictness guidance.

Thread Safety: Safe for concurrent use (stateless function).

func (*DefaultGrounder) ShouldReject

func (g *DefaultGrounder) ShouldReject(result *Result) bool

ShouldReject implements Grounder.

Description:

Determines if a validation result warrants rejecting the response.

Thread Safety: Safe for concurrent use.

func (*DefaultGrounder) Validate

func (g *DefaultGrounder) Validate(ctx context.Context, response string, assembledCtx *agent.AssembledContext) (*Result, error)

Validate implements Grounder.

Description:

Runs all registered checkers against the response and aggregates
violations. Supports short-circuit on critical violations if configured.

Inputs:

ctx - Context for cancellation and timeout.
response - The LLM response content to validate.
assembledCtx - The context that was given to the LLM.

Outputs:

*Result - The aggregated validation result.
error - Non-nil only if validation itself fails (not for violations).

Thread Safety: Safe for concurrent use.

func (*DefaultGrounder) VerifyPostSynthesis

func (g *DefaultGrounder) VerifyPostSynthesis(ctx context.Context, response string, assembledCtx *agent.AssembledContext, retryCount int) (*PostSynthesisResult, error)

VerifyPostSynthesis checks a synthesized response for hallucinations.

Description:

Runs a subset of grounding checkers specifically designed to catch
hallucinations in synthesized responses. These checkers focus on:
- Structural claims (fabricated directories/files)
- Phantom files (references to non-existent files)
- Language confusion (wrong-language patterns)

Inputs:

ctx - Context for cancellation.
response - The synthesized response text.
assembledCtx - The context that was given to the LLM.
retryCount - Current retry attempt (0 = first attempt).

Outputs:

*PostSynthesisResult - The verification result.
error - Non-nil only if verification itself fails.

Thread Safety: Safe for concurrent use.

type EvidenceIndex

type EvidenceIndex struct {
	// Files that were shown (normalized paths).
	Files map[string]bool

	// FileBasenames maps base filenames for convenience.
	FileBasenames map[string]bool

	// Symbols (function/type names) that appeared in context.
	Symbols map[string]bool

	// SymbolDetails maps symbol names to detailed information.
	// Key: symbol name, Value: list of SymbolInfo for all locations.
	// This provides richer information than Symbols map for validation.
	SymbolDetails map[string][]SymbolInfo

	// Frameworks/libraries mentioned in shown code.
	Frameworks map[string]bool

	// Languages of shown code.
	Languages map[string]bool

	// FileContents maps file paths to their content.
	FileContents map[string]string

	// FileLines maps file paths to their total line count.
	// Populated during evidence building by counting newlines in content.
	// Used by LineNumberChecker to validate cited line numbers.
	FileLines map[string]int

	// Imports maps file paths to their imported package paths.
	// Key: normalized file path, Value: list of import paths.
	// Used by RelationshipChecker to validate import claims.
	Imports map[string][]ImportInfo

	// CallsWithin tracks function calls within shown code.
	// Key: "FunctionName" or "pkg.FunctionName", Value: list of called functions.
	// Only populated for functions defined in evidence.
	// Used by RelationshipChecker to validate call relationship claims.
	CallsWithin map[string][]string

	// RawContent is concatenated content for substring matching.
	RawContent string
}

EvidenceIndex tracks exactly what the LLM was shown.

This is built BEFORE sending to LLM and used AFTER receiving response to validate that claims are grounded in actual evidence.

func NewEvidenceIndex

func NewEvidenceIndex() *EvidenceIndex

NewEvidenceIndex creates a new empty evidence index.

type EvidenceStrength

type EvidenceStrength int

EvidenceStrength indicates how well the available evidence supports a claim.

const (
	// EvidenceAbsent means no tool searched for this topic.
	EvidenceAbsent EvidenceStrength = iota

	// EvidencePartial means tool searched but results were truncated/limited.
	EvidencePartial

	// EvidenceStrong means comprehensive tool search was performed.
	EvidenceStrong
)

func (EvidenceStrength) String

func (e EvidenceStrength) String() string

String returns a string representation of EvidenceStrength.

type ExtractedClaim

type ExtractedClaim struct {
	// ID is a unique identifier for this claim.
	ID int

	// Statement is the text of the claim.
	Statement string

	// File is the file referenced (if any).
	File string

	// Line is the line number referenced (if any).
	Line int

	// RawText is the original text containing this claim.
	RawText string
}

ExtractedClaim represents a claim extracted for verification.

func ExtractClaimsForVerification

func ExtractClaimsForVerification(response string) []ExtractedClaim

ExtractClaimsForVerification extracts claims from a response for verification.

This uses the same extraction logic as GroundingChecker but formats claims for the verification prompt.

Inputs:

response - The LLM response text.

Outputs:

[]ExtractedClaim - The extracted claims.

type FileManifest

type FileManifest struct {
	// contains filtered or unexported fields
}

FileManifest tracks files that exist in a project directory.

This provides a cached view of the project's file structure for validating that file references in LLM responses actually exist. The manifest can be refreshed on-demand or checked for staleness.

Thread Safety: Safe for concurrent use.

func NewFileManifest

func NewFileManifest(extensions []string) *FileManifest

NewFileManifest creates a new empty file manifest.

Description:

Creates an uninitialized manifest. Call ScanDir to populate it
with files from a directory.

Inputs:

  • extensions: File extensions to include (nil means DefaultCodeExtensions).

Outputs:

  • *FileManifest: The empty manifest.

Thread Safety: Safe for concurrent use after construction.

func (*FileManifest) Contains

func (m *FileManifest) Contains(path string) bool

Contains checks if a file path exists in the manifest.

Description:

Checks the manifest for the given path. Handles path normalization
(leading ./, /, backslashes) and checks both full path and basename.

Inputs:

  • path: The file path to check.

Outputs:

  • bool: True if the file exists in the manifest.

Thread Safety: Safe for concurrent use (acquires read lock).

func (*FileManifest) ExtensionCounts

func (m *FileManifest) ExtensionCounts() map[string]int

ExtensionCounts returns a copy of the extension count map.

Description:

Returns a map of file extensions to their count in the manifest.
This is useful for detecting the primary language of a project.
Extensions include the leading dot (e.g., ".go", ".py").

Outputs:

  • map[string]int: Copy of the extension counts map.

Thread Safety: Safe for concurrent use (acquires read lock).

func (*FileManifest) FileCount

func (m *FileManifest) FileCount() int

FileCount returns the number of files in the manifest.

Thread Safety: Safe for concurrent use (acquires read lock).

func (*FileManifest) IsStale

func (m *FileManifest) IsStale(ttl time.Duration) bool

IsStale returns true if the manifest is older than the given TTL.

Description:

Checks if the manifest should be refreshed based on age.
An uninitialized manifest (zero LoadedAt) is always stale.

Inputs:

  • ttl: The maximum age before the manifest is considered stale.

Outputs:

  • bool: True if the manifest is stale.

Thread Safety: Safe for concurrent use (acquires read lock).

func (*FileManifest) LoadedAt

func (m *FileManifest) LoadedAt() time.Time

LoadedAt returns when the manifest was last loaded.

Thread Safety: Safe for concurrent use (acquires read lock).

func (*FileManifest) Root

func (m *FileManifest) Root() string

Root returns the root directory of the manifest.

Thread Safety: Safe for concurrent use (acquires read lock).

func (*FileManifest) ScanDir

func (m *FileManifest) ScanDir(ctx context.Context, root string) error

ScanDir populates the manifest by scanning a directory recursively.

Description:

Walks the directory tree starting at root and records all files
matching the configured extensions. Previous contents are cleared.
Skips hidden directories (starting with .) and common non-code
directories (node_modules, vendor, __pycache__, etc.).

Inputs:

  • ctx: Context for cancellation.
  • root: The directory to scan (absolute path).

Outputs:

  • error: Non-nil if scanning fails.

Thread Safety: Safe for concurrent use (acquires write lock).

func (*FileManifest) ToKnownFiles

func (m *FileManifest) ToKnownFiles() map[string]bool

ToKnownFiles returns a copy of the files map for use in CheckInput.

Description:

Returns a copy of the internal files map that can be used to
populate CheckInput.KnownFiles for grounding checks.

Outputs:

  • map[string]bool: Copy of the files map.

Thread Safety: Safe for concurrent use (acquires read lock).

type Grounder

type Grounder interface {
	// Validate checks if a response is grounded in project reality.
	//
	// Inputs:
	//   ctx - Context for cancellation.
	//   response - The LLM response content.
	//   assembledCtx - The context that was given to the LLM.
	//
	// Outputs:
	//   *Result - The validation result.
	//   error - Non-nil only if validation itself fails.
	Validate(ctx context.Context, response string, assembledCtx *agent.AssembledContext) (*Result, error)

	// ShouldReject determines if the result warrants rejection.
	//
	// Inputs:
	//   result - The validation result.
	//
	// Outputs:
	//   bool - True if the response should be rejected.
	ShouldReject(result *Result) bool

	// GenerateFootnote creates a warning footnote for questionable responses.
	//
	// Inputs:
	//   result - The validation result.
	//
	// Outputs:
	//   string - A footnote to append to the response, or empty if not needed.
	GenerateFootnote(result *Result) string
}

Grounder validates LLM responses against project reality.

Implementations validate that responses are grounded in actual code and do not contain hallucinated content.

Thread Safety: Implementations must be safe for concurrent use.

func NewGrounder

func NewGrounder(config *Config) Grounder

NewGrounder creates a fully configured Grounder with all detection checkers.

Inputs:

config - Configuration for grounding behavior. Nil uses defaults.

Outputs:

Grounder - A configured grounder ready for use.

Example:

grounder := grounding.NewGrounder(nil) // use defaults
result, err := grounder.Validate(ctx, response, assembledCtx)
if grounder.ShouldReject(result) {
    return ErrHallucination
}

func NewGrounderWithCheckers

func NewGrounderWithCheckers(config Config, checkers ...Checker) Grounder

NewGrounderWithCheckers creates a Grounder with custom checkers.

Use this when you need to add custom checkers or want fine-grained control over which checkers are used.

Inputs:

config - Configuration for grounding behavior.
checkers - The checkers to use.

Outputs:

Grounder - A configured grounder.

type GroundingChecker

type GroundingChecker struct {
	// contains filtered or unexported fields
}

GroundingChecker validates that claims in responses are grounded in evidence.

This is Layer 4 of the anti-hallucination defense system. It extracts claims about files, symbols, and frameworks from the LLM response and verifies each claim against the evidence index.

Thread Safety: Safe for concurrent use after construction.

func NewGroundingChecker

func NewGroundingChecker(config *GroundingCheckerConfig) *GroundingChecker

NewGroundingChecker creates a new grounding checker.

Inputs:

config - Configuration for the checker. If nil, defaults are used.

Outputs:

*GroundingChecker - The configured checker.

func (*GroundingChecker) Check

func (c *GroundingChecker) Check(ctx context.Context, input *CheckInput) []Violation

Check implements Checker.

Description:

Extracts claims from the response and validates each against the
evidence index. Returns violations for claims that are not grounded.

Thread Safety: Safe for concurrent use.

func (*GroundingChecker) Name

func (c *GroundingChecker) Name() string

Name returns the checker name for logging and metrics.

type GroundingCheckerConfig

type GroundingCheckerConfig struct {
	// CheckFiles enables file reference validation.
	CheckFiles bool

	// CheckSymbols enables symbol reference validation.
	CheckSymbols bool

	// CheckFrameworks enables framework reference validation.
	CheckFrameworks bool

	// SymbolSeverity is the severity for ungrounded symbols.
	// Defaults to warning since symbol names can be generic.
	SymbolSeverity Severity

	// FileSeverity is the severity for ungrounded file references.
	// Defaults to critical since files should definitely exist.
	FileSeverity Severity

	// FrameworkSeverity is the severity for ungrounded framework mentions.
	// Defaults to critical since this strongly indicates hallucination.
	FrameworkSeverity Severity
}

GroundingCheckerConfig configures the grounding checker.

func DefaultGroundingCheckerConfig

func DefaultGroundingCheckerConfig() *GroundingCheckerConfig

DefaultGroundingCheckerConfig returns a config with sensible defaults.

type ImportInfo

type ImportInfo struct {
	// Path is the import path (e.g., "github.com/pkg/errors").
	Path string

	// Alias is the local name if aliased, or the package name from path.
	// For `import foo "pkg/bar"`, Alias is "foo".
	// For `import "pkg/bar"`, Alias is "bar".
	Alias string
}

ImportInfo tracks an import statement with optional alias.

type LanguageChecker

type LanguageChecker struct {
	// contains filtered or unexported fields
}

LanguageChecker detects wrong-language content in responses.

This checker identifies when the LLM describes code in a language different from the project's actual language. For example, detecting Python/Flask patterns in a Go project response.

Thread Safety: Safe for concurrent use (stateless after construction).

func NewLanguageChecker

func NewLanguageChecker(config *LanguageCheckerConfig) *LanguageChecker

NewLanguageChecker creates a new language consistency checker.

Inputs:

config - Configuration for the checker (nil uses defaults).

Outputs:

*LanguageChecker - The configured checker.

func (*LanguageChecker) Check

func (c *LanguageChecker) Check(ctx context.Context, input *CheckInput) []Violation

Check implements Checker.

Description:

Scans the response for language-specific patterns and flags
any patterns that don't match the project's language.

IMPORTANT: If a language is present in the EvidenceIndex (meaning files of that
language were actually shown to the LLM), we don't flag that language as a
violation. This supports hybrid repos (e.g., Go backend with Python scripts).

Also checks for blocked frameworks - language-specific tools/frameworks
that should never appear in responses about the project language.

Thread Safety: Safe for concurrent use.

func (*LanguageChecker) Name

func (c *LanguageChecker) Name() string

Name implements Checker.

type LanguageCheckerConfig

type LanguageCheckerConfig struct {
	// WeightThreshold is the accumulated weight needed to trigger a violation.
	WeightThreshold float64

	// EnablePython enables Python pattern detection.
	EnablePython bool

	// EnableJavaScript enables JavaScript/TypeScript pattern detection.
	EnableJavaScript bool

	// EnableGo enables Go pattern detection.
	EnableGo bool
}

LanguageCheckerConfig configures the language consistency checker.

func DefaultLanguageCheckerConfig

func DefaultLanguageCheckerConfig() *LanguageCheckerConfig

DefaultLanguageCheckerConfig returns default language checker config.

type LanguageMarker

type LanguageMarker struct {
	// Pattern is the regex to match.
	Pattern *regexp.Regexp

	// Description is a human-readable description.
	Description string

	// Weight is how strongly this indicates the language (0.0-1.0).
	Weight float64
}

LanguageMarker defines a pattern that indicates a specific language.

type LibraryClaim

type LibraryClaim struct {
	// Type is the kind of library claim.
	Type LibraryClaimType

	// Library is the claimed library name (e.g., "gorm", "gin", "sqlx").
	Library string

	// Method is the claimed method/function (for API calls, e.g., "Open", "Get").
	Method string

	// Position is the character offset in the response.
	Position int

	// Raw is the original matched text.
	Raw string
}

LibraryClaim represents a parsed library/API claim from response text.

type LibraryClaimType

type LibraryClaimType int

LibraryClaimType categorizes types of library claims.

const (
	// ClaimLibraryUsage is a claim like "uses library X" or "depends on X".
	ClaimLibraryUsage LibraryClaimType = iota

	// ClaimAPICall is a claim like "calls pkg.Function()" or "uses pkg.Method".
	ClaimAPICall
)

func (LibraryClaimType) String

func (c LibraryClaimType) String() string

String returns the string representation of a LibraryClaimType.

type LineCitation

type LineCitation struct {
	// Raw is the original matched text.
	Raw string

	// FilePath is the file path (may be basename only).
	FilePath string

	// StartLine is the cited starting line (1-indexed).
	StartLine int

	// EndLine is the cited ending line (same as StartLine for single-line citations).
	EndLine int

	// Position is the character offset in the response where this was found.
	Position int

	// SymbolName is the associated symbol name, if any.
	SymbolName string
}

LineCitation represents a parsed line citation from response text.

type LineNumberChecker

type LineNumberChecker struct {
	// contains filtered or unexported fields
}

LineNumberChecker validates line number citations in responses.

This checker detects fabricated line numbers including: - Lines beyond file length - Symbol location mismatches (claim says line 42, symbol is at 127) - Invalid ranges (end < start)

Thread Safety: Safe for concurrent use (stateless after construction).

func NewLineNumberChecker

func NewLineNumberChecker(config *LineNumberCheckerConfig) *LineNumberChecker

NewLineNumberChecker creates a new line number checker.

Inputs:

config - Configuration for the checker (nil uses defaults).

Outputs:

*LineNumberChecker - The configured checker.

func (*LineNumberChecker) Check

func (c *LineNumberChecker) Check(ctx context.Context, input *CheckInput) []Violation

Check implements Checker.

Description:

Extracts line citations from the response and validates them against
the EvidenceIndex. Detects fabricated line numbers including:
- Lines beyond file length
- Symbol location mismatches
- Invalid line ranges

Thread Safety: Safe for concurrent use.

func (*LineNumberChecker) Name

func (c *LineNumberChecker) Name() string

Name implements Checker.

type LineNumberCheckerConfig

type LineNumberCheckerConfig struct {
	// Enabled determines if line number checking is active.
	Enabled bool

	// LineTolerance is the base tolerance for single line citations.
	// A citation is considered valid if within ±LineTolerance of actual line.
	LineTolerance int

	// RangeTolerance is the base tolerance for range citations.
	// Range start/end are each allowed ±RangeTolerance variance.
	RangeTolerance int

	// StrictMode requires exact line matches when true (tolerance = 0).
	StrictMode bool

	// ScaleTolerance adjusts tolerance based on file size.
	// Large files (>500 lines) get 2x tolerance.
	// Small files (<100 lines) get 0.5x tolerance.
	ScaleTolerance bool

	// MaxCitationsToCheck limits how many citations to check per response.
	// Prevents excessive checking on responses with many citations.
	MaxCitationsToCheck int
}

LineNumberCheckerConfig configures the line number fabrication checker.

func DefaultLineNumberCheckerConfig

func DefaultLineNumberCheckerConfig() *LineNumberCheckerConfig

DefaultLineNumberCheckerConfig returns default line number checker config.

type LocationClaim

type LocationClaim struct {
	// Symbol is the symbol name being referenced.
	Symbol string

	// Location is the claimed location (package, file, or partial path).
	Location string

	// Attribute is the claimed attribute (field, parameter, etc.).
	// Empty if no specific attribute is claimed.
	Attribute string

	// Position is the character offset in the response.
	Position int

	// Raw is the matched text.
	Raw string
}

LocationClaim represents a claim about a symbol at a specific location.

type MultiSampleConfig

type MultiSampleConfig struct {
	// Enabled determines if multi-sample verification is active.
	Enabled bool

	// NumSamples is the number of samples to generate (default: 3).
	NumSamples int

	// Temperature is the sampling temperature (default: 0.7).
	Temperature float64

	// ConsensusThreshold is the minimum samples for consensus (default: 2).
	ConsensusThreshold int
}

MultiSampleConfig configures the multi-sample consistency verifier.

func DefaultMultiSampleConfig

func DefaultMultiSampleConfig() *MultiSampleConfig

DefaultMultiSampleConfig returns sensible defaults.

type MultiSampleVerifier

type MultiSampleVerifier struct {
	// contains filtered or unexported fields
}

MultiSampleVerifier generates multiple responses and finds consensus.

This is Layer 7 of the anti-hallucination defense system. It generates N responses at non-zero temperature and only keeps claims that appear consistently across samples. Inconsistent claims indicate hallucination.

Key principle: Hallucinations are inconsistent across samples. True facts tend to appear in multiple samples.

Cost: N LLM calls per verification (use sparingly for high-stakes questions)

Thread Safety: Safe for concurrent use after construction.

func NewMultiSampleVerifier

func NewMultiSampleVerifier(config *MultiSampleConfig) *MultiSampleVerifier

NewMultiSampleVerifier creates a new multi-sample verifier.

Inputs:

config - Configuration for the verifier. If nil, defaults are used.

Outputs:

*MultiSampleVerifier - The configured verifier.

func (*MultiSampleVerifier) AnalyzeSamples

func (v *MultiSampleVerifier) AnalyzeSamples(samples []string) *ConsensusResult

AnalyzeSamples performs multi-sample consistency analysis.

Description:

Extracts claims from each sample, normalizes them for comparison,
and identifies which claims appear consistently across samples.

Inputs:

samples - The response samples to analyze.

Outputs:

*ConsensusResult - The consensus analysis result.

func (*MultiSampleVerifier) Check

func (v *MultiSampleVerifier) Check(ctx context.Context, input *CheckInput) []Violation

Check implements Checker interface for integration with grounding pipeline.

Note: This checker doesn't call LLM directly. It provides tools for analyzing multiple pre-generated samples. Use AnalyzeSamples to perform the actual consistency check.

Thread Safety: Safe for concurrent use.

func (*MultiSampleVerifier) ConvertToViolations

func (v *MultiSampleVerifier) ConvertToViolations(result *ConsensusResult) []Violation

ConvertToViolations converts inconsistent claims to grounding violations.

Inputs:

result - The consensus result.

Outputs:

[]Violation - Violations for inconsistent claims.

func (*MultiSampleVerifier) GetConfig

func (v *MultiSampleVerifier) GetConfig() *MultiSampleConfig

GetConfig returns the verifier configuration.

func (*MultiSampleVerifier) Name

func (v *MultiSampleVerifier) Name() string

Name returns the verifier name for logging and metrics.

type NormalizationLevel

type NormalizationLevel int

NormalizationLevel specifies how code is normalized before comparison.

const (
	// NormNone compares code as-is without any normalization.
	NormNone NormalizationLevel = iota

	// NormWhitespace normalizes whitespace only (tabs, spaces, newlines).
	// Default level - preserves comments which may be semantically important.
	NormWhitespace

	// NormFull removes comments and normalizes all whitespace.
	// Use sparingly as comments can be meaningful for doc queries.
	NormFull
)

type NormalizedClaim

type NormalizedClaim struct {
	// Original is the original claim text.
	Original string

	// Normalized is the normalized form for comparison.
	Normalized string

	// Type is the claim type (file, symbol, framework).
	Type ClaimType

	// Value is the extracted value.
	Value string

	// SampleIndices tracks which samples contained this claim.
	SampleIndices []int
}

NormalizedClaim is a claim normalized for comparison.

type PhantomCheckerConfig

type PhantomCheckerConfig struct {
	// Enabled determines if phantom file checking is active.
	Enabled bool

	// Extensions are the file extensions to check (e.g., ".go", ".py").
	// Empty means check all common code extensions.
	Extensions []string

	// MaxRefsToCheck limits how many file references to check per response.
	// Prevents excessive checking on large responses.
	MaxRefsToCheck int
}

PhantomCheckerConfig configures the phantom file checker.

func DefaultPhantomCheckerConfig

func DefaultPhantomCheckerConfig() *PhantomCheckerConfig

DefaultPhantomCheckerConfig returns default phantom checker config.

type PhantomFileChecker

type PhantomFileChecker struct {
	// contains filtered or unexported fields
}

PhantomFileChecker detects references to files that don't exist.

This checker identifies when the LLM references files that are not present in the codebase. Phantom file references are a strong signal of hallucination because the model is inventing file paths that don't exist.

This complements CitationChecker: CitationChecker validates citations that ARE in context, while PhantomFileChecker catches references to files that don't exist at all.

Thread Safety: Safe for concurrent use (stateless after construction).

func NewPhantomFileChecker

func NewPhantomFileChecker(config *PhantomCheckerConfig) *PhantomFileChecker

NewPhantomFileChecker creates a new phantom file checker.

Description:

Creates a checker that detects references to non-existent files.
Uses CheckInput.KnownFiles to validate file existence.

Inputs:

  • config: Configuration for the checker (nil uses defaults).

Outputs:

  • *PhantomFileChecker: The configured checker.

Thread Safety: Safe for concurrent use.

func (*PhantomFileChecker) Check

func (c *PhantomFileChecker) Check(ctx context.Context, input *CheckInput) []Violation

Check implements Checker.

Description:

Extracts file references from the response and validates they exist
in KnownFiles. Non-existent file references are flagged as
ViolationPhantomFile with CRITICAL severity.

Inputs:

  • ctx: Context for cancellation.
  • input: The check input containing response and KnownFiles.

Outputs:

  • []Violation: Any violations found.

Thread Safety: Safe for concurrent use.

func (*PhantomFileChecker) Name

func (c *PhantomFileChecker) Name() string

Name implements Checker.

type PhantomPackageChecker

type PhantomPackageChecker struct {
	// contains filtered or unexported fields
}

PhantomPackageChecker detects references to packages that don't exist.

This checker identifies when the LLM references package paths like pkg/config or cmd/database that are not present in the codebase. This is distinct from PhantomSymbolChecker which validates individual symbols within files.

Thread Safety: Safe for concurrent use (stateless after construction).

func NewPhantomPackageChecker

func NewPhantomPackageChecker(config *PhantomPackageCheckerConfig) *PhantomPackageChecker

NewPhantomPackageChecker creates a new phantom package checker.

Description:

Creates a checker that detects references to non-existent packages.
Uses CheckInput.KnownPackages for validation.

Inputs:

  • config: Configuration for the checker (nil uses defaults).

Outputs:

  • *PhantomPackageChecker: The configured checker.

Thread Safety: Safe for concurrent use.

func (*PhantomPackageChecker) Check

func (c *PhantomPackageChecker) Check(ctx context.Context, input *CheckInput) []Violation

Check implements Checker.

Description:

Extracts package path references from the response and validates they exist
in KnownPackages or are standard library packages. Non-existent package
references are flagged as ViolationPhantomPackage.

Inputs:

  • ctx: Context for cancellation. Must not be nil.
  • input: The check input containing response and package data. Must not be nil.

Outputs:

  • []Violation: Any violations found.

Thread Safety: Safe for concurrent use.

func (*PhantomPackageChecker) Name

func (c *PhantomPackageChecker) Name() string

Name implements Checker.

type PhantomPackageCheckerConfig

type PhantomPackageCheckerConfig struct {
	// Enabled determines if phantom package checking is active.
	Enabled bool

	// MaxPackagesToCheck limits how many package references to check.
	// Prevents excessive checking on large responses.
	MaxPackagesToCheck int

	// MinPackageLength filters out short paths (e.g., "pkg" alone).
	// Package paths shorter than this are ignored.
	MinPackageLength int

	// CheckGoPackages enables checking Go-style pkg/cmd/internal paths.
	CheckGoPackages bool

	// CheckPythonPackages enables checking Python-style dot.paths.
	CheckPythonPackages bool
}

PhantomPackageCheckerConfig configures the phantom package checker.

func DefaultPhantomPackageCheckerConfig

func DefaultPhantomPackageCheckerConfig() *PhantomPackageCheckerConfig

DefaultPhantomPackageCheckerConfig returns default phantom package checker config.

type PhantomSymbolChecker

type PhantomSymbolChecker struct {
	// contains filtered or unexported fields
}

PhantomSymbolChecker detects references to symbols that don't exist.

This checker identifies when the LLM references functions, types, variables, or constants that are not present in the codebase. Unlike PhantomFileChecker which validates file existence, this validates symbol existence within files.

Thread Safety: Safe for concurrent use (stateless after construction).

func NewPhantomSymbolChecker

func NewPhantomSymbolChecker(config *PhantomSymbolCheckerConfig) *PhantomSymbolChecker

NewPhantomSymbolChecker creates a new phantom symbol checker.

Description:

Creates a checker that detects references to non-existent symbols.
Uses CheckInput.KnownSymbols and EvidenceIndex.SymbolDetails for validation.

Inputs:

  • config: Configuration for the checker (nil uses defaults).

Outputs:

  • *PhantomSymbolChecker: The configured checker.

Thread Safety: Safe for concurrent use.

func (*PhantomSymbolChecker) Check

func (c *PhantomSymbolChecker) Check(ctx context.Context, input *CheckInput) []Violation

Check implements Checker.

Description:

Extracts symbol references from the response and validates they exist
in KnownSymbols or EvidenceIndex.SymbolDetails. Non-existent symbol
references are flagged as ViolationPhantomSymbol.

Inputs:

  • ctx: Context for cancellation.
  • input: The check input containing response and symbol data.

Outputs:

  • []Violation: Any violations found.

Thread Safety: Safe for concurrent use.

func (*PhantomSymbolChecker) Name

func (c *PhantomSymbolChecker) Name() string

Name implements Checker.

type PhantomSymbolCheckerConfig

type PhantomSymbolCheckerConfig struct {
	// Enabled determines if phantom symbol checking is active.
	Enabled bool

	// RequireFileAssociation requires symbol to be associated with a specific file.
	// If true, only validates symbols that have file context.
	// If false, validates against all known symbols globally.
	RequireFileAssociation bool

	// MinSymbolLength filters out short symbols that may be false positives.
	// Symbols shorter than this are ignored.
	MinSymbolLength int

	// MaxSymbolsToCheck limits how many symbol references to check per response.
	// Prevents excessive checking on large responses.
	MaxSymbolsToCheck int

	// IgnoredSymbols contains common type names to ignore (e.g., "Context", "Error").
	// These are too common to validate reliably.
	IgnoredSymbols []string
}

PhantomSymbolCheckerConfig configures the phantom symbol checker.

func DefaultPhantomSymbolCheckerConfig

func DefaultPhantomSymbolCheckerConfig() *PhantomSymbolCheckerConfig

DefaultPhantomSymbolCheckerConfig returns default phantom symbol checker config.

type PostSynthesisConfig

type PostSynthesisConfig struct {
	// Enabled determines if post-synthesis verification runs.
	Enabled bool

	// MaxRetries is the maximum number of synthesis retries before feedback loop.
	MaxRetries int

	// RelevantCheckers specifies which checkers to run post-synthesis.
	// Empty means use default relevant checkers.
	RelevantCheckers []string
}

PostSynthesisConfig configures post-synthesis verification.

func DefaultPostSynthesisConfig

func DefaultPostSynthesisConfig() *PostSynthesisConfig

DefaultPostSynthesisConfig returns sensible defaults for post-synthesis verification.

Outputs:

*PostSynthesisConfig - The default configuration.

type PostSynthesisResult

type PostSynthesisResult struct {
	// Passed is true if verification passed without critical violations.
	Passed bool

	// Violations found during post-synthesis checking.
	Violations []Violation

	// RetryCount is how many retries were attempted.
	RetryCount int

	// StrictnessLevel indicates the strictness at which verification ran.
	StrictnessLevel StrictnessLevel

	// NeedsFeedbackLoop is true if retries exhausted and should explore more.
	NeedsFeedbackLoop bool

	// FeedbackQuestions are questions to explore if feedback loop triggered.
	FeedbackQuestions []string

	// CheckDuration is how long verification took.
	CheckDuration time.Duration

	// CheckersRun is the number of checkers that ran.
	CheckersRun int
}

PostSynthesisResult contains the outcome of post-synthesis verification.

type PostSynthesisVerifier

type PostSynthesisVerifier interface {
	// VerifyPostSynthesis checks a synthesized response for hallucinations.
	//
	// Inputs:
	//   ctx - Context for cancellation.
	//   response - The synthesized response text.
	//   assembledCtx - The context that was given to the LLM.
	//   retryCount - Current retry attempt (0 = first attempt).
	//
	// Outputs:
	//   *PostSynthesisResult - The verification result.
	//   error - Non-nil only if verification itself fails.
	VerifyPostSynthesis(ctx context.Context, response string, assembledCtx *agent.AssembledContext, retryCount int) (*PostSynthesisResult, error)

	// GenerateStricterPrompt creates a prompt with increased strictness.
	//
	// Inputs:
	//   basePrompt - The original synthesis prompt.
	//   violations - The violations from the previous attempt.
	//   level - The strictness level to apply.
	//
	// Outputs:
	//   string - The enhanced prompt with strictness guidance.
	GenerateStricterPrompt(basePrompt string, violations []Violation, level StrictnessLevel) string
}

PostSynthesisVerifier verifies synthesized responses.

This interface allows phases to use post-synthesis verification without depending on the concrete DefaultGrounder type.

Thread Safety: Implementations must be safe for concurrent use.

type QuantitativeChecker

type QuantitativeChecker struct {
	// contains filtered or unexported fields
}

QuantitativeChecker validates quantitative claims in responses.

This checker detects incorrect numeric claims including: - Wrong file counts ("15 test files" when there are 3) - Wrong line counts ("200 lines" when file has 52) - Wrong symbol counts ("10 functions" when there are 5)

Thread Safety: Safe for concurrent use (stateless after construction).

func NewQuantitativeChecker

func NewQuantitativeChecker(config *QuantitativeCheckerConfig) *QuantitativeChecker

NewQuantitativeChecker creates a new quantitative checker.

Inputs:

config - Configuration for the checker (nil uses defaults).

Outputs:

*QuantitativeChecker - The configured checker.

func (*QuantitativeChecker) Check

func (c *QuantitativeChecker) Check(ctx context.Context, input *CheckInput) []Violation

Check implements Checker.

Description:

Extracts quantitative claims from the response and validates them against
the EvidenceIndex. Detects incorrect numeric claims by comparing claimed
values against actual counts from evidence.

Thread Safety: Safe for concurrent use.

func (*QuantitativeChecker) Name

func (c *QuantitativeChecker) Name() string

Name implements Checker.

type QuantitativeCheckerConfig

type QuantitativeCheckerConfig struct {
	// Enabled determines if quantitative hallucination checking is active.
	Enabled bool

	// CheckFileCounts enables checking claims about file counts.
	// E.g., "15 test files", "3 Go files".
	CheckFileCounts bool

	// CheckLineCounts enables checking claims about line counts.
	// E.g., "main.go is 200 lines", "function has 50 lines".
	CheckLineCounts bool

	// CheckSymbolCounts enables checking claims about symbol counts.
	// E.g., "5 functions in package", "3 methods on type".
	CheckSymbolCounts bool

	// ExactTolerance is the tolerance for exact claims (not using "about").
	// Default 0 means exact claims must match exactly.
	// Value of 1 allows off-by-one errors.
	ExactTolerance int

	// ApproximateUnderPct is tolerance for undercounting with approximate claims.
	// "About 200" for 140 actual = 30% under, allowed by default.
	// Default 0.3 (30% under is acceptable).
	ApproximateUnderPct float64

	// ApproximateOverPct is tolerance for overcounting with approximate claims.
	// "About 200" for 230 actual = 15% over, triggers violation by default.
	// Default 0.15 (15% over triggers violation).
	// Asymmetric because overcounting is worse than undercounting.
	ApproximateOverPct float64

	// MaxClaimsToCheck limits how many quantitative claims to check per response.
	// Prevents excessive checking on large responses.
	MaxClaimsToCheck int
}

QuantitativeCheckerConfig configures the quantitative hallucination checker.

func DefaultQuantitativeCheckerConfig

func DefaultQuantitativeCheckerConfig() *QuantitativeCheckerConfig

DefaultQuantitativeCheckerConfig returns default quantitative checker config.

type QuantitativeClaim

type QuantitativeClaim struct {
	// Type is the kind of quantitative claim.
	Type QuantitativeClaimType

	// Number is the claimed numeric value.
	Number int

	// Subject is what's being counted (e.g., "test files", "lines", "functions").
	Subject string

	// Context provides additional context (e.g., file name for line counts).
	Context string

	// IsApproximate indicates if the claim used "about", "around", etc.
	IsApproximate bool

	// Position is the character offset in the response.
	Position int

	// Raw is the original matched text.
	Raw string
}

QuantitativeClaim represents a parsed quantitative claim from response text.

type QuantitativeClaimType

type QuantitativeClaimType int

QuantitativeClaimType categorizes types of quantitative claims.

const (
	// ClaimFileCount is a claim about the number of files.
	ClaimFileCount QuantitativeClaimType = iota

	// ClaimLineCount is a claim about line counts in files.
	ClaimLineCount

	// ClaimSymbolCount is a claim about the number of symbols.
	ClaimSymbolCount
)

func (QuantitativeClaimType) String

func (c QuantitativeClaimType) String() string

String returns the string representation of a QuantitativeClaimType.

type QuestionType

type QuestionType int

QuestionType categorizes the type of question being asked.

const (
	// QuestionUnknown for questions that don't match known patterns.
	QuestionUnknown QuestionType = iota

	// QuestionList for "What X exist?", "List", "Show all" questions.
	QuestionList

	// QuestionHow for "How does", "How is", "How to" questions.
	QuestionHow

	// QuestionWhere for "Where is", "Where are" questions.
	QuestionWhere

	// QuestionWhy for "Why does", "Why is" questions.
	QuestionWhy

	// QuestionWhat for "What is", "What does" questions (definitional).
	QuestionWhat

	// QuestionDescribe for "Describe", "Explain" questions.
	QuestionDescribe
)

func (QuestionType) String

func (qt QuestionType) String() string

String returns the string representation of a QuestionType.

type RelationshipChecker

type RelationshipChecker struct {
	// contains filtered or unexported fields
}

RelationshipChecker validates relationship claims in responses.

This checker detects fabricated relationships including: - Function calls that don't exist ("A calls B" when A doesn't call B) - Imports that don't exist ("X imports Y" when X doesn't import Y)

Thread Safety: Safe for concurrent use (stateless after construction).

func NewRelationshipChecker

func NewRelationshipChecker(config *RelationshipCheckerConfig) *RelationshipChecker

NewRelationshipChecker creates a new relationship checker.

Inputs:

config - Configuration for the checker (nil uses defaults).

Outputs:

*RelationshipChecker - The configured checker.

func (*RelationshipChecker) Check

func (c *RelationshipChecker) Check(ctx context.Context, input *CheckInput) []Violation

Check implements Checker.

Description:

Extracts relationship claims from the response and validates them against
the EvidenceIndex. Detects fabricated relationships including:
- Function calls that don't exist in the call graph
- Imports that don't exist in the import list

Thread Safety: Safe for concurrent use.

func (*RelationshipChecker) Name

func (c *RelationshipChecker) Name() string

Name implements Checker.

type RelationshipCheckerConfig

type RelationshipCheckerConfig struct {
	// Enabled determines if relationship checking is active.
	Enabled bool

	// ValidateImports enables import relationship checking.
	// Validates claims like "X imports Y" against actual imports.
	ValidateImports bool

	// ValidateCalls enables function call relationship checking.
	// Validates claims like "A calls B" against call graph.
	ValidateCalls bool

	// ValidateImplements enables interface implementation checking.
	// Validates claims like "T implements I" against type methods.
	// NOTE: Currently disabled by default - requires type resolution.
	ValidateImplements bool

	// MaxRelationshipsToCheck limits how many relationship claims to check.
	// Prevents excessive checking on responses with many claims.
	MaxRelationshipsToCheck int
}

RelationshipCheckerConfig configures the relationship hallucination checker.

func DefaultRelationshipCheckerConfig

func DefaultRelationshipCheckerConfig() *RelationshipCheckerConfig

DefaultRelationshipCheckerConfig returns default relationship checker config.

type RelationshipClaim

type RelationshipClaim struct {
	// Subject is the entity making the relationship (caller, importer).
	Subject string

	// Object is the entity being related to (callee, imported package).
	Object string

	// Kind is the type of relationship.
	Kind RelationshipKind

	// Position is the character offset in the response.
	Position int

	// Raw is the original matched text.
	Raw string
}

RelationshipClaim represents a parsed relationship claim from response text.

type RelationshipKind

type RelationshipKind int

RelationshipKind categorizes types of relationships.

const (
	// ImportRelation is an import/dependency relationship.
	ImportRelation RelationshipKind = iota

	// CallRelation is a function call relationship.
	CallRelation
)

type Result

type Result struct {
	// Grounded is true if the response is sufficiently grounded.
	Grounded bool `json:"grounded"`

	// Confidence is a score from 0.0 to 1.0 indicating grounding confidence.
	Confidence float64 `json:"confidence"`

	// Violations contains all violations found during checking.
	Violations []Violation `json:"violations,omitempty"`

	// CriticalCount is the number of critical violations.
	CriticalCount int `json:"critical_count"`

	// WarningCount is the number of warnings.
	WarningCount int `json:"warning_count"`

	// ChecksRun is the number of grounding checks that were executed.
	ChecksRun int `json:"checks_run"`

	// CheckDuration is how long the grounding check took.
	CheckDuration time.Duration `json:"check_duration"`

	// CitationsFound is the number of citations found in the response.
	CitationsFound int `json:"citations_found"`

	// CitationsValid is the number of valid citations.
	CitationsValid int `json:"citations_valid"`
}

Result contains the outcome of grounding validation.

func (*Result) AddViolation

func (r *Result) AddViolation(v Violation)

AddViolation adds a violation to the result.

func (*Result) HasCritical

func (r *Result) HasCritical() bool

HasCritical returns true if there are critical violations.

func (*Result) HasWarnings

func (r *Result) HasWarnings() bool

HasWarnings returns true if there are warnings.

type SampleResponse

type SampleResponse struct {
	// Index is the sample number (0-based).
	Index int

	// Content is the response text.
	Content string

	// Claims are the extracted claims from this sample.
	Claims []NormalizedClaim
}

SampleResponse represents a single LLM response sample.

type ScoredEvidence

type ScoredEvidence struct {
	// Index is the original position in the ToolResults slice.
	Index int

	// InvocationID links to the original tool invocation.
	InvocationID string

	// Output is the tool result content.
	Output string

	// RecencyScore ranges from 0.0 (oldest) to 0.5 (most recent).
	RecencyScore float64

	// RelevanceScore ranges from 0.0 (not relevant) to 0.5 (highly relevant).
	RelevanceScore float64

	// TotalScore is RecencyScore + RelevanceScore.
	TotalScore float64

	// EstimatedTokens is the approximate token count for this result.
	EstimatedTokens int
}

ScoredEvidence represents a tool result with computed scores.

type SemanticDriftChecker

type SemanticDriftChecker struct {
	// contains filtered or unexported fields
}

SemanticDriftChecker detects when response doesn't address the original question.

This checker validates that the LLM response is actually answering the question that was asked. It uses keyword overlap, topic coherence, and question type matching to detect when the model has "drifted" to answering a different question.

Thread Safety: Safe for concurrent use (stateless after construction).

func NewSemanticDriftChecker

func NewSemanticDriftChecker(config *SemanticDriftCheckerConfig) *SemanticDriftChecker

NewSemanticDriftChecker creates a new semantic drift checker.

Description:

Creates a checker that detects when responses don't address the question.
Uses keyword overlap, topic coherence, and question type matching.

Inputs:

  • config: Configuration for the checker (nil uses defaults).

Outputs:

  • *SemanticDriftChecker: The configured checker.

Thread Safety: Safe for concurrent use.

func (*SemanticDriftChecker) Check

func (c *SemanticDriftChecker) Check(ctx context.Context, input *CheckInput) []Violation

Check implements Checker.

Description:

Analyzes the response to determine if it addresses the original question.
Uses keyword overlap, topic coherence, and question type matching to
calculate a drift score. High drift scores indicate the response is
off-topic.

Inputs:

  • ctx: Context for cancellation.
  • input: The check input containing response and user question.

Outputs:

  • []Violation: Any violations found.

Thread Safety: Safe for concurrent use.

func (*SemanticDriftChecker) Name

func (c *SemanticDriftChecker) Name() string

Name implements Checker.

type SemanticDriftCheckerConfig

type SemanticDriftCheckerConfig struct {
	// Enabled determines if semantic drift checking is active.
	Enabled bool

	// CriticalThreshold is the drift score above which violations are critical.
	// Default 0.7 - response completely off-topic.
	CriticalThreshold float64

	// HighThreshold is the drift score above which violations are high severity.
	// Default 0.5 - significant drift from the question.
	HighThreshold float64

	// WarningThreshold is the drift score above which warnings are generated.
	// Default 0.3 - partial drift detected.
	WarningThreshold float64

	// KeywordWeight is the weight for keyword overlap scoring.
	// Default 0.4 - how much keywords from question appear in response.
	KeywordWeight float64

	// TopicWeight is the weight for topic coherence scoring.
	// Default 0.4 - whether response discusses the right topic.
	TopicWeight float64

	// TypeWeight is the weight for question type matching.
	// Default 0.2 - whether response format matches question type.
	TypeWeight float64

	// MinKeywords is the minimum keywords needed to perform drift check.
	// Questions with fewer keywords are skipped (too ambiguous).
	MinKeywords int

	// MinResponseLength is the minimum response length to check.
	// Very short responses are skipped (can't meaningfully assess).
	MinResponseLength int

	// ListTypeMismatchPenalty is the penalty for non-list response to LIST question.
	// Default 0.7 - significant mismatch as list questions expect enumeration.
	ListTypeMismatchPenalty float64

	// WhereTypeMismatchPenalty is the penalty for non-location response to WHERE question.
	// Default 0.5 - moderate mismatch as WHERE questions expect file/path references.
	WhereTypeMismatchPenalty float64

	// HowTypeMismatchPenalty is the penalty for non-process response to HOW question.
	// Default 0.3 - slight mismatch as HOW questions expect process descriptions.
	HowTypeMismatchPenalty float64
}

SemanticDriftCheckerConfig configures the semantic drift checker.

func DefaultSemanticDriftCheckerConfig

func DefaultSemanticDriftCheckerConfig() *SemanticDriftCheckerConfig

DefaultSemanticDriftCheckerConfig returns default semantic drift checker config.

type Severity

type Severity string

Severity indicates the severity of a grounding violation.

const (
	// SeverityInfo is for informational messages.
	SeverityInfo Severity = "info"

	// SeverityWarning is for warnings that should be reviewed.
	SeverityWarning Severity = "warning"

	// SeverityHigh is for high-severity issues requiring attention.
	SeverityHigh Severity = "high"

	// SeverityCritical is for critical issues that indicate hallucination.
	SeverityCritical Severity = "critical"
)

type StrictnessLevel

type StrictnessLevel int

StrictnessLevel indicates the level of verification strictness.

const (
	// StrictnessNormal is the default strictness level.
	StrictnessNormal StrictnessLevel = iota

	// StrictnessElevated adds violation feedback to the prompt.
	StrictnessElevated

	// StrictnessHigh requires explicit grounding and adds warnings.
	StrictnessHigh

	// StrictnessFeedback indicates retries exhausted, need exploration.
	StrictnessFeedback
)

func (StrictnessLevel) String

func (s StrictnessLevel) String() string

String returns the string representation of StrictnessLevel.

type StructuralClaimChecker

type StructuralClaimChecker struct {
	// contains filtered or unexported fields
}

StructuralClaimChecker detects structural claims without supporting evidence.

This checker identifies when the LLM describes directory structures or file lists without having tool evidence (ls, find, tree output) to support those claims. Unsupported structural claims often indicate the model is fabricating a "generic project" structure rather than describing the actual codebase.

Thread Safety: Safe for concurrent use (stateless after construction).

func NewStructuralClaimChecker

func NewStructuralClaimChecker(config *StructuralClaimCheckerConfig) *StructuralClaimChecker

NewStructuralClaimChecker creates a new structural claim checker.

Description:

Creates a checker that detects structural claims (directory trees, file lists)
and validates they are backed by tool evidence.

Inputs:

  • config: Configuration for the checker (nil uses defaults).

Outputs:

  • *StructuralClaimChecker: The configured checker.

Thread Safety: Safe for concurrent use.

func (*StructuralClaimChecker) Check

func (c *StructuralClaimChecker) Check(ctx context.Context, input *CheckInput) []Violation

Check implements Checker.

Description:

Scans the response for structural claims (directory trees, file lists)
and verifies they are backed by tool evidence. Structural claims without
supporting evidence are flagged as ViolationStructuralClaim.

Inputs:

  • ctx: Context for cancellation.
  • input: The check input containing response and evidence.

Outputs:

  • []Violation: Any violations found.

Thread Safety: Safe for concurrent use.

func (*StructuralClaimChecker) Name

func (c *StructuralClaimChecker) Name() string

Name implements Checker.

type StructuralClaimCheckerConfig

type StructuralClaimCheckerConfig struct {
	// Enabled determines if structural claim checking is active.
	Enabled bool

	// RequireToolEvidence requires tool output (ls/find/tree) for structural claims.
	RequireToolEvidence bool

	// MaxPathsToExtract limits the number of paths extracted from tree structures.
	MaxPathsToExtract int
}

StructuralClaimCheckerConfig configures the structural claim checker.

func DefaultStructuralClaimCheckerConfig

func DefaultStructuralClaimCheckerConfig() *StructuralClaimCheckerConfig

DefaultStructuralClaimCheckerConfig returns default structural claim checker config.

type StructuredClaim

type StructuredClaim struct {
	// Statement is the factual claim being made.
	Statement string `json:"statement"`

	// File is the file that supports this claim.
	File string `json:"file"`

	// LineStart is the starting line number.
	LineStart int `json:"line_start"`

	// LineEnd is the ending line number.
	LineEnd int `json:"line_end"`

	// EvidenceQuote is the exact text from the file.
	EvidenceQuote string `json:"evidence_quote"`

	// Confidence is how certain the LLM is (0.0-1.0).
	Confidence float64 `json:"confidence"`
}

StructuredClaim represents a single factual claim with evidence.

type StructuredOutputChecker

type StructuredOutputChecker struct {
	// contains filtered or unexported fields
}

StructuredOutputChecker validates structured JSON responses.

This is Layer 2 of the anti-hallucination defense system. It parses JSON responses and validates that claims have proper evidence.

Thread Safety: Safe for concurrent use after construction.

func NewStructuredOutputChecker

func NewStructuredOutputChecker(config *StructuredOutputConfig) *StructuredOutputChecker

NewStructuredOutputChecker creates a new structured output checker.

Inputs:

config - Configuration for the checker. If nil, defaults are used.

Outputs:

*StructuredOutputChecker - The configured checker.

func (*StructuredOutputChecker) Check

Check validates a structured response against evidence.

Description:

Attempts to parse the response as JSON. If successful, validates
each claim's file reference and evidence quote. If parsing fails,
handles gracefully based on configuration.

Thread Safety: Safe for concurrent use.

func (*StructuredOutputChecker) Name

func (c *StructuredOutputChecker) Name() string

Name returns the checker name for logging and metrics.

type StructuredOutputConfig

type StructuredOutputConfig struct {
	// Enabled determines if structured output validation is active.
	Enabled bool

	// RequireJSON requires the response to be valid JSON.
	RequireJSON bool

	// RequireEvidenceQuotes requires claims to have evidence quotes.
	RequireEvidenceQuotes bool

	// MinConfidence is the minimum confidence to accept a claim.
	MinConfidence float64

	// ValidateEvidenceExists checks that evidence quotes exist in file content.
	ValidateEvidenceExists bool
}

StructuredOutputConfig configures the structured output validator.

func DefaultStructuredOutputConfig

func DefaultStructuredOutputConfig() *StructuredOutputConfig

DefaultStructuredOutputConfig returns sensible defaults.

type StructuredResponse

type StructuredResponse struct {
	// Summary is a brief answer to the user's question.
	Summary string `json:"summary"`

	// Claims are the factual claims made in the response.
	Claims []StructuredClaim `json:"claims"`

	// FilesExamined lists the files the LLM saw in context.
	FilesExamined []string `json:"files_examined"`

	// ToolsUsed lists the tools the LLM used.
	ToolsUsed []string `json:"tools_used"`

	// Uncertainty describes what the LLM is not sure about.
	Uncertainty string `json:"uncertainty,omitempty"`
}

StructuredResponse is the required JSON output format for grounded responses.

When structured output is enabled, the LLM is instructed to respond in this JSON format, making claims machine-parseable and easier to validate.

func ParseStructuredResponse

func ParseStructuredResponse(response string) (*StructuredResponse, error)

ParseStructuredResponse attempts to parse a response as StructuredResponse.

Inputs:

response - The response text to parse.

Outputs:

*StructuredResponse - The parsed response, or nil if parsing fails.
error - Non-nil if parsing fails.

type SymbolInfo

type SymbolInfo struct {
	// Name is the symbol name (function, type, variable, constant name).
	Name string

	// Kind is the symbol kind: "function", "type", "variable", "constant", "method".
	Kind string

	// File is the file path where the symbol is defined.
	File string

	// Line is the line number where the symbol is defined.
	Line int

	// ReturnTypes lists the return types for functions/methods (in order).
	// For "func Parse() (*Result, error)", this is ["*Result", "error"].
	// Empty for non-functions.
	ReturnTypes []string

	// Parameters lists the parameter types for functions/methods (in order).
	// For "func Parse(ctx context.Context, data []byte)", this is ["context.Context", "[]byte"].
	// Empty for non-functions.
	Parameters []string

	// Fields lists the field names for structs.
	// For "type Config struct { Name string; Value int }", this is ["Name", "Value"].
	// Empty for non-structs.
	Fields []string

	// Methods lists the method names for interfaces.
	// For "type Reader interface { Read() }", this is ["Read"].
	// Empty for non-interfaces.
	Methods []string

	// Receiver is the receiver type for methods (e.g., "*Config" for pointer receiver).
	// Empty for functions and non-method types.
	Receiver string
}

SymbolInfo contains detailed information about a symbol.

This tracks where symbols are defined in the codebase, enabling validation that referenced symbols actually exist and have correct attributes.

type TMSVerificationResult

type TMSVerificationResult struct {
	// SupportedClaims are claims with evidence support (status IN).
	SupportedClaims []VerifiedClaim

	// UnsupportedClaims are claims without evidence support (status OUT).
	UnsupportedClaims []VerifiedClaim

	// Contradictions are logical contradictions detected by TMS.
	Contradictions []string

	// PropagationIterations is how many TMS iterations were run.
	PropagationIterations int

	// TotalClaims is the total number of claims verified.
	TotalClaims int
}

TMSVerificationResult contains the outcome of TMS-based verification.

type TMSVerifier

type TMSVerifier struct {
	// contains filtered or unexported fields
}

TMSVerifier uses TMS to verify claims against evidence.

This is Layer 6 of the anti-hallucination defense system. It models claims and evidence as TMS beliefs, then uses propagation to determine which claims are supported by evidence.

Key concepts:

  • Evidence (files, symbols) are registered as base beliefs (always IN)
  • Claims are registered as beliefs that depend on evidence
  • TMS propagation determines if claims have supporting evidence
  • Unsupported claims (status OUT) indicate potential hallucination

Thread Safety: Safe for concurrent use after construction.

func NewTMSVerifier

func NewTMSVerifier(config *TMSVerifierConfig) *TMSVerifier

NewTMSVerifier creates a new TMS-based claim verifier.

Inputs:

config - Configuration for the verifier. If nil, defaults are used.

Outputs:

*TMSVerifier - The configured verifier.

func (*TMSVerifier) Check

func (v *TMSVerifier) Check(ctx context.Context, input *CheckInput) []Violation

Check implements Checker interface for integration with grounding pipeline.

Description:

Extracts claims from the response (using GroundingChecker's patterns),
verifies them using TMS, and returns violations for unsupported claims.

Thread Safety: Safe for concurrent use.

func (*TMSVerifier) Name

func (v *TMSVerifier) Name() string

Name returns the verifier name for logging and metrics.

func (*TMSVerifier) VerifyClaims

func (v *TMSVerifier) VerifyClaims(ctx context.Context, claims []Claim, evidence *EvidenceIndex) (*TMSVerificationResult, error)

VerifyClaims uses TMS to verify which claims are supported by evidence.

Description:

Registers evidence as base beliefs (always IN), registers claims as
dependent beliefs, runs TMS propagation, and collects results.

Inputs:

ctx - Context for cancellation.
claims - The claims to verify.
evidence - The evidence index from context.

Outputs:

*TMSVerificationResult - The verification result.
error - Non-nil if verification fails.

Thread Safety: Safe for concurrent use.

type TMSVerifierConfig

type TMSVerifierConfig struct {
	// Enabled determines if TMS verification is enabled.
	Enabled bool

	// MaxIterations limits TMS propagation iterations.
	MaxIterations int
}

TMSVerifierConfig configures the TMS verifier.

func DefaultTMSVerifierConfig

func DefaultTMSVerifierConfig() *TMSVerifierConfig

DefaultTMSVerifierConfig returns sensible defaults.

type TemporalChecker

type TemporalChecker struct {
	// contains filtered or unexported fields
}

TemporalChecker validates temporal claims in responses.

This checker detects: - Recency claims ("recently added", "just updated") - Historical claims ("was originally", "used to be") - Version claims ("added in v1.0", "since version 2") - Reason claims ("changed because", "refactored to improve")

Without git evidence, these claims are flagged as unverifiable.

Thread Safety: Safe for concurrent use (stateless after construction).

func NewTemporalChecker

func NewTemporalChecker(config *TemporalCheckerConfig) *TemporalChecker

NewTemporalChecker creates a new temporal hallucination checker.

Inputs:

config - Configuration for the checker (nil uses defaults).

Outputs:

*TemporalChecker - The configured checker.

func (*TemporalChecker) Check

func (c *TemporalChecker) Check(ctx context.Context, input *CheckInput) []Violation

Check implements Checker.

Description:

Extracts temporal claims from the response and flags them as unverifiable
when no git evidence is available. This is not about proving claims wrong,
but about flagging confident assertions about code history that cannot be
verified from the available evidence.

Thread Safety: Safe for concurrent use.

func (*TemporalChecker) Name

func (c *TemporalChecker) Name() string

Name implements Checker.

type TemporalCheckerConfig

type TemporalCheckerConfig struct {
	// Enabled determines if temporal hallucination checking is active.
	Enabled bool

	// StrictMode flags all temporal claims as warnings instead of info.
	// When false (default), temporal claims use SeverityInfo.
	// When true, temporal claims use SeverityWarning.
	StrictMode bool

	// CheckRecencyClaims enables checking "recently", "just", "new" claims.
	CheckRecencyClaims bool

	// CheckHistoricalClaims enables checking "was", "used to", "originally" claims.
	CheckHistoricalClaims bool

	// CheckVersionClaims enables checking "v1.0", "version 2", "since" claims.
	CheckVersionClaims bool

	// CheckReasonClaims enables checking "because", "due to", "in order to" claims.
	// These claims about WHY code was changed are always unverifiable.
	CheckReasonClaims bool

	// MaxClaimsToCheck limits how many temporal claims to check per response.
	// Prevents excessive checking on responses with many temporal references.
	MaxClaimsToCheck int

	// SkipCodeBlocks if true, ignores temporal words inside code blocks.
	// Default true - avoids flagging "new" in "new(Something)" etc.
	SkipCodeBlocks bool
}

TemporalCheckerConfig configures the temporal hallucination checker.

func DefaultTemporalCheckerConfig

func DefaultTemporalCheckerConfig() *TemporalCheckerConfig

DefaultTemporalCheckerConfig returns default temporal checker config.

type TemporalClaim

type TemporalClaim struct {
	// Category is the type of temporal claim.
	Category TemporalClaimCategory

	// Position is the character offset in the response.
	Position int

	// Raw is the matched text.
	Raw string
}

TemporalClaim represents a detected temporal claim in the response.

type TemporalClaimCategory

type TemporalClaimCategory int

TemporalClaimCategory categorizes types of temporal claims.

const (
	// ClaimCategoryRecency is for "recently", "just", "new", "latest" claims.
	ClaimCategoryRecency TemporalClaimCategory = iota

	// ClaimCategoryHistorical is for "was", "used to", "originally" claims.
	ClaimCategoryHistorical

	// ClaimCategoryVersion is for "v1.0", "version 2", "since" claims.
	ClaimCategoryVersion

	// ClaimCategoryReason is for "because", "due to", "in order to" claims.
	ClaimCategoryReason
)

func (TemporalClaimCategory) String

func (c TemporalClaimCategory) String() string

String returns the string representation of TemporalClaimCategory.

type ToolResult

type ToolResult struct {
	// InvocationID links back to the invocation.
	InvocationID string

	// Output is the tool's output text.
	Output string
}

ToolResult is a simplified tool result for grounding checks.

type ValidationStats

type ValidationStats struct {
	ChecksRun       int
	ViolationsFound int
	CriticalCount   int
	WarningCount    int
	Grounded        bool
	Confidence      float64
	Duration        time.Duration
}

ValidationStats contains statistics for a complete validation run.

type VerificationRequest

type VerificationRequest struct {
	// Claims are the claims extracted from the response.
	Claims []ExtractedClaim

	// OriginalResponse is the LLM response being verified.
	OriginalResponse string

	// Evidence is the evidence index from context.
	Evidence *EvidenceIndex
}

VerificationRequest contains the data needed for verification.

type VerificationResponse

type VerificationResponse struct {
	// Verifications contains the result for each claim.
	Verifications []ClaimVerification `json:"verifications"`
}

VerificationResponse is the expected response from the verification LLM call.

type VerificationResult

type VerificationResult struct {
	// AllVerified is true if all claims were verified.
	AllVerified bool

	// VerifiedClaims are claims that passed verification.
	VerifiedClaims []VerifiedClaimResult

	// UnverifiedClaims are claims that failed verification.
	UnverifiedClaims []VerifiedClaimResult

	// RawResponse is the raw verification response for debugging.
	RawResponse string
}

VerificationResult contains the outcome of chain-of-verification.

type VerifiedClaim

type VerifiedClaim struct {
	// Index is the claim's position in the original list.
	Index int

	// Claim is the original claim.
	Claim Claim

	// Supported indicates if the claim is supported by evidence.
	Supported bool

	// Reason explains why the claim is or isn't supported.
	Reason string

	// MissingEvidence lists evidence that would be needed to support this claim.
	MissingEvidence []string
}

VerifiedClaim contains the verification result for a single claim.

type VerifiedClaimResult

type VerifiedClaimResult struct {
	// ClaimID is the ID of the claim.
	ClaimID int

	// Statement is the claim text.
	Statement string

	// Verified is true if verified.
	Verified bool

	// Confidence is the confidence level.
	Confidence string

	// Citation is the supporting citation.
	Citation string

	// Reason explains lack of verification.
	Reason string
}

VerifiedClaimResult contains verification details for a claim.

type Violation

type Violation struct {
	// Type is the kind of violation.
	Type ViolationType `json:"type"`

	// Severity indicates how serious the violation is.
	Severity Severity `json:"severity"`

	// Code is a machine-readable issue code.
	Code string `json:"code"`

	// Message is a human-readable description.
	Message string `json:"message"`

	// Evidence is what triggered this violation.
	Evidence string `json:"evidence,omitempty"`

	// Expected is what should exist instead.
	Expected string `json:"expected,omitempty"`

	// Location is where in the response this occurred.
	Location string `json:"location,omitempty"`

	// Suggestion provides guidance on how to fix the violation.
	Suggestion string `json:"suggestion,omitempty"`

	// Phase indicates when this violation was detected ("pre_synthesis" or "post_synthesis").
	Phase string `json:"phase,omitempty"`

	// RetryCount indicates which retry attempt detected this violation.
	RetryCount int `json:"retry_count,omitempty"`

	// LocationOffset is the character position in the response (for sorting).
	LocationOffset int `json:"location_offset,omitempty"`
}

Violation represents a single grounding failure.

func DeduplicateCascadeViolations

func DeduplicateCascadeViolations(violations []Violation) []Violation

DeduplicateCascadeViolations removes lower-priority violations subsumed by higher ones.

Description:

When a higher-priority violation covers the same evidence as a lower-priority
one, the lower-priority violation is often a symptom of the higher one.
For example:
  - PhantomFile("config/app.py") + LanguageConfusion("Flask") →
    Keep only PhantomFile (language confusion is a consequence)

This function removes such redundant violations while preserving violations
with different evidence.

Inputs:

  • violations: Slice of violations (may be unsorted).

Outputs:

  • []Violation: Deduplicated violations, sorted by priority.

Thread Safety: Safe for concurrent use (creates new slice).

func FilterViolationsByPriority

func FilterViolationsByPriority(violations []Violation, threshold ViolationPriority) []Violation

FilterViolationsByPriority returns violations at or above a priority threshold.

Description:

Filters to include only violations with priority <= threshold.
Lower priority numbers are higher priority, so threshold=2 includes
P1 (PhantomFile) and P2 (StructuralClaim).

Inputs:

  • violations: Slice of violations to filter.
  • threshold: Maximum priority value to include (lower = higher priority).

Outputs:

  • []Violation: Filtered violations.

Thread Safety: Safe for concurrent use.

func SortViolationsByPriority

func SortViolationsByPriority(violations []Violation) []Violation

SortViolationsByPriority returns violations sorted by priority.

Description:

Sorts violations so that higher-priority violations (lower numeric value)
come first. Uses stable sort to preserve order within the same priority.
This ensures deterministic ordering for consistent behavior.

Inputs:

  • violations: Slice of violations to sort.

Outputs:

  • []Violation: New slice with violations sorted by priority.

Thread Safety: Safe for concurrent use (creates new slice).

func ValidateStructuredResponse

func ValidateStructuredResponse(resp *StructuredResponse, evidence *EvidenceIndex) []Violation

ValidateStructuredResponse validates a StructuredResponse against evidence.

This is a standalone function for use outside the checker pipeline.

Inputs:

resp - The structured response to validate.
evidence - The evidence index from context.

Outputs:

[]Violation - Any violations found.

func (*Violation) Priority

func (v *Violation) Priority() ViolationPriority

Priority returns the processing priority for this violation.

Description:

Convenience method that calls ViolationTypeToPriority with
the violation's type.

Outputs:

  • ViolationPriority: The priority level for ordering.

Thread Safety: Safe for concurrent use.

type ViolationPriority

type ViolationPriority int

ViolationPriority defines processing order for violation types. Lower values = higher priority (processed first). This is used to sort violations and deduplicate cascade violations.

const (
	// PrioritySemanticDrift is highest priority - response doesn't address the question.
	// Process first because if the entire response is off-topic, other violations are moot.
	PrioritySemanticDrift ViolationPriority = 0

	// PriorityPhantomFile is high priority - file doesn't exist at all.
	// Always process early because it invalidates any other claims about that file.
	PriorityPhantomFile ViolationPriority = 1

	// PriorityStructuralClaim is high priority - fabricated directory structure.
	// Process after phantom because a phantom file in a structure claim should
	// be reported as PhantomFile, not StructuralClaim.
	PriorityStructuralClaim ViolationPriority = 2

	// PriorityLanguageConfusion is medium priority - wrong language patterns.
	// Lower priority because language confusion is often a symptom of phantom
	// file references (referencing imaginary Python files in a Go project).
	PriorityLanguageConfusion ViolationPriority = 3

	// PriorityGenericPattern is low priority - generic descriptions.
	// Lowest priority as it's the least severe form of hallucination.
	PriorityGenericPattern ViolationPriority = 4

	// PriorityOther is for existing violation types not in the hierarchy.
	PriorityOther ViolationPriority = 5

	// PriorityPhantomSymbol is high priority - symbol doesn't exist in file.
	// Process after phantom file (P1) but at same level as structural claim (P2).
	PriorityPhantomSymbol ViolationPriority = 2

	// PriorityAttributeHallucination is high priority - wrong attributes on real symbols.
	// Same level as phantom symbol (P2) as it's a factual error about existing code.
	PriorityAttributeHallucination ViolationPriority = 2

	// PriorityLineNumberFabrication is medium priority - incorrect line citations.
	// Less severe than phantom symbol since the file/symbol may exist, just wrong location.
	PriorityLineNumberFabrication ViolationPriority = 3

	// PriorityRelationshipHallucination is high priority - fabricated code relationships.
	// Wrong understanding of system architecture (calls, imports, dependencies).
	PriorityRelationshipHallucination ViolationPriority = 2

	// PriorityBehavioralHallucination is high priority - fabricated behavior claims.
	// Wrong claims about what code does (validation, logging, security).
	// Same level as relationship hallucination (P2) due to security implications.
	PriorityBehavioralHallucination ViolationPriority = 2

	// PriorityQuantitativeHallucination is medium priority - incorrect numeric claims.
	// Wrong counts are factual errors but less severe than structural hallucinations.
	// Same level as language confusion (P3).
	PriorityQuantitativeHallucination ViolationPriority = 3

	// PriorityFabricatedCode is critical priority - invented code shown as real.
	// Same level as PhantomFile (P1) - both are actively misleading.
	// Fabricated code is as dangerous as claiming a file exists when it doesn't.
	PriorityFabricatedCode ViolationPriority = 1

	// PriorityAPIHallucination is high priority - incorrect library/API claims.
	// Same level as structural claim (P2) - factual error about project dependencies.
	PriorityAPIHallucination ViolationPriority = 2

	// PriorityTemporalHallucination is low priority - unverifiable history claims.
	// Same level as generic pattern (P4) - often harmless filler.
	// Temporal claims are unverifiable without git access, not provably wrong.
	PriorityTemporalHallucination ViolationPriority = 4

	// PriorityCrossContextConfusion is high priority - mixing up different code locations.
	// Same level as structural claim (P2) - factual error about code structure.
	// Conflating information from different symbols is a serious hallucination.
	PriorityCrossContextConfusion ViolationPriority = 2

	// PriorityConfidenceFabrication is medium priority - overconfident claims.
	// Same level as line number fabrication (P3) - epistemically problematic but
	// not necessarily factually wrong. The claim might be true, just not supported.
	PriorityConfidenceFabrication ViolationPriority = 3

	// PriorityPhantomPackage is high priority - package path doesn't exist.
	// Same level as structural claim (P2) - factual error about project structure.
	// Model claims pkg/config exists when it doesn't - this is conformity hallucination.
	PriorityPhantomPackage ViolationPriority = 2
)

func ViolationTypeToPriority

func ViolationTypeToPriority(vt ViolationType) ViolationPriority

ViolationTypeToPriority maps a violation type to its processing priority.

Description:

Returns the priority for a given violation type. Lower values indicate
higher priority (processed first). New regression-related violations
have explicit priorities; existing types default to PriorityOther.

Inputs:

  • vt: The violation type to get priority for.

Outputs:

  • ViolationPriority: The priority level for ordering.

Thread Safety: Safe for concurrent use (pure function).

type ViolationType

type ViolationType string

ViolationType categorizes the kind of grounding failure.

const (
	// ViolationWrongLanguage indicates response contains wrong language patterns.
	ViolationWrongLanguage ViolationType = "wrong_language"

	// ViolationFileNotFound indicates referenced file doesn't exist.
	ViolationFileNotFound ViolationType = "file_not_found"

	// ViolationSymbolNotFound indicates referenced symbol doesn't exist.
	ViolationSymbolNotFound ViolationType = "symbol_not_found"

	// ViolationCitationInvalid indicates a [file:line] citation is invalid.
	ViolationCitationInvalid ViolationType = "citation_invalid"

	// ViolationNoCitations indicates response makes claims without citations.
	ViolationNoCitations ViolationType = "no_citations"

	// ViolationUngrounded indicates a claim is not grounded in evidence.
	ViolationUngrounded ViolationType = "ungrounded"

	// ViolationContradiction indicates response contradicts its context.
	ViolationContradiction ViolationType = "contradiction"

	// ViolationEvidenceMismatch indicates quoted evidence doesn't match file.
	ViolationEvidenceMismatch ViolationType = "evidence_mismatch"

	// ViolationPhantomFile indicates reference to a file that doesn't exist.
	// This is the highest priority violation as it invalidates other claims.
	ViolationPhantomFile ViolationType = "phantom_file"

	// ViolationStructuralClaim indicates fabricated directory/file structure.
	// Structural claims about project layout without supporting evidence.
	ViolationStructuralClaim ViolationType = "structural_claim"

	// ViolationLanguageConfusion indicates wrong-language patterns in response.
	// E.g., describing Flask patterns in a Go codebase.
	ViolationLanguageConfusion ViolationType = "language_confusion"

	// ViolationGenericPattern indicates generic descriptions without grounding.
	// Response describes common patterns instead of project-specific findings.
	ViolationGenericPattern ViolationType = "generic_pattern"

	// ViolationPhantomSymbol indicates reference to a symbol that doesn't exist.
	// The file exists but the referenced function/type/variable does not.
	// This is distinct from ViolationSymbolNotFound which is for cited symbols
	// that should exist based on citation format but don't.
	ViolationPhantomSymbol ViolationType = "phantom_symbol"

	// ViolationSemanticDrift indicates response doesn't address the original question.
	// The response may be internally consistent but completely irrelevant to what was asked.
	// This is the highest priority violation as the entire response is invalid.
	ViolationSemanticDrift ViolationType = "semantic_drift"

	// ViolationAttributeHallucination indicates wrong attributes about real code elements.
	// The referenced element exists but the properties described are fabricated
	// (wrong return types, wrong parameter counts, wrong field names).
	ViolationAttributeHallucination ViolationType = "attribute_hallucination"

	// ViolationLineNumberFabrication indicates fabricated or incorrect line numbers.
	// The file exists and may be in context, but the cited line number is wrong.
	// This includes lines beyond file length and symbol location mismatches.
	ViolationLineNumberFabrication ViolationType = "line_number_fabrication"

	// ViolationRelationshipHallucination indicates fabricated relationships between code elements.
	// Claims about function calls, imports, or interface implementations that don't exist.
	// E.g., "A calls B" when A doesn't call B, or "X imports Y" when X doesn't import Y.
	ViolationRelationshipHallucination ViolationType = "relationship_hallucination"

	// ViolationBehavioralHallucination indicates fabricated claims about code behavior.
	// Claims about what code does (validates, logs, encrypts) that are contradicted by the code.
	// E.g., "validates input" when there's no validation, "logs errors" when they're swallowed.
	ViolationBehavioralHallucination ViolationType = "behavioral_hallucination"

	// ViolationQuantitativeHallucination indicates incorrect numeric claims.
	// Wrong file counts, line counts, function counts, etc.
	// E.g., "15 test files" when there are 3, "200 lines" when file has 52.
	ViolationQuantitativeHallucination ViolationType = "quantitative_hallucination"

	// ViolationFabricatedCode indicates code snippet not found in evidence.
	// Model shows code blocks that don't exist in the codebase,
	// inventing implementations or showing "improved" code as original.
	// This is a critical violation - actively misleading users.
	ViolationFabricatedCode ViolationType = "fabricated_code"

	// ViolationAPIHallucination indicates claims about libraries/APIs not in evidence.
	// Model claims usage of libraries not imported, or confuses similar libraries
	// (e.g., claims "uses gorm" when project actually uses sqlx).
	ViolationAPIHallucination ViolationType = "api_hallucination"

	// ViolationTemporalHallucination indicates unverifiable claims about code history.
	// Model makes claims about when code was added, changed, refactored, or versioned
	// without access to git history. Examples: "recently refactored", "added in v2.0",
	// "originally implemented for X", "was changed because Y".
	ViolationTemporalHallucination ViolationType = "temporal_hallucination"

	// ViolationCrossContextConfusion indicates mixing information from different code locations.
	// Model conflates attributes, fields, or behaviors from different instances of
	// similarly-named symbols. E.g., describing Config in pkg/server with fields
	// that actually belong to Config in pkg/client.
	ViolationCrossContextConfusion ViolationType = "cross_context_confusion"

	// ViolationConfidenceFabrication indicates overconfident claims without supporting evidence.
	// Model asserts certainty ("always", "never", "all", "none") when evidence is weak
	// or absent. E.g., "all inputs are validated" when only one validation was seen,
	// or "there is no error logging" after searching only 3 files.
	ViolationConfidenceFabrication ViolationType = "confidence_fabrication"

	// ViolationPhantomPackage indicates reference to a package path that doesn't exist.
	// The model mentions pkg/config, cmd/database, etc. that are not in the codebase.
	// This is conformity hallucination - assuming standard patterns exist.
	// E.g., claiming "pkg/config" handles configuration when no such package exists.
	ViolationPhantomPackage ViolationType = "phantom_package"
)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL