feat(agent): support global memory inheritance in isolated agents

This commit is contained in:
stevef 2026-04-19 10:48:45 +02:00
parent 94a2756b8e
commit 8106d61011
2 changed files with 30 additions and 10 deletions

View file

@ -86,7 +86,7 @@ func NewContextBuilder(workspace string, baseWorkspace string) *ContextBuilder {
workspace: workspace, workspace: workspace,
baseWorkspace: baseWorkspace, baseWorkspace: baseWorkspace,
skillsLoader: skills.NewSkillsLoader(workspace, baseWorkspace, globalSkillsDir, builtinSkillsDir, nil, false), skillsLoader: skills.NewSkillsLoader(workspace, baseWorkspace, globalSkillsDir, builtinSkillsDir, nil, false),
memory: NewMemoryStore(workspace), memory: NewMemoryStore(workspace, baseWorkspace),
} }
} }

View file

@ -21,13 +21,15 @@ import (
// - Daily notes: memory/YYYYMM/YYYYMMDD.md // - Daily notes: memory/YYYYMM/YYYYMMDD.md
type MemoryStore struct { type MemoryStore struct {
workspace string workspace string
baseWorkspace string
memoryDir string memoryDir string
memoryFile string memoryFile string
} }
// NewMemoryStore creates a new MemoryStore with the given workspace path. // NewMemoryStore creates a new MemoryStore with the given workspace path.
// It also takes an optional baseWorkspace for global memory inheritance.
// It ensures the memory directory exists. // It ensures the memory directory exists.
func NewMemoryStore(workspace string) *MemoryStore { func NewMemoryStore(workspace string, baseWorkspace string) *MemoryStore {
memoryDir := filepath.Join(workspace, "memory") memoryDir := filepath.Join(workspace, "memory")
memoryFile := filepath.Join(memoryDir, "MEMORY.md") memoryFile := filepath.Join(memoryDir, "MEMORY.md")
@ -36,6 +38,7 @@ func NewMemoryStore(workspace string) *MemoryStore {
return &MemoryStore{ return &MemoryStore{
workspace: workspace, workspace: workspace,
baseWorkspace: baseWorkspace,
memoryDir: memoryDir, memoryDir: memoryDir,
memoryFile: memoryFile, memoryFile: memoryFile,
} }
@ -131,18 +134,35 @@ func (ms *MemoryStore) GetRecentDailyNotes(days int) string {
// GetMemoryContext returns formatted memory context for the agent prompt. // GetMemoryContext returns formatted memory context for the agent prompt.
// Includes long-term memory and recent daily notes. // Includes long-term memory and recent daily notes.
// If baseWorkspace is set, it also includes global memory context.
func (ms *MemoryStore) GetMemoryContext() string { func (ms *MemoryStore) GetMemoryContext() string {
longTerm := ms.ReadLongTerm() longTerm := ms.ReadLongTerm()
recentNotes := ms.GetRecentDailyNotes(3) recentNotes := ms.GetRecentDailyNotes(3)
if longTerm == "" && recentNotes == "" { var globalLongTerm string
if ms.baseWorkspace != "" && ms.baseWorkspace != ms.workspace {
globalFile := filepath.Join(ms.baseWorkspace, "memory", "MEMORY.md")
if data, err := os.ReadFile(globalFile); err == nil {
globalLongTerm = string(data)
}
}
if longTerm == "" && recentNotes == "" && globalLongTerm == "" {
return "" return ""
} }
var sb strings.Builder var sb strings.Builder
if globalLongTerm != "" {
sb.WriteString("## Global Memory\n\n")
sb.WriteString(globalLongTerm)
if longTerm != "" || recentNotes != "" {
sb.WriteString("\n\n---\n\n")
}
}
if longTerm != "" { if longTerm != "" {
sb.WriteString("## Long-term Memory\n\n") sb.WriteString("## Session Memory\n\n")
sb.WriteString(longTerm) sb.WriteString(longTerm)
} }