feat: store PicoClaw memory in SQLite
Store session history, preferences, and dated records in one database per home. Commit complete turns, checkpoint tool batches, and compact history by prefix. Add memory search and writes, with previews for ambiguous corrections. Review conversations in the background and support SQLite backup and restore.
This commit is contained in:
1 parent
cd4a95934a
commit
b8b11d452f
112 files changed
+8117
-6310
No files matched your search
@@ -0,0 +1,235 @@
|
||||
package session_test
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
"github.com/sipeed/picoclaw/pkg/memory"
|
||||
"github.com/sipeed/picoclaw/pkg/providers"
|
||||
"github.com/sipeed/picoclaw/pkg/session"
|
||||
)
|
||||
|
||||
// Compile-time interface satisfaction checks.
|
||||
var (
|
||||
_ session.SessionStore = (*session.SessionManager)(nil)
|
||||
_ session.SessionStore = (*session.SQLiteSessionStore)(nil)
|
||||
)
|
||||
|
||||
func newBackend(t *testing.T) *session.SQLiteSessionStore {
|
||||
t.Helper()
|
||||
store, err := memory.Open(memory.Path(t.TempDir()))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Cleanup(func() { store.Close() })
|
||||
return session.NewSQLiteSessionStore(store)
|
||||
}
|
||||
|
||||
func TestSQLiteSessionStore_AddAndGetHistory(t *testing.T) {
|
||||
b := newBackend(t)
|
||||
|
||||
b.AddMessage("s1", "user", "hello")
|
||||
b.AddMessage("s1", "assistant", "hi")
|
||||
|
||||
history := b.GetHistory("s1")
|
||||
if len(history) != 2 {
|
||||
t.Fatalf("got %d messages, want 2", len(history))
|
||||
}
|
||||
if history[0].Role != "user" || history[0].Content != "hello" {
|
||||
t.Errorf("msg[0] = %+v", history[0])
|
||||
}
|
||||
if history[1].Role != "assistant" || history[1].Content != "hi" {
|
||||
t.Errorf("msg[1] = %+v", history[1])
|
||||
}
|
||||
}
|
||||
|
||||
func TestSQLiteSessionStore_AddFullMessage(t *testing.T) {
|
||||
b := newBackend(t)
|
||||
|
||||
msg := providers.Message{
|
||||
Role: "assistant",
|
||||
Content: "done",
|
||||
ToolCalls: []providers.ToolCall{
|
||||
{ID: "tc1", Function: &providers.FunctionCall{Name: "read_file", Arguments: `{"path":"x"}`}},
|
||||
},
|
||||
}
|
||||
b.AddFullMessage("s1", msg)
|
||||
|
||||
history := b.GetHistory("s1")
|
||||
if len(history) != 1 {
|
||||
t.Fatalf("got %d, want 1", len(history))
|
||||
}
|
||||
if len(history[0].ToolCalls) != 1 || history[0].ToolCalls[0].ID != "tc1" {
|
||||
t.Errorf("tool calls = %+v", history[0].ToolCalls)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSQLiteSessionStore_AddFullMessage_PreservesModelName(t *testing.T) {
|
||||
b := newBackend(t)
|
||||
|
||||
msg := providers.Message{
|
||||
Role: "assistant",
|
||||
Content: "done",
|
||||
ModelName: "gpt-5.4-mini",
|
||||
}
|
||||
b.AddFullMessage("s1", msg)
|
||||
|
||||
history := b.GetHistory("s1")
|
||||
if len(history) != 1 {
|
||||
t.Fatalf("got %d, want 1", len(history))
|
||||
}
|
||||
if history[0].ModelName != "gpt-5.4-mini" {
|
||||
t.Fatalf("ModelName = %q, want %q", history[0].ModelName, "gpt-5.4-mini")
|
||||
}
|
||||
}
|
||||
|
||||
func TestSQLiteSessionStore_Summary(t *testing.T) {
|
||||
b := newBackend(t)
|
||||
|
||||
if got := b.GetSummary("s1"); got != "" {
|
||||
t.Errorf("got %q, want empty", got)
|
||||
}
|
||||
|
||||
b.SetSummary("s1", "test summary")
|
||||
if got := b.GetSummary("s1"); got != "test summary" {
|
||||
t.Errorf("got %q, want %q", got, "test summary")
|
||||
}
|
||||
}
|
||||
|
||||
func TestSQLiteSessionStore_TruncateAndSave(t *testing.T) {
|
||||
b := newBackend(t)
|
||||
|
||||
for i := 0; i < 10; i++ {
|
||||
b.AddMessage("s1", "user", fmt.Sprintf("msg %d", i))
|
||||
}
|
||||
b.TruncateHistory("s1", 3)
|
||||
|
||||
history := b.GetHistory("s1")
|
||||
if len(history) != 3 {
|
||||
t.Fatalf("got %d, want 3", len(history))
|
||||
}
|
||||
if history[0].Content != "msg 7" {
|
||||
t.Errorf("got %q, want %q", history[0].Content, "msg 7")
|
||||
}
|
||||
|
||||
// Save reports any pending write errors.
|
||||
if err := b.Save("s1"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
// The retained messages stay accessible after Save.
|
||||
history = b.GetHistory("s1")
|
||||
if len(history) != 3 {
|
||||
t.Fatalf("after save: got %d, want 3", len(history))
|
||||
}
|
||||
}
|
||||
|
||||
func TestSQLiteSessionStore_SetHistory(t *testing.T) {
|
||||
b := newBackend(t)
|
||||
b.AddMessage("s1", "user", "old")
|
||||
|
||||
b.SetHistory("s1", []providers.Message{
|
||||
{Role: "user", Content: "new1"},
|
||||
{Role: "assistant", Content: "new2"},
|
||||
})
|
||||
|
||||
history := b.GetHistory("s1")
|
||||
if len(history) != 2 {
|
||||
t.Fatalf("got %d, want 2", len(history))
|
||||
}
|
||||
if history[0].Content != "new1" {
|
||||
t.Errorf("got %q, want %q", history[0].Content, "new1")
|
||||
}
|
||||
}
|
||||
|
||||
func TestSQLiteSessionStore_EmptySession(t *testing.T) {
|
||||
b := newBackend(t)
|
||||
|
||||
history := b.GetHistory("nonexistent")
|
||||
if history == nil {
|
||||
t.Fatal("got nil, want empty slice")
|
||||
}
|
||||
if len(history) != 0 {
|
||||
t.Errorf("got %d, want 0", len(history))
|
||||
}
|
||||
}
|
||||
|
||||
func TestSQLiteSessionStore_SessionIsolation(t *testing.T) {
|
||||
b := newBackend(t)
|
||||
b.AddMessage("s1", "user", "session1")
|
||||
b.AddMessage("s2", "user", "session2")
|
||||
|
||||
h1 := b.GetHistory("s1")
|
||||
h2 := b.GetHistory("s2")
|
||||
|
||||
if len(h1) != 1 || h1[0].Content != "session1" {
|
||||
t.Errorf("s1: %+v", h1)
|
||||
}
|
||||
if len(h2) != 1 || h2[0].Content != "session2" {
|
||||
t.Errorf("s2: %+v", h2)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSQLiteSessionStore_SummarizeFlow(t *testing.T) {
|
||||
// Simulates the real summarization flow in the agent loop:
|
||||
// SetSummary → TruncateHistory → Save
|
||||
b := newBackend(t)
|
||||
|
||||
for i := 0; i < 20; i++ {
|
||||
b.AddMessage("s1", "user", fmt.Sprintf("msg %d", i))
|
||||
}
|
||||
|
||||
b.SetSummary("s1", "conversation about testing")
|
||||
b.TruncateHistory("s1", 4)
|
||||
if err := b.Save("s1"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if got := b.GetSummary("s1"); got != "conversation about testing" {
|
||||
t.Errorf("summary = %q", got)
|
||||
}
|
||||
history := b.GetHistory("s1")
|
||||
if len(history) != 4 {
|
||||
t.Fatalf("got %d messages, want 4", len(history))
|
||||
}
|
||||
if history[0].Content != "msg 16" {
|
||||
t.Errorf("first message = %q, want %q", history[0].Content, "msg 16")
|
||||
}
|
||||
}
|
||||
|
||||
func TestSQLiteSessionStore_ResolveAliasAndPersistMetadata(t *testing.T) {
|
||||
b := newBackend(t)
|
||||
|
||||
scope := &session.SessionScope{
|
||||
Version: session.ScopeVersionV1,
|
||||
AgentID: "main",
|
||||
Channel: "telegram",
|
||||
Account: "default",
|
||||
Dimensions: []string{"chat"},
|
||||
Values: map[string]string{
|
||||
"chat": "group:c1",
|
||||
},
|
||||
}
|
||||
b.EnsureSessionMetadata("canonical", scope, []string{"legacy"})
|
||||
|
||||
if got := b.ResolveSessionKey("legacy"); got != "canonical" {
|
||||
t.Fatalf("ResolveSessionKey() = %q, want %q", got, "canonical")
|
||||
}
|
||||
|
||||
b.AddMessage("legacy", "user", "hello through alias")
|
||||
history := b.GetHistory("canonical")
|
||||
if len(history) != 1 {
|
||||
t.Fatalf("len(history) = %d, want 1", len(history))
|
||||
}
|
||||
if history[0].Content != "hello through alias" {
|
||||
t.Fatalf("history[0].Content = %q, want %q", history[0].Content, "hello through alias")
|
||||
}
|
||||
|
||||
resolvedScope := b.GetSessionScope("legacy")
|
||||
if resolvedScope == nil {
|
||||
t.Fatal("GetSessionScope() returned nil")
|
||||
}
|
||||
if resolvedScope.AgentID != scope.AgentID || resolvedScope.Values["chat"] != scope.Values["chat"] {
|
||||
t.Fatalf("GetSessionScope() = %+v, want %+v", resolvedScope, scope)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user