add startup session compaction check

This commit is contained in:
DBT
2026-02-23 10:33:21 +00:00
parent 3209396568
commit 27d765f58b
2 changed files with 78 additions and 24 deletions

View File

@@ -33,6 +33,9 @@ type AgentLoop struct {
sessions *session.SessionManager sessions *session.SessionManager
contextBuilder *ContextBuilder contextBuilder *ContextBuilder
tools *tools.ToolRegistry tools *tools.ToolRegistry
compactionEnabled bool
compactionTrigger int
compactionKeepRecent int
running bool running bool
} }
@@ -125,6 +128,9 @@ func NewAgentLoop(cfg *config.Config, msgBus *bus.MessageBus, provider providers
sessions: sessionsManager, sessions: sessionsManager,
contextBuilder: NewContextBuilder(workspace, func() []string { return toolsRegistry.GetSummaries() }), contextBuilder: NewContextBuilder(workspace, func() []string { return toolsRegistry.GetSummaries() }),
tools: toolsRegistry, tools: toolsRegistry,
compactionEnabled: cfg.Agents.Defaults.ContextCompaction.Enabled,
compactionTrigger: cfg.Agents.Defaults.ContextCompaction.TriggerMessages,
compactionKeepRecent: cfg.Agents.Defaults.ContextCompaction.KeepRecentMessages,
running: false, running: false,
} }
@@ -587,14 +593,52 @@ func truncate(s string, maxLen int) string {
} }
// GetStartupInfo returns information about loaded tools and skills for logging. // GetStartupInfo returns information about loaded tools and skills for logging.
// RunStartupSelfCheckAllSessions runs a lightweight startup check. // RunStartupSelfCheckAllSessions runs startup compaction checks across loaded sessions.
// Current implementation reports loaded session counts and leaves compaction as no-op.
func (al *AgentLoop) RunStartupSelfCheckAllSessions(ctx context.Context) StartupCompactionReport { func (al *AgentLoop) RunStartupSelfCheckAllSessions(ctx context.Context) StartupCompactionReport {
_ = ctx report := StartupCompactionReport{TotalSessions: al.sessions.Count()}
return StartupCompactionReport{ if !al.compactionEnabled {
TotalSessions: al.sessions.Count(), return report
CompactedSessions: 0,
} }
trigger := al.compactionTrigger
if trigger <= 0 {
trigger = 60
}
keepRecent := al.compactionKeepRecent
if keepRecent <= 0 || keepRecent >= trigger {
keepRecent = trigger / 2
if keepRecent < 10 {
keepRecent = 10
}
}
for _, key := range al.sessions.Keys() {
select {
case <-ctx.Done():
return report
default:
}
history := al.sessions.GetHistory(key)
if len(history) <= trigger {
continue
}
removed := len(history) - keepRecent
summary := al.sessions.GetSummary(key)
note := fmt.Sprintf("[startup-compaction] removed %d old messages, kept %d recent messages", removed, keepRecent)
if strings.TrimSpace(summary) == "" {
al.sessions.SetSummary(key, note)
} else {
al.sessions.SetSummary(key, summary+"\n"+note)
}
al.sessions.TruncateHistory(key, keepRecent)
al.sessions.Save(al.sessions.GetOrCreate(key))
report.CompactedSessions++
}
return report
} }
func (al *AgentLoop) GetStartupInfo() map[string]interface{} { func (al *AgentLoop) GetStartupInfo() map[string]interface{} {

View File

@@ -237,6 +237,16 @@ func (sm *SessionManager) Count() int {
return len(sm.sessions) return len(sm.sessions)
} }
func (sm *SessionManager) Keys() []string {
sm.mu.RLock()
defer sm.mu.RUnlock()
keys := make([]string, 0, len(sm.sessions))
for k := range sm.sessions {
keys = append(keys, k)
}
return keys
}
func (sm *SessionManager) loadSessions() error { func (sm *SessionManager) loadSessions() error {
files, err := os.ReadDir(sm.storage) files, err := os.ReadDir(sm.storage)
if err != nil { if err != nil {