Documentation
¶
Overview ¶
Package knight implements the background auto-evolution agent. Knight runs during idle time in daemon mode, analyzing sessions, creating and validating skills, generating tests, and maintaining project health.
Index ¶
- func CheckDuplicate(entry *SkillEntry, existing []*SkillEntry) bool
- func FormatSkillRefForDisplay(scope, name string) string
- type AgentFactory
- type AgentRunner
- type AnalysisResult
- type Budget
- type CandidateQueue
- type Emitter
- type Knight
- func (k *Knight) BudgetStatus() (used int, remaining int, limit int)
- func (k *Knight) CanPerformTask() bool
- func (k *Knight) FindActiveSkill(ref string) (*SkillEntry, error)
- func (k *Knight) FindStagingSkill(ref string) (*SkillEntry, error)
- func (k *Knight) Index() *SkillIndex
- func (k *Knight) NotifyActivity()
- func (k *Knight) PerformSkillAnalysis(ctx context.Context) error
- func (k *Knight) PerformSkillValidation(ctx context.Context) ([]ValidationResult, error)
- func (k *Knight) PromoteStaging(skillName string) error
- func (k *Knight) RecordSkillEffectiveness(ref string, score int)
- func (k *Knight) RecordSkillUse(ref string)
- func (k *Knight) RejectStaging(skillName string) error
- func (k *Knight) RollbackSkill(name string) error
- func (k *Knight) RunAdhocTask(ctx context.Context, goal string) (TaskResult, error)
- func (k *Knight) RunTask(ctx context.Context, taskName, prompt string, factory AgentFactory) TaskResult
- func (k *Knight) Running() bool
- func (k *Knight) SetEmitter(e Emitter)
- func (k *Knight) SetFactory(f AgentFactory)
- func (k *Knight) SetSkillFrozen(name string, frozen bool) error
- func (k *Knight) SkillFeedback(ref string) (avgScore float64, samples int)
- func (k *Knight) SkillUsage(ref string) (count int, lastUsed time.Time, avgScore float64)
- func (k *Knight) Start(ctx context.Context) error
- func (k *Knight) Status() string
- func (k *Knight) Stop()
- type Promoter
- type SessionAnalyzer
- func (sa *SessionAnalyzer) AnalyzeRecent(ctx context.Context) (*AnalysisResult, error)
- func (sa *SessionAnalyzer) CollectProjectConventions() string
- func (sa *SessionAnalyzer) GenerateSkillFromAnalysis(ctx context.Context, candidate SkillCandidate, factory AgentFactory) (string, error)
- func (sa *SessionAnalyzer) RecordUsage(usage provider.TokenUsage)
- type SkillCandidate
- type SkillEntry
- type SkillIndex
- func (si *SkillIndex) ActiveSkills() ([]*SkillEntry, error)
- func (si *SkillIndex) Directories() []string
- func (si *SkillIndex) FindActiveByName(name string) *SkillEntry
- func (si *SkillIndex) Invalidate()
- func (si *SkillIndex) Scan() ([]*SkillEntry, error)
- func (si *SkillIndex) StagingSkills() ([]*SkillEntry, error)
- type SkillMeta
- type TaskResult
- type UsageTracker
- func (ut *UsageTracker) AllUsage() map[string]skillUsage
- func (ut *UsageTracker) EnsureDir() error
- func (ut *UsageTracker) Flush()
- func (ut *UsageTracker) GetFeedback(name string) (avgScore float64, samples int)
- func (ut *UsageTracker) GetUsage(name string) (count int, lastUsed time.Time, avgScore float64)
- func (ut *UsageTracker) IsStale(name string, threshold time.Duration) bool
- func (ut *UsageTracker) RecordEffectiveness(name string, score int)
- func (ut *UsageTracker) RecordUse(name string)
- func (ut *UsageTracker) Snapshot(name string) (skillUsage, bool)
- type ValidationResult
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func CheckDuplicate ¶
func CheckDuplicate(entry *SkillEntry, existing []*SkillEntry) bool
CheckDuplicate returns true if a skill with similar name already exists.
func FormatSkillRefForDisplay ¶
FormatSkillRefForDisplay returns the canonical scope-qualified skill reference.
Types ¶
type AgentFactory ¶
type AgentFactory func(systemPrompt string, maxTurns int, onUsage func(provider.TokenUsage)) (AgentRunner, error)
AgentFactory creates a Knight agent with restricted tools. The onUsage callback receives token usage after each LLM call.
type AgentRunner ¶
type AgentRunner interface {
RunStream(ctx context.Context, prompt string, onEvent func(provider.StreamEvent)) error
}
AgentRunner is the interface Knight uses to run LLM-powered tasks. It mirrors subagent.AgentRunner for compatibility.
type AnalysisResult ¶
type AnalysisResult struct {
SessionsAnalyzed int
SkillCandidates []SkillCandidate
}
AnalysisResult holds the outcome of a session analysis.
type Budget ¶
type Budget struct {
// contains filtered or unexported fields
}
Budget tracks daily token consumption for Knight.
func NewBudget ¶
func NewBudget(dir string, cfg config.KnightConfig) *Budget
NewBudget creates a Budget tracker. Data is stored under dir/knight/.
func (*Budget) CanSpend ¶
CanSpend returns true if Knight has enough remaining budget to start a task.
func (*Budget) DailyLimit ¶
DailyLimit returns the configured daily budget.
type CandidateQueue ¶
type CandidateQueue struct {
// contains filtered or unexported fields
}
CandidateQueue persists deferred high-value skill candidates between runs.
func NewCandidateQueue ¶
func NewCandidateQueue(path string) *CandidateQueue
NewCandidateQueue creates a queue backed by the given JSON file path.
func (*CandidateQueue) EnsureDir ¶
func (q *CandidateQueue) EnsureDir() error
EnsureDir creates the parent directory for the queue file.
func (*CandidateQueue) List ¶
func (q *CandidateQueue) List() ([]SkillCandidate, error)
List returns the current deferred candidates sorted by priority.
func (*CandidateQueue) Remove ¶
func (q *CandidateQueue) Remove(candidate SkillCandidate) error
Remove deletes a candidate from the queue if present.
func (*CandidateQueue) Upsert ¶
func (q *CandidateQueue) Upsert(candidate SkillCandidate) error
Upsert stores or updates a deferred candidate.
type Knight ¶
type Knight struct {
// contains filtered or unexported fields
}
Knight is the background auto-evolution agent. It runs scheduled tasks during idle time, analyzes sessions, creates and validates skills.
func (*Knight) BudgetStatus ¶
BudgetStatus returns current Knight token usage counters.
func (*Knight) CanPerformTask ¶
CanPerformTask checks if Knight has budget and is allowed to run.
func (*Knight) FindActiveSkill ¶
func (k *Knight) FindActiveSkill(ref string) (*SkillEntry, error)
FindActiveSkill resolves an active skill reference, optionally scoped as project:name or global:name.
func (*Knight) FindStagingSkill ¶
func (k *Knight) FindStagingSkill(ref string) (*SkillEntry, error)
FindStagingSkill resolves a staging skill reference, optionally scoped as project:name or global:name.
func (*Knight) Index ¶
func (k *Knight) Index() *SkillIndex
Index returns the skill index for external access.
func (*Knight) NotifyActivity ¶
func (k *Knight) NotifyActivity()
NotifyActivity is called when the user sends a message, resetting the idle timer.
func (*Knight) PerformSkillAnalysis ¶
PerformSkillAnalysis triggers an immediate skill analysis task.
func (*Knight) PerformSkillValidation ¶
func (k *Knight) PerformSkillValidation(ctx context.Context) ([]ValidationResult, error)
PerformSkillValidation validates all active skills.
func (*Knight) PromoteStaging ¶
PromoteStaging promotes a staging skill to active after validation.
func (*Knight) RecordSkillEffectiveness ¶
RecordSkillEffectiveness records a 1-5 effectiveness score for a skill.
func (*Knight) RecordSkillUse ¶
RecordSkillUse increments the usage counter for a skill. Called from the skill tool when a Knight-managed skill is loaded.
func (*Knight) RejectStaging ¶
RejectStaging removes a staging skill.
func (*Knight) RollbackSkill ¶
RollbackSkill restores the latest snapshot for an active skill.
func (*Knight) RunAdhocTask ¶
RunAdhocTask executes a user-directed Knight task using the configured agent factory.
func (*Knight) RunTask ¶
func (k *Knight) RunTask(ctx context.Context, taskName, prompt string, factory AgentFactory) TaskResult
RunTask executes a single Knight task with budget tracking. Token usage is tracked via the onUsage callback wired into the agent.
func (*Knight) SetEmitter ¶
SetEmitter sets the IM emitter for Knight notifications.
func (*Knight) SetFactory ¶
func (k *Knight) SetFactory(f AgentFactory)
SetFactory sets the agent factory for LLM-powered tasks.
func (*Knight) SetSkillFrozen ¶
SetSkillFrozen updates an active skill's frozen flag.
func (*Knight) SkillFeedback ¶
SkillFeedback returns runtime feedback stats for a skill.
func (*Knight) SkillUsage ¶
SkillUsage returns runtime usage stats for a skill.
type Promoter ¶
type Promoter struct {
// contains filtered or unexported fields
}
Promoter handles moving skills from staging to active directories.
func NewPromoter ¶
NewPromoter creates a skill promoter.
func (*Promoter) Promote ¶
func (p *Promoter) Promote(entry *SkillEntry) error
Promote moves a skill from staging to the active skills directory. It creates a snapshot of any existing skill with the same name for rollback.
func (*Promoter) Reject ¶
func (p *Promoter) Reject(entry *SkillEntry) error
Reject removes a skill from staging.
func (*Promoter) Rollback ¶
func (p *Promoter) Rollback(entry *SkillEntry) error
Rollback restores the most recent snapshot for an active skill.
type SessionAnalyzer ¶
type SessionAnalyzer struct {
// contains filtered or unexported fields
}
SessionAnalyzer analyzes session history to discover reusable patterns.
func NewSessionAnalyzer ¶
func NewSessionAnalyzer(k *Knight) *SessionAnalyzer
NewSessionAnalyzer creates a session analyzer.
func (*SessionAnalyzer) AnalyzeRecent ¶
func (sa *SessionAnalyzer) AnalyzeRecent(ctx context.Context) (*AnalysisResult, error)
AnalyzeRecent scans recent sessions for reusable patterns.
func (*SessionAnalyzer) CollectProjectConventions ¶
func (sa *SessionAnalyzer) CollectProjectConventions() string
CollectProjectConventions reads well-known project config files for context.
func (*SessionAnalyzer) GenerateSkillFromAnalysis ¶
func (sa *SessionAnalyzer) GenerateSkillFromAnalysis(ctx context.Context, candidate SkillCandidate, factory AgentFactory) (string, error)
GenerateSkillFromAnalysis uses LLM to create a skill from a high-value candidate.
func (*SessionAnalyzer) RecordUsage ¶
func (sa *SessionAnalyzer) RecordUsage(usage provider.TokenUsage)
RecordUsage records token usage for session analysis tasks.
type SkillCandidate ¶
type SkillCandidate struct {
Name string
Description string
Scope string // "global" or "project"
Score float64
Reason string
EvidenceCount int
SourceSessions []string
}
SkillCandidate is a potential skill discovered from session analysis.
type SkillEntry ¶
type SkillEntry struct {
Name string
Meta SkillMeta
Path string // full path to the skill file
Scope string // "global" or "project"
Staging bool // true if in staging directory
LoadedAt time.Time
}
SkillEntry is a discovered skill with its metadata and file location.
type SkillIndex ¶
type SkillIndex struct {
// contains filtered or unexported fields
}
SkillIndex scans and indexes skills from both global and project directories. Results are cached with a TTL to avoid repeated disk reads.
func NewSkillIndex ¶
func NewSkillIndex(homeDir, projectDir string) *SkillIndex
NewSkillIndex creates a skill index for the given home and project dirs.
func (*SkillIndex) ActiveSkills ¶
func (si *SkillIndex) ActiveSkills() ([]*SkillEntry, error)
ActiveSkills returns only active (non-staging) skills.
func (*SkillIndex) Directories ¶
func (si *SkillIndex) Directories() []string
Directories returns all skill directories for display purposes.
func (*SkillIndex) FindActiveByName ¶
func (si *SkillIndex) FindActiveByName(name string) *SkillEntry
FindActiveByName finds an active skill by name.
func (*SkillIndex) Invalidate ¶
func (si *SkillIndex) Invalidate()
Invalidate clears the cache, forcing a fresh scan on next access.
func (*SkillIndex) Scan ¶
func (si *SkillIndex) Scan() ([]*SkillEntry, error)
Scan returns all discovered skills (active + staging), using cache when fresh.
func (*SkillIndex) StagingSkills ¶
func (si *SkillIndex) StagingSkills() ([]*SkillEntry, error)
StagingSkills returns only staging skills.
type SkillMeta ¶
type SkillMeta struct {
Name string `yaml:"name"`
Description string `yaml:"description"`
Scope string `yaml:"scope"` // "global" or "project"
Platforms []string `yaml:"platforms"`
Requires []string `yaml:"requires"`
CreatedBy string `yaml:"created_by"` // "user", "knight", "agent"
CreatedFrom string `yaml:"created_from"` // source session ID
CreatedAt string `yaml:"created_at"`
UpdatedAt string `yaml:"updated_at"`
UsageCount int `yaml:"usage_count"`
LastUsed string `yaml:"last_used"`
EffectivenessScores []int `yaml:"effectiveness_scores"`
Frozen bool `yaml:"frozen"`
}
SkillMeta is the parsed frontmatter from a SKILL.md or staging skill file.
type TaskResult ¶
type TaskResult struct {
TaskName string
Output string
Tokens provider.TokenUsage
Duration time.Duration
Error error
}
TaskResult holds the outcome of a Knight task execution.
type UsageTracker ¶
type UsageTracker struct {
// contains filtered or unexported fields
}
UsageTracker tracks skill usage for Knight's lifecycle management. Persists to a JSON file in the project's .ggcode/ directory. Uses a dirty flag to batch disk writes — only saves when data changed and at most once per writeInterval.
func NewUsageTracker ¶
func NewUsageTracker(path string) *UsageTracker
NewUsageTracker creates a usage tracker that persists to the given path.
func (*UsageTracker) AllUsage ¶
func (ut *UsageTracker) AllUsage() map[string]skillUsage
AllUsage returns a snapshot of all usage data.
func (*UsageTracker) EnsureDir ¶
func (ut *UsageTracker) EnsureDir() error
EnsureDir creates the parent directory for the usage file.
func (*UsageTracker) Flush ¶
func (ut *UsageTracker) Flush()
Flush persists dirty data to disk. Called periodically by the scheduler.
func (*UsageTracker) GetFeedback ¶
func (ut *UsageTracker) GetFeedback(name string) (avgScore float64, samples int)
GetFeedback returns the average effectiveness score and sample count.
func (*UsageTracker) IsStale ¶
func (ut *UsageTracker) IsStale(name string, threshold time.Duration) bool
IsStale returns true if a skill hasn't been used for the given duration.
func (*UsageTracker) RecordEffectiveness ¶
func (ut *UsageTracker) RecordEffectiveness(name string, score int)
RecordEffectiveness adds an effectiveness score (1-5) for a skill.
func (*UsageTracker) RecordUse ¶
func (ut *UsageTracker) RecordUse(name string)
RecordUse increments the usage count and updates last_used for a skill.
func (*UsageTracker) Snapshot ¶
func (ut *UsageTracker) Snapshot(name string) (skillUsage, bool)
type ValidationResult ¶
type ValidationResult struct {
Valid bool `json:"valid"`
Errors []string `json:"errors,omitempty"`
Warnings []string `json:"warnings,omitempty"`
}
ValidationResult contains the outcome of skill validation.
func ValidateSkill ¶
func ValidateSkill(entry *SkillEntry) ValidationResult
ValidateSkill runs all validation checks on a skill entry.