diff --git a/.changeset/call-only-om-saves.md b/.changeset/call-only-om-saves.md new file mode 100644 index 00000000000..0a9c57b2cad --- /dev/null +++ b/.changeset/call-only-om-saves.md @@ -0,0 +1,5 @@ +--- +'@mastra/memory': patch +--- + +Fixed agent turns being lost when `observationalMemory` is enabled only in a call's `memory.options` and not on the `Memory` instance. Observational memory runs only when it is configured on the instance, so a call-only opt-in now falls back to normal message history instead of skipping the save, and Memory logs a one-time warning that the opt-in was ignored. Per-call opt-outs are unchanged. Before, the regular agent saved nothing for the turn and durable agents saved only the user message. diff --git a/.changeset/durable-om-merged-config.md b/.changeset/durable-om-merged-config.md new file mode 100644 index 00000000000..6601faaa2db --- /dev/null +++ b/.changeset/durable-om-merged-config.md @@ -0,0 +1,5 @@ +--- +'@mastra/core': patch +--- + +Durable agents no longer treat a call-level `observationalMemory` opt-in as active when the Memory instance does not run observational memory, so such calls save their messages and generate the thread title like any other run. diff --git a/packages/core/src/agent/durable/preparation.ts b/packages/core/src/agent/durable/preparation.ts index 0d131753af7..00af9b2ce3d 100644 --- a/packages/core/src/agent/durable/preparation.ts +++ b/packages/core/src/agent/durable/preparation.ts @@ -843,7 +843,14 @@ export async function prepareForDurableExecution( // 12. Resolve memory persistence flags const savePerStep = execOptions?.savePerStep; - const observationalMemory = isObservationalMemoryEnabled(memoryConfig?.observationalMemory); + // A call-level OM opt-in counts only if it is still on after merging with the Memory instance's + // config; otherwise the run saves through finalize like any other. Instance-only OM (#28886) still + // reads as off here; durable relies on the finalize flush for output-processor edits. + const observationalMemory = + isObservationalMemoryEnabled(memoryConfig?.observationalMemory) && + isObservationalMemoryEnabled( + (memory ? memory.getMergedThreadConfig(memoryConfig) : memoryConfig)?.observationalMemory, + ); if (execOptions.isTaskComplete) await loadDefaultOptions(); // 12b. Open MODEL_GENERATION under the AGENT_RUN opened in step 6, and export both diff --git a/packages/core/src/memory/types.ts b/packages/core/src/memory/types.ts index cdd4d4f335f..13f5f271600 100644 --- a/packages/core/src/memory/types.ts +++ b/packages/core/src/memory/types.ts @@ -1111,6 +1111,10 @@ type BaseMemoryConfig = { * * Set to `true` to enable with defaults, `false` to disable, or an object to customize. * + * Observational memory must be configured on the `Memory` instance. A per-call value in + * `memory.options` can turn it off for that call, but cannot turn it on for an instance that + * does not configure it; such a call falls back to message history. + * * @example * ```typescript * // Enable with defaults diff --git a/packages/memory/integration-tests/src/durable-agent-call-only-om.test.ts b/packages/memory/integration-tests/src/durable-agent-call-only-om.test.ts new file mode 100644 index 00000000000..61161f3fa39 --- /dev/null +++ b/packages/memory/integration-tests/src/durable-agent-call-only-om.test.ts @@ -0,0 +1,118 @@ +/** + * Observational memory turned on only in the call's `memory.options` (not on `new Memory`). + * + * The OM engine is built from the Memory instance config, so a call-only OM setting has no + * engine to save the turn. Every engine must still store the turn once, through the normal + * save path (durable: finalize flush + title), while instance-level OM keeps observing. + */ +import { randomUUID } from 'node:crypto'; +import { MockLanguageModelV2, convertArrayToReadableStream } from '@internal/ai-sdk-v5/test'; +import { Agent } from '@mastra/core/agent'; +import { createDurableAgent } from '@mastra/core/agent/durable'; +import { Mastra } from '@mastra/core/mastra'; +import { LibSQLStore } from '@mastra/libsql'; +import { Memory } from '@mastra/memory'; +import { describe, expect, it } from 'vitest'; +import { persistedMessages, streamTurn } from './durable-harness-helpers'; + +const REPLY = 'Lisbon is sunny.'; +const TITLE = 'Lisbon weather'; + +function mockModel(text: string, calls?: { count: number }) { + return new MockLanguageModelV2({ + doGenerate: async () => { + if (calls) calls.count++; + return { + content: [{ type: 'text', text }], + finishReason: 'stop', + usage: { inputTokens: 5, outputTokens: 5, totalTokens: 10 }, + warnings: [], + }; + }, + doStream: async () => { + if (calls) calls.count++; + return { + rawCall: { rawPrompt: null, rawSettings: {} }, + warnings: [], + stream: convertArrayToReadableStream([ + { type: 'stream-start', warnings: [] }, + { type: 'response-metadata', id: 'r1', modelId: 'mock', timestamp: new Date(0) }, + { type: 'text-start', id: 't1' }, + { type: 'text-delta', id: 't1', delta: text }, + { type: 'text-end', id: 't1' }, + { type: 'finish', finishReason: 'stop', usage: { inputTokens: 5, outputTokens: 5, totalTokens: 10 } }, + ]), + }; + }, + }); +} + +const CASES = { + 'regular Agent, OM on call only': { engine: 'plain', instanceOM: false, callOM: true }, + 'regular Agent, OM on Memory and call': { engine: 'plain', instanceOM: true, callOM: true }, + 'DurableAgent, OM on call only': { engine: 'durable', instanceOM: false, callOM: true }, + 'DurableAgent, OM on Memory and call': { engine: 'durable', instanceOM: true, callOM: true }, + 'DurableAgent, OM on Memory only': { engine: 'durable', instanceOM: true, callOM: false }, +} as const; + +describe('call-only observational memory', () => { + it.each(Object.entries(CASES))('%s stores the turn once', async (_name, c) => { + const observerCalls = { count: 0 }; + const omOptions = { + observationalMemory: { + enabled: true, + scope: 'thread', + observation: { + model: mockModel('\n* 🔴 User asked about Lisbon\n', observerCalls), + messageTokens: 1, + bufferTokens: false, + }, + reflection: { model: mockModel(''), observationTokens: 50_000 }, + }, + } as any; + const storage = new LibSQLStore({ id: randomUUID(), url: ':memory:' }); + const memory = new Memory({ + storage, + options: { + lastMessages: 10, + semanticRecall: false, + generateTitle: { model: mockModel(TITLE) }, + ...(c.instanceOM ? omOptions : {}), + } as any, + }); + const agent = new Agent({ + id: `a-${randomUUID()}`, + name: 'a', + model: mockModel(REPLY), + memory, + instructions: 'hi', + }); + const runner = c.engine === 'plain' ? agent : createDurableAgent({ agent }); + const mastra = new Mastra({ agents: { a: runner as any }, storage, logger: false }); + + const thread = randomUUID(); + const resource = randomUUID(); + const run = await streamTurn(runner, c.engine, 'Weather in Lisbon?', { + memory: { thread, resource, ...(c.callOM ? { options: omOptions } : {}) }, + }); + await (mastra as any).stopWorkers?.(); + expect(run.threw).toBeNull(); + + const messages = await persistedMessages(memory, thread, resource); + expect(messages.map(m => m.role)).toEqual(['user', 'assistant']); + expect(new Set(messages.map(m => m.id)).size).toBe(messages.length); + + if (c.instanceOM) { + // OM still runs: the observer was invoked and an observation was stored. + expect(observerCalls.count).toBeGreaterThan(0); + const record = await (await memory.omEngine)!.getRecord(thread, resource); + expect(record?.activeObservations).toContain('Lisbon'); + } else { + // The normal save path ran, including thread titling. + expect(observerCalls.count).toBe(0); + await expect + .poll(async () => (await memory.getThreadById({ threadId: thread }))?.title, { timeout: 5000 }) + .toBe(TITLE); + } + }); +}); diff --git a/packages/memory/src/call-only-observational-memory.test.ts b/packages/memory/src/call-only-observational-memory.test.ts new file mode 100644 index 00000000000..8472636a064 --- /dev/null +++ b/packages/memory/src/call-only-observational-memory.test.ts @@ -0,0 +1,50 @@ +import { InMemoryStore } from '@mastra/core/storage'; +import { describe, expect, it, vi } from 'vitest'; +import { Memory } from './index'; + +const om = { scope: 'thread', model: 'openai/gpt-4o-mini' } as const; + +function memoryWith(observationalMemory?: unknown) { + const memory = new Memory({ + storage: new InMemoryStore(), + options: observationalMemory === undefined ? {} : ({ observationalMemory } as any), + }); + const warn = vi.spyOn((memory as any).logger, 'warn'); + return { memory, warn }; +} + +describe('per-call observationalMemory on Memory.getMergedThreadConfig', () => { + it('turns a call opt-in off when the instance has no OM, and warns once', () => { + const { memory, warn } = memoryWith(); + expect(memory.getMergedThreadConfig({ observationalMemory: true }).observationalMemory).toBe(false); + expect(memory.getMergedThreadConfig({ observationalMemory: om as any }).observationalMemory).toBe(false); + expect(warn).toHaveBeenCalledTimes(1); + }); + + it('turns a call opt-in off when the instance sets { enabled: false }', () => { + const { memory, warn } = memoryWith({ enabled: false }); + expect(memory.getMergedThreadConfig({ observationalMemory: true }).observationalMemory).toBe(false); + expect(warn).toHaveBeenCalledTimes(1); + }); + + it('leaves a call opt-out unchanged and does not warn', () => { + const withOM = memoryWith(om); + expect(withOM.memory.getMergedThreadConfig({ observationalMemory: false }).observationalMemory).toBe(false); + expect( + withOM.memory.getMergedThreadConfig({ observationalMemory: { enabled: false } as any }).observationalMemory, + ).toMatchObject({ enabled: false }); + + const withoutOM = memoryWith(); + expect( + withoutOM.memory.getMergedThreadConfig({ observationalMemory: { enabled: false } as any }).observationalMemory, + ).toEqual({ enabled: false }); + expect(withOM.warn).not.toHaveBeenCalled(); + expect(withoutOM.warn).not.toHaveBeenCalled(); + }); + + it('keeps a call opt-in when the instance configures OM', () => { + const { memory, warn } = memoryWith(om); + expect(memory.getMergedThreadConfig({ observationalMemory: true }).observationalMemory).toBeTruthy(); + expect(warn).not.toHaveBeenCalled(); + }); +}); diff --git a/packages/memory/src/index.ts b/packages/memory/src/index.ts index d68ca42b37c..f7125e23043 100644 --- a/packages/memory/src/index.ts +++ b/packages/memory/src/index.ts @@ -541,8 +541,29 @@ export class Memory extends MastraMemory { } } + // Undefined while the base constructor runs getMergedThreadConfig to build threadConfig; + // true once this class's field initializers have run. + private readonly derivedFieldsInitialized = true; + private warnedIgnoredCallObservationalMemory = false; + public override getMergedThreadConfig(config?: MemoryConfigInternal): MemoryConfigInternal { const merged = super.getMergedThreadConfig(config); + // The OM engine is built from the instance config only (_initOMEngine), so a per-call + // opt-in on an instance without OM has nothing to observe or save the turn. Treat it as off + // so message history keeps saving instead of every engine skipping persistence. + if ( + this.derivedFieldsInitialized && + !normalizeObservationalMemoryConfig(this.threadConfig.observationalMemory) && + normalizeObservationalMemoryConfig(merged.observationalMemory) + ) { + if (!this.warnedIgnoredCallObservationalMemory) { + this.warnedIgnoredCallObservationalMemory = true; + this.logger.warn( + 'observationalMemory is enabled in call options but not on the Memory instance; it is ignored and message history is used instead. Configure observationalMemory on new Memory({ options }).', + ); + } + return this.applyManagedWorkingMemoryDefaults({ ...merged, observationalMemory: false }); + } return this.applyManagedWorkingMemoryDefaults(merged); }