diff --git a/internal/app/archied/bootstrap.go b/internal/app/archied/bootstrap.go index 11b525b1f..3c0a67803 100644 --- a/internal/app/archied/bootstrap.go +++ b/internal/app/archied/bootstrap.go @@ -690,6 +690,7 @@ func (b *boot) setupGateways(ctx context.Context, cfgPath, overlayPath string) b ChannelManager: b.channelManager, AgentStatus: b.agentStatus, RateLimiter: b.rateLimiter, MemoryEngine: b.memoryStore(), + MemoryWriter: b.memoryWriter(), }) if !ok { return false @@ -1157,21 +1158,40 @@ func (b *boot) setupMemoryAll() error { return b.setupMemoryEngine() } -// memoryStore resolves the active memory engine (the same one -// setupMemoryEngine registered under cfg.Memory.Engine) as the narrow read -// surface a chat turn runner needs. Nil when the registry was never set up -// or the configured engine is not registered -- both cases the turn runner -// already treats as "no memory block" (gateway.renderMemory), so a chat -// turn degrades instead of failing. -func (b *boot) memoryStore() gateway.MemoryStore { +// activeMemoryEngine resolves the engine setupMemoryEngine registered under +// cfg.Memory.Engine. Both memoryStore and memoryWriter narrow this same +// engine to the read or write surface their caller needs; ok is false when +// the registry was never set up or the configured engine is not registered. +func (b *boot) activeMemoryEngine() (domainmemory.MemoryEngine, bool) { if b.memEngines == nil { - return nil + return nil, false } name := b.cfg.Memory.Engine if name == "" { name = infraMemory.EngineName } - engine, ok := b.memEngines.Get(name) + return b.memEngines.Get(name) +} + +// memoryStore resolves the active memory engine as the narrow read surface +// a chat turn runner needs. Nil when activeMemoryEngine has none -- the +// turn runner already treats that as "no memory block" +// (gateway.renderMemory), so a chat turn degrades instead of failing. +func (b *boot) memoryStore() gateway.MemoryStore { + engine, ok := b.activeMemoryEngine() + if !ok { + return nil + } + return engine +} + +// memoryWriter resolves the active memory engine as the narrow write +// surface the per-turn memory tool needs (docs/prds/ +// memory-engine-unification.md §5). Nil when activeMemoryEngine has none -- +// MemoryTools already treats that as "no memory tools", so a chat turn +// simply has no memory_create/update/delete/list tools rather than failing. +func (b *boot) memoryWriter() gateway.MemoryWriteStore { + engine, ok := b.activeMemoryEngine() if !ok { return nil } diff --git a/internal/app/archied/gateway_runtime.go b/internal/app/archied/gateway_runtime.go index cb0260799..df7a5261a 100644 --- a/internal/app/archied/gateway_runtime.go +++ b/internal/app/archied/gateway_runtime.go @@ -68,6 +68,7 @@ func (b *boot) setupGatewayChat(ctx context.Context, actor gateway.ChatTaskActor DefaultChatIdentity: b.defaultChatIdentity, SessionStore: b.chatSessionStore, Bus: b.bus, Log: b.log, Secrets: b.secrets, MemoryEngine: b.memoryStore(), + MemoryWriter: b.memoryWriter(), } router.LLM, router.LLMStream = makeChatLLMResponder(ctx, "web", setup, b.chatSessionStore, router) router.Titles = newChatTitleGenerator(setup) diff --git a/internal/app/archied/telegram_setup.go b/internal/app/archied/telegram_setup.go index 481ddc62f..003aac19a 100644 --- a/internal/app/archied/telegram_setup.go +++ b/internal/app/archied/telegram_setup.go @@ -72,6 +72,10 @@ type telegramSetup struct { // engine, resolved by cfg.Memory.Engine). Nil disables the chat // block for every turn runner built from this setup. MemoryEngine gateway.MemoryStore + // MemoryWriter is the same active engine's write surface, used to build + // the per-turn memory_create/update/delete/list tools (docs/prds/ + // memory-engine-unification.md §5). Nil omits those tools. + MemoryWriter gateway.MemoryWriteStore } // telegramValidateConfigMap builds the map Gateway.ValidateConfig expects @@ -360,6 +364,7 @@ func newChatTurnRunner( Workspace: cfg.Chat.Workspace, Repos: chatRepoEnv(cfg, s.DefaultChatIdentity), MemoryEngine: s.MemoryEngine, + MemoryWriter: s.MemoryWriter, UserIdentity: userIdentityResolver(channel), Log: s.Log, }) diff --git a/internal/gateway/turn.go b/internal/gateway/turn.go index af22bdab0..8b3b76ba1 100644 --- a/internal/gateway/turn.go +++ b/internal/gateway/turn.go @@ -104,6 +104,11 @@ type TurnRunnerConfig struct { // in prepareTurn. Nil disables the block entirely (renderMemory // treats a nil store the same as a failed read). MemoryEngine MemoryStore + // MemoryWriter is the write surface the per-turn memory tool (slice 4) + // uses to build memory_create/memory_update/memory_delete/memory_list. + // Nil omits those tools entirely (MemoryTools treats a nil store the + // same as a subject with no writable scope: no tools registered). + MemoryWriter MemoryWriteStore // UserIdentity resolves the initiating user's identity for one inbound // message, per channel. False (or a nil UserIdentity) means the channel // carries no per-person identity -- the dashboard's one bearer token, or a @@ -298,6 +303,7 @@ type preparedTurn struct { // for one turn's generation call. func (r *TurnRunner) prepareTurn(ctx context.Context, sessionID string, in Inbound, history []messaging.Message) (preparedTurn, error) { compressed := compressTurnHistory(history) + subject := r.resolveSubject(in.Message) extraTools := append( TaskTools(r.TaskLister, r.Tasks, r.TaskLogs, r.TaskActor, r.TaskIdentity), SessionTools(r.Sessions, r.Router.SessionTracker(), r.Channel, in.Message)..., @@ -306,6 +312,7 @@ func (r *TurnRunner) prepareTurn(ctx context.Context, sessionID string, in Inbou // The dashboard tools (page_index, dashboard_navigate) belong to the web // UI only: a non-web channel has no dashboard to point at. extraTools = append(extraTools, PageIndexTools(r.Channel)...) + extraTools = append(extraTools, MemoryTools(r.MemoryWriter, subject)...) modelName := r.Models.ActiveModel() modelDetails := ModelDetails{} if detailed, ok := r.Models.(DetailedModelManager); ok { @@ -326,7 +333,7 @@ func (r *TurnRunner) prepareTurn(ctx context.Context, sessionID string, in Inbou if r.Personas != nil { persona = r.Personas.GetActive(sessionID) } - memoryBlock := renderMemory(ctx, r.MemoryEngine, r.resolveSubject(in.Message), r.Log) + memoryBlock := renderMemory(ctx, r.MemoryEngine, subject, r.Log) systemPrompt := BuildSystemPrompt(SystemPromptConfig{ Persona: persona, Tools: prepared.ToolSummaries(), diff --git a/internal/gateway/turn_memory_tool.go b/internal/gateway/turn_memory_tool.go new file mode 100644 index 000000000..71082e5e6 --- /dev/null +++ b/internal/gateway/turn_memory_tool.go @@ -0,0 +1,260 @@ +package gateway + +import ( + "context" + "fmt" + "strings" + + domainmemory "github.com/samcharles93/archie-core/internal/domain/memory" + "github.com/samcharles93/archie-core/internal/tools" +) + +// MemoryWriteStore is the write surface the per-turn memory tool needs: +// Create/Update/Forget/List, the four operations docs/prds/ +// memory-engine-unification.md §5 maps the tool's create/update/delete/list +// actions onto. Narrowed from domainmemory.Store (which also carries Get, +// Query and Revisions) so this package's write path cannot accidentally +// reach for read operations the tool does not expose. +type MemoryWriteStore interface { + Create(ctx context.Context, in domainmemory.NewRecord) (domainmemory.Record, error) + Update(ctx context.Context, in domainmemory.RecordUpdate) (domainmemory.Record, error) + Forget(ctx context.Context, scope domainmemory.Scope, id domainmemory.RecordID) error + List(ctx context.Context, scope domainmemory.Scope) ([]domainmemory.Record, error) +} + +// memoryToolAuthor and memoryToolSource are the provenance the memory tool +// stamps on every record it writes, matching NewRecord/RecordUpdate's +// "free text, the engine retains it, it does not interpret it" contract. +const ( + memoryToolAuthor = "chat" + memoryToolSource = "memory tool" +) + +// MemoryRecordSummary is the per-record view the memory tool returns to the +// model: enough to act on (id, scope, content, revision) without exposing +// engine-internal fields. +type MemoryRecordSummary struct { + ID string `json:"id"` + Scope string `json:"scope"` + Kind string `json:"kind"` + Content string `json:"content"` + Revision int `json:"revision"` +} + +func recordSummary(r domainmemory.Record) MemoryRecordSummary { + return MemoryRecordSummary{ + ID: string(r.ID), Scope: string(r.Scope.Kind), Kind: r.Kind, + Content: r.Content, Revision: r.Revision, + } +} + +// MemoryTools builds the chat tools that let the model manage its own +// durable memory, scoped to one turn's resolved Subject +// (docs/prds/memory-engine-unification.md §5). +// +// Built per turn, not registered once at boot, because the writable scope +// set is a function of the turn's Subject: two different users talking to +// the same agent get two different sets of tools, each only able to +// address that user's own scopes. +// +// The model chooses the scope KIND (an enum built from +// subject.WritableScopes(), which never includes global); it never chooses +// the agent or user id a scope resolves to -- there is no schema field for +// either, so even a model that tries to emit another user's id has nowhere +// to write it. A nil store, or a subject with no writable scope at all +// (no resolvable agent and no resolvable user), omits every tool rather +// than registering ones that always fail. +func MemoryTools(store MemoryWriteStore, subject domainmemory.Subject) []tools.ToolEntry { + if store == nil { + return nil + } + writable := subject.WritableScopes() + if len(writable) == 0 { + return nil + } + kinds := make([]string, 0, len(writable)) + for _, s := range writable { + kinds = append(kinds, string(s.Kind)) + } + resolve := func(kind string) (domainmemory.Scope, error) { + for _, s := range writable { + if string(s.Kind) == kind { + return s, nil + } + } + return domainmemory.Scope{}, fmt.Errorf("memory: scope %q is not writable for this conversation (available: %s)", + kind, strings.Join(kinds, ", ")) + } + return []tools.ToolEntry{ + memoryCreateTool(store, resolve, kinds), + memoryUpdateTool(store, resolve, kinds), + memoryDeleteTool(store, resolve, kinds), + memoryListTool(store, resolve, kinds), + } +} + +// memoryScopeSchemaProperty is the schema fragment every memory tool shares +// for its scope argument: an enum of the kinds this turn's subject may +// write to. It carries no agent or user field -- resolve (built from the +// turn's Subject) supplies those, never the model. +func memoryScopeSchemaProperty(kinds []string) map[string]any { + enum := make([]any, len(kinds)) + for i, k := range kinds { + enum[i] = k + } + return map[string]any{ + "type": "string", + "enum": enum, + "description": "Which memory scope to use. This names a KIND only -- the agent and user " + + "this scope belongs to are resolved from the current conversation, never from this call.", + } +} + +func memoryCreateTool(store MemoryWriteStore, resolve func(string) (domainmemory.Scope, error), kinds []string) tools.ToolEntry { + return tools.ToolEntry{ + Name: "memory_create", + Toolset: "memory", + Description: "Save a new durable memory record. Use this to remember a fact, preference, " + + "or piece of context that should persist across conversations.", + Classification: tools.ClassMutating, + Schema: tools.JSONSchema{ + "type": "object", + "properties": map[string]any{ + "scope": memoryScopeSchemaProperty(kinds), + "content": map[string]any{"type": "string", "description": "The memory to record."}, + "kind": map[string]any{ + "type": "string", + "description": "Free-text category for the record (e.g. \"note\", \"preference\"). Defaults to \"note\".", + }, + }, + "required": []any{"scope", "content"}, + }, + Handler: func(ctx context.Context, input map[string]any) (any, error) { + scope, err := resolve(asString(input["scope"])) + if err != nil { + return nil, err + } + content := strings.TrimSpace(asString(input["content"])) + if content == "" { + return nil, fmt.Errorf("memory_create: content is required") + } + kind := strings.TrimSpace(asString(input["kind"])) + if kind == "" { + kind = "note" + } + rec, err := store.Create(ctx, domainmemory.NewRecord{ + Scope: scope, Kind: kind, Content: content, + Author: memoryToolAuthor, OriginUser: scope.User, Source: memoryToolSource, + }) + if err != nil { + return nil, fmt.Errorf("memory_create: %w", err) + } + return recordSummary(rec), nil + }, + } +} + +func memoryUpdateTool(store MemoryWriteStore, resolve func(string) (domainmemory.Scope, error), kinds []string) tools.ToolEntry { + return tools.ToolEntry{ + Name: "memory_update", + Toolset: "memory", + Description: "Replace the content of an existing memory record, addressed by the id shown " + + "in the block. The prior content remains available as a revision.", + Classification: tools.ClassMutating, + Schema: tools.JSONSchema{ + "type": "object", + "properties": map[string]any{ + "scope": memoryScopeSchemaProperty(kinds), + "id": map[string]any{"type": "string", "description": "The record id from the block."}, + "content": map[string]any{"type": "string", "description": "The new content, replacing the old."}, + }, + "required": []any{"scope", "id", "content"}, + }, + Handler: func(ctx context.Context, input map[string]any) (any, error) { + scope, err := resolve(asString(input["scope"])) + if err != nil { + return nil, err + } + id := strings.TrimSpace(asString(input["id"])) + if id == "" { + return nil, fmt.Errorf("memory_update: id is required") + } + content := strings.TrimSpace(asString(input["content"])) + if content == "" { + return nil, fmt.Errorf("memory_update: content is required") + } + rec, err := store.Update(ctx, domainmemory.RecordUpdate{ + Scope: scope, ID: domainmemory.RecordID(id), Content: content, + Author: memoryToolAuthor, Source: memoryToolSource, + }) + if err != nil { + return nil, fmt.Errorf("memory_update: %w", err) + } + return recordSummary(rec), nil + }, + } +} + +func memoryDeleteTool(store MemoryWriteStore, resolve func(string) (domainmemory.Scope, error), kinds []string) tools.ToolEntry { + return tools.ToolEntry{ + Name: "memory_delete", + Toolset: "memory", + Description: "Forget a memory record, addressed by the id shown in the block. " + + "Its content is removed from future prompts; its provenance is retained as a revision.", + Classification: tools.ClassMutating, + Schema: tools.JSONSchema{ + "type": "object", + "properties": map[string]any{ + "scope": memoryScopeSchemaProperty(kinds), + "id": map[string]any{"type": "string", "description": "The record id from the block."}, + }, + "required": []any{"scope", "id"}, + }, + Handler: func(ctx context.Context, input map[string]any) (any, error) { + scope, err := resolve(asString(input["scope"])) + if err != nil { + return nil, err + } + id := strings.TrimSpace(asString(input["id"])) + if id == "" { + return nil, fmt.Errorf("memory_delete: id is required") + } + if err := store.Forget(ctx, scope, domainmemory.RecordID(id)); err != nil { + return nil, fmt.Errorf("memory_delete: %w", err) + } + return map[string]any{"id": id, "scope": string(scope.Kind), "message": "forgotten"}, nil + }, + } +} + +func memoryListTool(store MemoryWriteStore, resolve func(string) (domainmemory.Scope, error), kinds []string) tools.ToolEntry { + return tools.ToolEntry{ + Name: "memory_list", + Toolset: "memory", + Description: "List the live memory records in one writable scope. Use this to find a " + + "record's id before calling memory_update or memory_delete.", + Classification: tools.ClassIdempotent, + Schema: tools.JSONSchema{ + "type": "object", + "properties": map[string]any{ + "scope": memoryScopeSchemaProperty(kinds), + }, + "required": []any{"scope"}, + }, + Handler: func(ctx context.Context, input map[string]any) (any, error) { + scope, err := resolve(asString(input["scope"])) + if err != nil { + return nil, err + } + records, err := store.List(ctx, scope) + if err != nil { + return nil, fmt.Errorf("memory_list: %w", err) + } + summaries := make([]MemoryRecordSummary, 0, len(records)) + for _, r := range records { + summaries = append(summaries, recordSummary(r)) + } + return map[string]any{"records": summaries}, nil + }, + } +} diff --git a/internal/gateway/turn_memory_tool_test.go b/internal/gateway/turn_memory_tool_test.go new file mode 100644 index 000000000..128ed21cf --- /dev/null +++ b/internal/gateway/turn_memory_tool_test.go @@ -0,0 +1,364 @@ +package gateway + +import ( + "context" + "log/slog" + "strings" + "testing" + + domainmemory "github.com/samcharles93/archie-core/internal/domain/memory" + infraMemory "github.com/samcharles93/archie-core/internal/infrastructure/memory" + "github.com/samcharles93/archie-core/internal/tools" +) + +// findTool returns the named entry from a tool slice, failing the test if +// it is missing. +func findTool(t *testing.T, entries []tools.ToolEntry, name string) tools.ToolEntry { + t.Helper() + for _, e := range entries { + if e.Name == name { + return e + } + } + t.Fatalf("tool %q not found among %d entries", name, len(entries)) + return tools.ToolEntry{} +} + +// schemaEnum extracts the "enum" list of a scope property as a string set. +func schemaEnum(t *testing.T, entry tools.ToolEntry) []string { + t.Helper() + props, ok := entry.Schema["properties"].(map[string]any) + if !ok { + t.Fatalf("tool %q schema has no properties", entry.Name) + } + scope, ok := props["scope"].(map[string]any) + if !ok { + t.Fatalf("tool %q schema has no scope property", entry.Name) + } + enum, ok := scope["enum"].([]any) + if !ok { + t.Fatalf("tool %q scope property has no enum", entry.Name) + } + out := make([]string, len(enum)) + for i, v := range enum { + s, ok := v.(string) + if !ok { + t.Fatalf("enum entry %v is not a string", v) + } + out[i] = s + } + return out +} + +// asRecordSummary checks and unwraps a tool handler's MemoryRecordSummary +// output, failing the test on any unexpected shape. +func asRecordSummary(t *testing.T, out any) MemoryRecordSummary { + t.Helper() + rec, ok := out.(MemoryRecordSummary) + if !ok { + t.Fatalf("output type = %T, want MemoryRecordSummary", out) + } + return rec +} + +// listRecords checks and unwraps memory_list's output shape, failing the +// test on any unexpected shape. +func listRecords(t *testing.T, out any) []MemoryRecordSummary { + t.Helper() + result, ok := out.(map[string]any) + if !ok { + t.Fatalf("memory_list output type = %T, want map[string]any", out) + } + records, ok := result["records"].([]MemoryRecordSummary) + if !ok { + t.Fatalf("memory_list \"records\" type = %T, want []MemoryRecordSummary", result["records"]) + } + return records +} + +// TestMemoryToolsNilStoreOmitsEverything guards the composition case where +// no memory engine is registered: a nil store must not panic MemoryTools +// and must offer nothing, matching the read path's degrade-to-nothing +// contract. +func TestMemoryToolsNilStoreOmitsEverything(t *testing.T) { + got := MemoryTools(nil, domainmemory.Subject{AgentID: "archie", UserID: "alice"}) + if got != nil { + t.Fatalf("MemoryTools(nil store) = %d tools, want none", len(got)) + } +} + +// TestMemoryToolsNoWritableScopeOmitsEverything is the structural half of +// acceptance criterion 7: a subject with no agent and no user id (nothing +// writable) gets no memory tools at all, not tools that always error. +func TestMemoryToolsNoWritableScopeOmitsEverything(t *testing.T) { + engine := infraMemory.NewBuiltinEngine(t.TempDir(), 0) + got := MemoryTools(engine, domainmemory.Subject{}) + if got != nil { + t.Fatalf("MemoryTools(no writable scope) = %d tools, want none", len(got)) + } +} + +// TestMemoryToolsScopeEnumNeverIncludesGlobal is acceptance criterion 9's +// global half: global is never model-writable, so it must never appear in +// any memory tool's scope enum, regardless of subject. +func TestMemoryToolsScopeEnumNeverIncludesGlobal(t *testing.T) { + engine := infraMemory.NewBuiltinEngine(t.TempDir(), 0) + entries := MemoryTools(engine, domainmemory.Subject{AgentID: "archie", UserID: "alice"}) + for _, e := range entries { + for _, kind := range schemaEnum(t, e) { + if kind == string(domainmemory.ScopeGlobal) { + t.Fatalf("tool %q offers global in its scope enum", e.Name) + } + } + } +} + +// TestMemoryToolsScopeEnumMatchesWritableScopes: with no resolvable user +// (agent scope only), the tools must offer exactly "agent", never "user" or +// "agent-user" -- the structural form of fail-closed. +func TestMemoryToolsScopeEnumMatchesWritableScopes(t *testing.T) { + engine := infraMemory.NewBuiltinEngine(t.TempDir(), 0) + entries := MemoryTools(engine, domainmemory.Subject{AgentID: "archie"}) + create := findTool(t, entries, "memory_create") + enum := schemaEnum(t, create) + if len(enum) != 1 || enum[0] != string(domainmemory.ScopeAgent) { + t.Fatalf("scope enum = %v, want exactly [agent]", enum) + } +} + +// TestMemoryToolsSchemaHasNoIdentityField is acceptance criterion 9: the +// tool schemas structurally carry no field the model could put an agent or +// user id into. Only "scope" (a kind enum), "id", "content" and "kind" are +// permitted. +func TestMemoryToolsSchemaHasNoIdentityField(t *testing.T) { + engine := infraMemory.NewBuiltinEngine(t.TempDir(), 0) + entries := MemoryTools(engine, domainmemory.Subject{AgentID: "archie", UserID: "alice"}) + allowed := map[string]bool{"scope": true, "id": true, "content": true, "kind": true} + for _, e := range entries { + props, ok := e.Schema["properties"].(map[string]any) + if !ok { + t.Fatalf("tool %q schema has no properties", e.Name) + } + for k := range props { + if !allowed[k] { + t.Fatalf("tool %q schema exposes unexpected field %q -- possible identity leak", e.Name, k) + } + if strings.Contains(strings.ToLower(k), "agent") || strings.Contains(strings.ToLower(k), "user") || + strings.Contains(strings.ToLower(k), "owner") { + t.Fatalf("tool %q schema field %q looks like an identity field", e.Name, k) + } + } + } +} + +// TestMemoryToolsFailClosedOnDisallowedScope is acceptance criterion 7's +// tool-layer half: a handler call naming a scope kind the subject cannot +// write to (bypassing the schema enum, as a model that ignores the schema +// might) is refused with a clear reason, never silently widened. +func TestMemoryToolsFailClosedOnDisallowedScope(t *testing.T) { + engine := infraMemory.NewBuiltinEngine(t.TempDir(), 0) + entries := MemoryTools(engine, domainmemory.Subject{AgentID: "archie"}) // no user id + create := findTool(t, entries, "memory_create") + + _, err := create.Handler(context.Background(), map[string]any{ + "scope": "user", "content": "should never be written", + }) + if err == nil { + t.Fatal("memory_create with disallowed scope kind succeeded, want an error") + } + if !strings.Contains(err.Error(), "not writable") { + t.Fatalf("error = %v, want a clear \"not writable\" reason", err) + } +} + +// TestMemoryToolsCreateUpdateDeleteRoundTrip drives the four handlers +// directly against a real engine and asserts each engine-level effect: +// create is retrievable via list, update supersedes content and is visible +// in Revisions with author and time, delete removes it from list while +// Revisions retains the deleted state. This is acceptance criterion 5 at +// the tool/engine boundary. +func TestMemoryToolsCreateUpdateDeleteRoundTrip(t *testing.T) { + ctx := context.Background() + engine := infraMemory.NewBuiltinEngine(t.TempDir(), 0) + subject := domainmemory.Subject{AgentID: "archie", UserID: "alice"} + entries := MemoryTools(engine, subject) + create := findTool(t, entries, "memory_create") + update := findTool(t, entries, "memory_update") + del := findTool(t, entries, "memory_delete") + list := findTool(t, entries, "memory_list") + + createOut, err := create.Handler(ctx, map[string]any{ + "scope": "agent-user", "content": "alice prefers dark roast coffee", + }) + if err != nil { + t.Fatalf("memory_create: %v", err) + } + created := asRecordSummary(t, createOut) + if created.ID == "" { + t.Fatal("memory_create returned an empty id") + } + + listOut, err := list.Handler(ctx, map[string]any{"scope": "agent-user"}) + if err != nil { + t.Fatalf("memory_list: %v", err) + } + records := listRecords(t, listOut) + if len(records) != 1 || records[0].Content != "alice prefers dark roast coffee" { + t.Fatalf("memory_list after create = %+v, want the planted record", records) + } + + updateOut, err := update.Handler(ctx, map[string]any{ + "scope": "agent-user", "id": created.ID, "content": "alice prefers oat milk lattes", + }) + if err != nil { + t.Fatalf("memory_update: %v", err) + } + updated := asRecordSummary(t, updateOut) + if updated.Content != "alice prefers oat milk lattes" { + t.Fatalf("memory_update content = %q, want the new content", updated.Content) + } + + scope := domainmemory.Scope{Kind: domainmemory.ScopeAgentUser, Agent: "archie", User: "alice"} + revisions, err := engine.Revisions(ctx, scope, domainmemory.RecordID(created.ID)) + if err != nil { + t.Fatalf("Revisions() error = %v", err) + } + found := false + for _, rev := range revisions { + if rev.Record.Content == "alice prefers dark roast coffee" { + found = true + if rev.Record.Author != memoryToolAuthor { + t.Fatalf("superseded revision author = %q, want %q", rev.Record.Author, memoryToolAuthor) + } + if rev.SupersededAt.IsZero() { + t.Fatal("superseded revision has a zero SupersededAt") + } + } + } + if !found { + t.Fatalf("Revisions() = %+v, missing the pre-update content", revisions) + } + + if _, err := del.Handler(ctx, map[string]any{"scope": "agent-user", "id": created.ID}); err != nil { + t.Fatalf("memory_delete: %v", err) + } + listOut2, err := list.Handler(ctx, map[string]any{"scope": "agent-user"}) + if err != nil { + t.Fatalf("memory_list after delete: %v", err) + } + records2 := listRecords(t, listOut2) + if len(records2) != 0 { + t.Fatalf("memory_list after delete = %+v, want empty", records2) + } + revisionsAfterDelete, err := engine.Revisions(ctx, scope, domainmemory.RecordID(created.ID)) + if err != nil { + t.Fatalf("Revisions() after delete error = %v", err) + } + deletedFound := false + for _, rev := range revisionsAfterDelete { + if rev.Deleted { + deletedFound = true + } + } + if !deletedFound { + t.Fatal("Revisions() after delete has no deleted marker; provenance was not retained") + } +} + +// TestTurnMemoryWriteRoundTripVisibleNextTurn drives the round trip through +// the actual turn path: prepareTurn builds the memory tools for turn 1, the +// test calls memory_create the way a dispatched tool call would, and turn +// 2's prompt shows the new record by id. This is acceptance criterion 5 at +// the turn boundary, and also confirms the write path inherits the read +// path's Subject rather than re-deriving identity a second way. +func TestTurnMemoryWriteRoundTripVisibleNextTurn(t *testing.T) { + ctx := context.Background() + engine := infraMemory.NewBuiltinEngine(t.TempDir(), 0) + store := NewSessionStoreMemory() + router := NewRouter(nil, nil, "telegram") + router.Identity = "archie" + router.InitSessions(store) + prepared := &turnTestPreparedModel{reply: "ok"} + model := &turnTestModel{prepared: prepared} + runner := NewTurnRunner(TurnRunnerConfig{ + Router: router, + Sessions: store, + Models: &fakeModelManager{models: []string{"test/model"}, activeModel: "test/model"}, + Personas: NewPersonaRegistry(DefaultPersonas()), + Model: model, + BotUser: "archie", + Channel: "telegram", + Log: slog.New(slog.DiscardHandler), + MemoryEngine: engine, + MemoryWriter: engine, + UserIdentity: telegramIdentity, + }) + + msg := chatMsg("s1", "chat-1", "alice", "remember I like dark roast") + if _, err := runner.Run(ctx, msg, nil); err != nil { + t.Fatalf("Run() turn 1 error = %v", err) + } + + create := findTool(t, model.gotExtra, "memory_create") + out, err := create.Handler(ctx, map[string]any{ + "scope": "agent-user", "content": "alice prefers dark roast coffee", + }) + if err != nil { + t.Fatalf("memory_create: %v", err) + } + created := asRecordSummary(t, out) + + if _, err := runner.Run(ctx, chatMsg("s2", "chat-1", "alice", "hi again"), nil); err != nil { + t.Fatalf("Run() turn 2 error = %v", err) + } + prompt := lastSystemPrompt(prepared) + if !strings.Contains(prompt, "alice prefers dark roast coffee") { + t.Fatalf("turn 2 prompt missing the record written in turn 1, prompt = %q", prompt) + } + if !strings.Contains(prompt, created.ID) { + t.Fatalf("turn 2 prompt missing the record's id %q, prompt = %q", created.ID, prompt) + } +} + +// TestTurnMemoryWriteNoResolvableUserOmitsUserTools is acceptance criterion +// 7 at the turn boundary: a channel with no resolvable user (nil +// UserIdentity, the dashboard/webhook shape) gets a memory_create tool that +// only offers agent scope, never user or agent-user. +func TestTurnMemoryWriteNoResolvableUserOmitsUserTools(t *testing.T) { + ctx := context.Background() + engine := infraMemory.NewBuiltinEngine(t.TempDir(), 0) + store := NewSessionStoreMemory() + router := NewRouter(nil, nil, "web") + router.Identity = "archie" + router.InitSessions(store) + prepared := &turnTestPreparedModel{reply: "ok"} + model := &turnTestModel{prepared: prepared} + runner := NewTurnRunner(TurnRunnerConfig{ + Router: router, + Sessions: store, + Models: &fakeModelManager{models: []string{"test/model"}, activeModel: "test/model"}, + Personas: NewPersonaRegistry(DefaultPersonas()), + Model: model, + BotUser: "archie", + Channel: "web", + Log: slog.New(slog.DiscardHandler), + MemoryEngine: engine, + MemoryWriter: engine, + UserIdentity: nil, + }) + + if _, err := runner.Run(ctx, chatMsg("s1", "chat-1", "webhook-route", "hi"), nil); err != nil { + t.Fatalf("Run() error = %v", err) + } + create := findTool(t, model.gotExtra, "memory_create") + for _, kind := range schemaEnum(t, create) { + if kind == string(domainmemory.ScopeUser) || kind == string(domainmemory.ScopeAgentUser) { + t.Fatalf("resolverless channel's memory_create offers scope %q, want agent only", kind) + } + } + + _, err := create.Handler(ctx, map[string]any{"scope": "user", "content": "should never write"}) + if err == nil { + t.Fatal("memory_create with scope=user on a resolverless channel succeeded, want an error") + } +}