nanocoder icon indicating copy to clipboard operation
nanocoder copied to clipboard

[Bug] createMemoryCommand ignores options.memoryManager when constructing SummarizerService, causing /memory accept to write to detached store

Open rishu685 opened this issue 1 month ago • 1 comments

Description

Summary

In source/commands/memory.tsx, createMemoryCommand(options) allows callers to inject a custom memoryManager via options.memoryManager. However, when memoryManager is provided without an explicit options.summarizerService, the command instantiates SummarizerService using the no-arg constructor new SummarizerService().

Because SummarizerService defaults its constructor parameter to a new SemanticMemoryManager(), /memory accept saves accepted proposals to a disconnected default manager instead of the injected options.memoryManager.


Location

source/commands/memory.tsx#L185-L197

export function createMemoryCommand(
	options: MemoryCommandOptions = {},
): Command {
	let memoryManager = options.memoryManager;
	let summarizerService = options.summarizerService;
	if (!memoryManager) {
		const manager = new SemanticMemoryManager();
		memoryManager = manager;
		summarizerService ??= new SummarizerService(manager);
	} else {
		summarizerService ??= new SummarizerService(); // <--- BUG HERE
	}

Root Cause

  1. In source/memory/summarizer-service.ts, SummarizerService's constructor is:
    export class SummarizerService {
        constructor(
            private readonly memoryManager = new SemanticMemoryManager(),
            private readonly isMemoryEnabled: () => boolean = getSemanticMemoryEnabled,
        ) {}
    
  2. When a custom options.memoryManager is passed into createMemoryCommand({ memoryManager }), the else branch in createMemoryCommand executes:
    summarizerService ??= new SummarizerService();
    
  3. This creates a SummarizerService bound to an unconfigured, default SemanticMemoryManager instance rather than the caller-provided memoryManager.
  4. When a user runs /memory accept <number>, createMemoryCommand delegates to:
    const memory = await summarizerService.acceptProposal(
        proposal,
        metadata.sessionId,
    );
    
    summarizerService.acceptProposal calls this.memoryManager.addMemory(...), saving the accepted memory into the default store.
  5. In contrast, /memory list, /memory delete, and /memory clear all operate on memoryManager (options.memoryManager).
  6. As a result, the accepted proposal never appears in /memory list on the configured manager, causing silent data desynchronization.

Steps to Reproduce / Reproduction Test Case

import test from 'ava';
import { createMemoryCommand } from './memory';
import { SemanticMemoryManager } from '@/memory/semantic-memory-manager';

test('createMemoryCommand binds summarizerService to the injected memoryManager', async t => {
    const customManager = new SemanticMemoryManager();
    const command = createMemoryCommand({ memoryManager: customManager });

    // Propose and accept a memory
    await command.handler(['propose'], [
        { role: 'user', content: 'We must always use UTF-8 encoding.' },
        { role: 'assistant', content: 'Understood, will always enforce UTF-8.' }
    ], { sessionId: 'test-session' } as any);

    await command.handler(['accept', '1'], [], { sessionId: 'test-session' } as any);

    // List from the injected memory manager
    const memories = await customManager.listMemories();
    t.is(memories.length, 1, 'Injected memoryManager should contain the accepted memory');
});

Expected: customManager.listMemories() returns 1 memory.
Actual: customManager.listMemories() returns 0 memories because the accepted proposal was saved to the default SemanticMemoryManager instantiated inside new SummarizerService().


Suggested Fix

Simplify and properly pass memoryManager when constructing SummarizerService:

 export function createMemoryCommand(
 	options: MemoryCommandOptions = {},
 ): Command {
-	let memoryManager = options.memoryManager;
-	let summarizerService = options.summarizerService;
-	if (!memoryManager) {
-		const manager = new SemanticMemoryManager();
-		memoryManager = manager;
-		summarizerService ??= new SummarizerService(manager);
-	} else {
-		summarizerService ??= new SummarizerService();
-	}
+	const memoryManager = options.memoryManager ?? new SemanticMemoryManager();
+	const summarizerService =
+		options.summarizerService ?? new SummarizerService(memoryManager);

rishu685 avatar Oct 12 '26 10:10 rishu685

once confirmed,will raise the PR

rishu685 avatar Oct 12 '26 10:10 rishu685