Keep personal notes private by default and require review before publishing shared notes. Restore backups and replay forgotten notes in one transaction, and retain the installed release when an unattended rollback could bring back forgotten notes.
157 lines
5.4 KiB
Go
157 lines
5.4 KiB
Go
package chat
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"log/slog"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/google/uuid"
|
|
|
|
"github.com/samcharles93/archie-core/internal/platform/hub"
|
|
"github.com/samcharles93/archie-core/modules/chat/internal/store"
|
|
"github.com/samcharles93/archie-core/modules/governance"
|
|
"github.com/samcharles93/archie-core/modules/memory"
|
|
)
|
|
|
|
// memoryFake records the person each memory call was made for.
|
|
type memoryFake struct {
|
|
Memory
|
|
remembered []string
|
|
listed []string
|
|
proposed []string
|
|
forgot bool
|
|
forgetErr error
|
|
}
|
|
|
|
func (f *memoryFake) Notes(_ context.Context, owner string, _ memory.Class) ([]memory.Note, error) {
|
|
f.listed = append(f.listed, owner)
|
|
return nil, nil
|
|
}
|
|
|
|
func (f *memoryFake) Remember(_ context.Context, owner, text string) (memory.Note, error) {
|
|
f.remembered = append(f.remembered, owner+": "+text)
|
|
return memory.Note{ID: uuid.New()}, nil
|
|
}
|
|
|
|
func (f *memoryFake) Propose(_ context.Context, owner, text string) (memory.Note, error) {
|
|
f.proposed = append(f.proposed, owner+": "+text)
|
|
return memory.Note{ID: uuid.New()}, nil
|
|
}
|
|
|
|
func (f *memoryFake) Forget(context.Context, string, uuid.UUID) (bool, error) {
|
|
return f.forgot, f.forgetErr
|
|
}
|
|
|
|
// memoryDeps builds the module around a memory fake and one conversation alice
|
|
// owns.
|
|
func memoryDeps(t *testing.T) (*Module, Deps, *memoryFake, store.Turn) {
|
|
t.Helper()
|
|
session := uuid.New()
|
|
fs := &fakeStore{sessions: map[uuid.UUID]store.Session{session: {ID: session, Owner: alice}}, turns: map[uuid.UUID]store.Turn{}, events: map[uuid.UUID][]store.Event{}}
|
|
remember := &memoryFake{}
|
|
d := Deps{
|
|
People: fakePeople{alice: person(alice, governance.RoleAdmin)},
|
|
Access: governance.NewEngine(governance.ShippedOrgPolicies(governance.DefaultOrgID)),
|
|
Memory: remember,
|
|
}
|
|
m := &Module{orgID: "default", store: fs, log: slog.New(slog.DiscardHandler), hub: hub.New()}
|
|
return m, d, remember, store.Turn{ID: uuid.New(), SessionID: session, Owner: alice}
|
|
}
|
|
|
|
// TestMemoryToolsActAsTheTurnsOwner guards the scope boundary at the tool
|
|
// boundary: every memory tool names the conversation's owner, never one the
|
|
// model supplied.
|
|
func TestMemoryToolsActAsTheTurnsOwner(t *testing.T) {
|
|
m, d, remember, turn := memoryDeps(t)
|
|
ctx, err := m.actingAs(t.Context(), d, alice)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
for _, name := range []string{"memory_save", "memory_propose_shared"} {
|
|
if err := builtinOver(m, d, turn, name)(ctx); err != nil {
|
|
t.Fatalf("%s: %v", name, err)
|
|
}
|
|
}
|
|
if len(remember.remembered) != 1 || remember.remembered[0] != alice+": a fact" {
|
|
t.Fatalf("remembered %v, want alice's fact", remember.remembered)
|
|
}
|
|
if len(remember.proposed) != 1 || remember.proposed[0] != alice+": a fact" {
|
|
t.Fatalf("proposed %v, want alice's proposal", remember.proposed)
|
|
}
|
|
if err := builtinOver(m, d, turn, "memory_list")(ctx); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if len(remember.listed) != 1 || remember.listed[0] != alice {
|
|
t.Fatalf("listed for %v, want alice", remember.listed)
|
|
}
|
|
}
|
|
|
|
// TestForgetReportsAPendingRecord guards the partial outcome at the tool
|
|
// boundary: a committed deletion whose record is pending is an error, and the
|
|
// error says the note was deleted and the record is being retried. It must not
|
|
// read as a clean forget.
|
|
func TestForgetReportsAPendingRecord(t *testing.T) {
|
|
m, d, remember, turn := memoryDeps(t)
|
|
remember.forgot = true
|
|
remember.forgetErr = fmt.Errorf("%w: the record cannot be written", memory.ErrRecordIncomplete)
|
|
ctx, err := m.actingAs(t.Context(), d, alice)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
var out string
|
|
for _, tool := range m.builtins(d, turn) {
|
|
if tool.tool.Name != "memory_forget" {
|
|
continue
|
|
}
|
|
text, err := tool.tool.Execute(ctx, `{"id":"`+uuid.New().String()+`"}`)
|
|
if err == nil {
|
|
t.Fatalf("forget of a pending record returned %q and no error", text)
|
|
}
|
|
out = err.Error()
|
|
}
|
|
if !strings.Contains(out, "the note was deleted") || !strings.Contains(out, "pending") || !strings.Contains(out, "retried") {
|
|
t.Fatalf("forget failed with %q, want the deletion and the pending record", out)
|
|
}
|
|
if strings.Contains(out, "forgotten") {
|
|
t.Fatalf("a pending record reported %q as a clean forget", out)
|
|
}
|
|
}
|
|
|
|
// TestTaintedConversationWritesNoMemory guards requirement 6: a conversation
|
|
// that has read untrusted content cannot write memory, and a fresh conversation
|
|
// can. Taint tightens, so nothing about the refusal depends on the model.
|
|
func TestTaintedConversationWritesNoMemory(t *testing.T) {
|
|
m, d, remember, turn := memoryDeps(t)
|
|
ctx, err := m.actingAs(t.Context(), d, alice)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := m.store.Taint(ctx, m.orgID, alice, turn.SessionID); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
for _, name := range []string{"memory_save", "memory_propose_shared"} {
|
|
if err := builtinOver(m, d, turn, name)(ctx); err == nil {
|
|
t.Fatalf("%s wrote memory in a tainted conversation", name)
|
|
}
|
|
}
|
|
if len(remember.remembered) != 0 || len(remember.proposed) != 0 {
|
|
t.Fatalf("wrote %v and %v in a tainted conversation", remember.remembered, remember.proposed)
|
|
}
|
|
}
|
|
|
|
// builtinOver runs one builtin of turn with a note as its argument.
|
|
func builtinOver(m *Module, d Deps, turn store.Turn, name string) func(context.Context) error {
|
|
return func(ctx context.Context) error {
|
|
for _, tool := range m.builtins(d, turn) {
|
|
if tool.tool.Name == name {
|
|
_, err := tool.tool.Execute(ctx, `{"text":"a fact"}`)
|
|
return err
|
|
}
|
|
}
|
|
return errors.New("no tool " + name)
|
|
}
|
|
}
|