mirror of
https://github.com/YspCoder/clawgo.git
synced 2026-05-07 07:27:31 +08:00
add startup session compaction check
This commit is contained in:
@@ -25,15 +25,18 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type AgentLoop struct {
|
type AgentLoop struct {
|
||||||
bus *bus.MessageBus
|
bus *bus.MessageBus
|
||||||
provider providers.LLMProvider
|
provider providers.LLMProvider
|
||||||
workspace string
|
workspace string
|
||||||
model string
|
model string
|
||||||
maxIterations int
|
maxIterations int
|
||||||
sessions *session.SessionManager
|
sessions *session.SessionManager
|
||||||
contextBuilder *ContextBuilder
|
contextBuilder *ContextBuilder
|
||||||
tools *tools.ToolRegistry
|
tools *tools.ToolRegistry
|
||||||
running bool
|
compactionEnabled bool
|
||||||
|
compactionTrigger int
|
||||||
|
compactionKeepRecent int
|
||||||
|
running bool
|
||||||
}
|
}
|
||||||
|
|
||||||
// StartupCompactionReport provides startup memory/session maintenance stats.
|
// StartupCompactionReport provides startup memory/session maintenance stats.
|
||||||
@@ -117,15 +120,18 @@ func NewAgentLoop(cfg *config.Config, msgBus *bus.MessageBus, provider providers
|
|||||||
sessionsManager := session.NewSessionManager(filepath.Join(filepath.Dir(cfg.WorkspacePath()), "sessions"))
|
sessionsManager := session.NewSessionManager(filepath.Join(filepath.Dir(cfg.WorkspacePath()), "sessions"))
|
||||||
|
|
||||||
loop := &AgentLoop{
|
loop := &AgentLoop{
|
||||||
bus: msgBus,
|
bus: msgBus,
|
||||||
provider: provider,
|
provider: provider,
|
||||||
workspace: workspace,
|
workspace: workspace,
|
||||||
model: provider.GetDefaultModel(),
|
model: provider.GetDefaultModel(),
|
||||||
maxIterations: cfg.Agents.Defaults.MaxToolIterations,
|
maxIterations: cfg.Agents.Defaults.MaxToolIterations,
|
||||||
sessions: sessionsManager,
|
sessions: sessionsManager,
|
||||||
contextBuilder: NewContextBuilder(workspace, func() []string { return toolsRegistry.GetSummaries() }),
|
contextBuilder: NewContextBuilder(workspace, func() []string { return toolsRegistry.GetSummaries() }),
|
||||||
tools: toolsRegistry,
|
tools: toolsRegistry,
|
||||||
running: false,
|
compactionEnabled: cfg.Agents.Defaults.ContextCompaction.Enabled,
|
||||||
|
compactionTrigger: cfg.Agents.Defaults.ContextCompaction.TriggerMessages,
|
||||||
|
compactionKeepRecent: cfg.Agents.Defaults.ContextCompaction.KeepRecentMessages,
|
||||||
|
running: false,
|
||||||
}
|
}
|
||||||
|
|
||||||
// 注入递归运行逻辑,使 subagent 具备 full tool-calling 能力
|
// 注入递归运行逻辑,使 subagent 具备 full tool-calling 能力
|
||||||
@@ -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{} {
|
||||||
|
|||||||
@@ -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 {
|
||||||
|
|||||||
Reference in New Issue
Block a user