Files
goclaw/internal/tools/exec_memory_hints.go
viettranx 7ba3a5115a fix(tools): hint model when shell commands target DB-backed memory files
Memory files (MEMORY.md, memory/*.md) are stored in PostgreSQL, not on
disk. When the model uses exec to run cat/ls/grep/find on memory paths,
short-circuit with a hint to use memory_search or memory_get instead.
Also append [Source: database, not filesystem] to read_file and
list_files results served from the memory interceptor.
2026-04-02 19:28:04 +07:00

48 lines
1.3 KiB
Go

package tools
import "strings"
// memoryHintPrefixes are shell commands that read/list/search filesystem content.
// If these target memory paths, the model should use dedicated memory tools instead.
var memoryHintPrefixes = []string{
"cat ", "head ", "tail ", "less ", "more ",
"grep ", "rg ", "ag ",
"find ", "ls ", "wc ",
}
// memoryPathTokens are path fragments indicating memory file access.
var memoryPathTokens = []string{
"MEMORY.md", "memory.md", "memory/",
}
// MaybeMemoryExecHint checks if a shell command targets memory files
// (which live in the database, not on disk) and returns an LLM hint.
// Returns empty string if no memory path detected.
func MaybeMemoryExecHint(command string) string {
trimmed := strings.TrimSpace(command)
if trimmed == "" {
return ""
}
// Check if command uses a filesystem read/list/search binary.
hasReadCmd := false
for _, prefix := range memoryHintPrefixes {
if strings.HasPrefix(trimmed, prefix) {
hasReadCmd = true
break
}
}
if !hasReadCmd {
return ""
}
// Check if the command references a memory path.
for _, token := range memoryPathTokens {
if strings.Contains(trimmed, token) {
return "[HINT] Memory files are in the database, not on disk. Use memory_search or memory_get tool instead."
}
}
return ""
}