Help us shape the future of Go! Take the 2026 Go Developer Survey

reflection

package
v0.5.0 Latest Latest
Warning

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

Go to latest
Published: Jun 19, 2026 License: GPL-3.0 Imports: 7 Imported by: 0

Documentation

Overview

Package reflection provides self-reflection capabilities for analyzing memory patterns and generating insights from past sessions.

The package supports:

  • Pattern analysis across memories (success, failure, recurring, improving, declining)
  • Correlation detection between tags and outcomes
  • Insight generation based on patterns and correlations
  • Report generation in multiple formats (JSON, text, markdown)

Architecture

The main components are:

  • Analyzer: Identifies patterns in memories by category and frequency
  • Reporter: Generates comprehensive reflection reports
  • Pattern: Represents a detected behavioral pattern
  • Insight: Actionable recommendation derived from patterns

Usage

Create an analyzer with a ReasoningBank service:

analyzer := reflection.NewAnalyzer(reasoningBankSvc)
patterns, err := analyzer.Analyze(ctx, reflection.AnalyzeOptions{
    ProjectID:     "my-project",
    MinConfidence: 0.3,
    MinFrequency:  2,
    MaxPatterns:   20,
})

Generate a reflection report:

reporter := reflection.NewReporter(reasoningBankSvc)
report, err := reporter.Generate(ctx, reflection.ReportOptions{
    ProjectID:           "my-project",
    Period:              period,
    IncludePatterns:     true,
    IncludeCorrelations: true,
    IncludeInsights:     true,
    MaxInsights:         10,
    Format:              "markdown",
})

Pattern Categories

Patterns are grouped into categories:

  • Success: Strategies that consistently work well
  • Failure: Approaches that frequently fail
  • Recurring: Patterns that appear regularly
  • Improving: Patterns with increasing confidence over time
  • Declining: Patterns with decreasing effectiveness

Report Persistence

Reports can be persisted to disk using StoreReflectionReport(), which saves to .claude/reflections/ in the project directory with path traversal protection and restrictive file permissions.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func FormatReport

func FormatReport(report *ReflectionReport, format string) string

FormatReport formats a report as text, markdown, or JSON.

Types

type AnalyzeOptions

type AnalyzeOptions struct {
	// ProjectID to analyze.
	ProjectID string
	// MinConfidence threshold for patterns.
	MinConfidence float64
	// MinFrequency threshold for patterns.
	MinFrequency int
	// IncludeTags to filter by.
	IncludeTags []string
	// ExcludeTags to filter out.
	ExcludeTags []string
	// Period to analyze.
	Period *ReportPeriod
	// MaxPatterns to return.
	MaxPatterns int
}

AnalyzeOptions configures pattern analysis.

type Analyzer

type Analyzer interface {
	// Analyze finds patterns in memories for a project.
	Analyze(ctx context.Context, opts AnalyzeOptions) ([]Pattern, error)
}

Analyzer identifies patterns in memories.

type CorrelateOptions

type CorrelateOptions struct {
	// PatternIDs to correlate (empty = all).
	PatternIDs []string
	// MinStrength threshold for correlations.
	MinStrength float64
	// Types of correlations to find.
	Types []CorrelationType
	// MaxCorrelations to return.
	MaxCorrelations int
}

CorrelateOptions configures correlation analysis.

type Correlation

type Correlation struct {
	// ID is the unique identifier for this correlation.
	ID string `json:"id"`
	// SourceID is the ID of the source pattern/memory.
	SourceID string `json:"source_id"`
	// TargetID is the ID of the target pattern/memory.
	TargetID string `json:"target_id"`
	// Type describes the type of correlation.
	Type CorrelationType `json:"type"`
	// Strength indicates correlation strength (0-1).
	Strength float64 `json:"strength"`
	// Description explains the correlation.
	Description string `json:"description"`
}

Correlation represents a relationship between patterns or memories.

type CorrelationType

type CorrelationType string

CorrelationType represents the type of correlation between items.

const (
	// CorrelationCausal indicates a causal relationship.
	CorrelationCausal CorrelationType = "causal"
	// CorrelationSimilar indicates similar patterns.
	CorrelationSimilar CorrelationType = "similar"
	// CorrelationOpposite indicates contrasting patterns.
	CorrelationOpposite CorrelationType = "opposite"
	// CorrelationSequential indicates sequential occurrence.
	CorrelationSequential CorrelationType = "sequential"
	// CorrelationCoOccurs indicates patterns that occur together.
	CorrelationCoOccurs CorrelationType = "co_occurs"
)

type Correlator

type Correlator interface {
	// Correlate finds relationships between patterns.
	Correlate(patterns []Pattern, opts CorrelateOptions) ([]Correlation, error)
}

Correlator finds relationships between patterns and memories.

type DefaultAnalyzer

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

DefaultAnalyzer implements pattern analysis using the ReasoningBank.

func NewAnalyzer

func NewAnalyzer(memorySvc *reasoningbank.Service) *DefaultAnalyzer

NewAnalyzer creates a new pattern analyzer.

func (*DefaultAnalyzer) Analyze

func (a *DefaultAnalyzer) Analyze(ctx context.Context, opts AnalyzeOptions) ([]Pattern, error)

Analyze identifies patterns in memories for a project.

type DefaultCorrelator

type DefaultCorrelator struct{}

DefaultCorrelator implements correlation analysis between patterns.

func NewCorrelator

func NewCorrelator() *DefaultCorrelator

NewCorrelator creates a new correlation analyzer.

func (*DefaultCorrelator) Correlate

func (c *DefaultCorrelator) Correlate(patterns []Pattern, opts CorrelateOptions) ([]Correlation, error)

Correlate finds relationships between patterns.

type DefaultReporter

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

DefaultReporter implements report generation.

func NewReporter

func NewReporter(memorySvc *reasoningbank.Service) *DefaultReporter

NewReporter creates a new report generator.

func (*DefaultReporter) Generate

Generate creates a comprehensive reflection report.

type DomainCount

type DomainCount struct {
	Domain string `json:"domain"`
	Count  int    `json:"count"`
}

DomainCount represents a domain and its occurrence count.

type Insight

type Insight struct {
	// Title is a brief summary of the insight.
	Title string `json:"title"`
	// Description provides details about the insight.
	Description string `json:"description"`
	// Category classifies the insight.
	Category string `json:"category"`
	// Confidence score for this insight (0-1).
	Confidence float64 `json:"confidence"`
	// RelatedPatterns are pattern IDs that support this insight.
	RelatedPatterns []string `json:"related_patterns,omitempty"`
	// Recommendations based on this insight.
	Recommendations []string `json:"recommendations,omitempty"`
}

Insight represents a key learning derived from analysis.

type Pattern

type Pattern struct {
	// ID is the unique identifier for this pattern.
	ID string `json:"id"`
	// Category classifies the type of pattern.
	Category PatternCategory `json:"category"`
	// Description summarizes the pattern.
	Description string `json:"description"`
	// Tags associated with this pattern.
	Tags []string `json:"tags"`
	// Domains where this pattern appears.
	Domains []string `json:"domains"`
	// Frequency is how often this pattern occurs.
	Frequency int `json:"frequency"`
	// Confidence score for this pattern (0-1).
	Confidence float64 `json:"confidence"`
	// MemoryIDs are the memories that contributed to this pattern.
	MemoryIDs []string `json:"memory_ids"`
	// FirstSeen is when this pattern was first observed.
	FirstSeen time.Time `json:"first_seen"`
	// LastSeen is when this pattern was last observed.
	LastSeen time.Time `json:"last_seen"`
}

Pattern represents a behavioral pattern identified in memories.

type PatternCategory

type PatternCategory string

PatternCategory represents the category of a behavioral pattern.

const (
	// PatternSuccess indicates a pattern of successful behaviors.
	PatternSuccess PatternCategory = "success"
	// PatternFailure indicates a pattern of failed behaviors.
	PatternFailure PatternCategory = "failure"
	// PatternRecurring indicates a recurring behavioral pattern.
	PatternRecurring PatternCategory = "recurring"
	// PatternImproving indicates a pattern showing improvement.
	PatternImproving PatternCategory = "improving"
	// PatternDeclining indicates a pattern showing decline.
	PatternDeclining PatternCategory = "declining"
)

type ReflectionReport

type ReflectionReport struct {
	// ID is the unique identifier for this report.
	ID string `json:"id"`
	// ProjectID is the project this report covers.
	ProjectID string `json:"project_id"`
	// GeneratedAt is when the report was created.
	GeneratedAt time.Time `json:"generated_at"`
	// Period describes the time period covered.
	Period ReportPeriod `json:"period"`
	// Summary provides a high-level overview.
	Summary string `json:"summary"`
	// Patterns discovered during analysis.
	Patterns []Pattern `json:"patterns"`
	// Correlations between patterns/memories.
	Correlations []Correlation `json:"correlations"`
	// Insights derived from analysis.
	Insights []Insight `json:"insights"`
	// Statistics about the analyzed data.
	Statistics ReportStatistics `json:"statistics"`
	// Recommendations for improvement.
	Recommendations []string `json:"recommendations"`
}

ReflectionReport is a comprehensive analysis report.

type ReportOptions

type ReportOptions struct {
	// ProjectID to report on.
	ProjectID string
	// Period to cover.
	Period ReportPeriod
	// IncludePatterns in the report.
	IncludePatterns bool
	// IncludeCorrelations in the report.
	IncludeCorrelations bool
	// IncludeInsights in the report.
	IncludeInsights bool
	// MaxInsights to include.
	MaxInsights int
	// Format for output ("text", "json", "markdown").
	Format string
}

ReportOptions configures report generation.

type ReportPeriod

type ReportPeriod struct {
	// Start of the period.
	Start time.Time `json:"start"`
	// End of the period.
	End time.Time `json:"end"`
	// Description like "Last 7 days" or "This month".
	Description string `json:"description"`
}

ReportPeriod describes the time period for a report.

type ReportStatistics

type ReportStatistics struct {
	// TotalMemories analyzed.
	TotalMemories int `json:"total_memories"`
	// SuccessfulMemories count.
	SuccessfulMemories int `json:"successful_memories"`
	// FailedMemories count.
	FailedMemories int `json:"failed_memories"`
	// UniquePatterns identified.
	UniquePatterns int `json:"unique_patterns"`
	// UniqueCorrelations found.
	UniqueCorrelations int `json:"unique_correlations"`
	// TopTags most frequently used.
	TopTags []TagCount `json:"top_tags"`
	// TopDomains most active.
	TopDomains []DomainCount `json:"top_domains"`
	// SuccessRate overall.
	SuccessRate float64 `json:"success_rate"`
	// AverageConfidence across memories.
	AverageConfidence float64 `json:"average_confidence"`
}

ReportStatistics contains numerical summary of the report.

type Reporter

type Reporter interface {
	// Generate creates a reflection report.
	Generate(ctx context.Context, opts ReportOptions) (*ReflectionReport, error)
}

Reporter generates reflection reports.

type TagCount

type TagCount struct {
	Tag   string `json:"tag"`
	Count int    `json:"count"`
}

TagCount represents a tag and its occurrence count.

Jump to

Keyboard shortcuts

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