From 418884d5ff67fbc18430c8afde845ec786b7ebc6 Mon Sep 17 00:00:00 2001 From: wbs <2185972232@qq.com> Date: Fri, 20 Nov 2026 16:14:34 +0800 Subject: [PATCH] fix(memory): report an empty memory index --- CHANGELOG.md | 2 + docs/features/harness/prompt/context.md | 2 + src/context.ts | 9 +- .../__tests__/emptyMemoryContext.test.ts | 120 ++++++++++++++++++ src/utils/claudemd.ts | 25 ++++ 5 files changed, 157 insertions(+), 1 deletion(-) create mode 100644 src/utils/__tests__/emptyMemoryContext.test.ts diff --git a/CHANGELOG.md b/CHANGELOG.md index d7121eac..e9976cac 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -28,6 +28,8 @@ All notable changes to DreamCoder will be documented in this file. ### Fixed +- The main session now tells the agent when its memory index is empty or has not been created yet (#278). + - **Saving a memory no longer asks for approval every time.** Writes to the project's memory folder are allowed like the instructions say, also in bypass mode (#270). - **Stopping a reply keeps the session's context.** Stop ends the agent process only when the reply has not stopped a few seconds after you press Stop, and a restarted agent keeps uploaded images and files, instruction files from subfolders and files restored after compaction, without adding a "Continue from where you left off." message (#270). - The agent knows the available skills again after compaction, gets only newly added skills when the skill folders change, and does not get the whole list again after a restart (#270). diff --git a/docs/features/harness/prompt/context.md b/docs/features/harness/prompt/context.md index 3a99bad3..f18425ab 100644 --- a/docs/features/harness/prompt/context.md +++ b/docs/features/harness/prompt/context.md @@ -186,6 +186,8 @@ ### T-CTX-10 `MEMORY.md` 不存在或为空时什么也不发,记忆说明却写「MEMORY.md 总会载入上下文」 +> 已修复:自动记忆启用且索引不存在或为空时,主会话会收到记忆为空的说明;下文记录修复前的行为。 + - 类型:问题 - 严重程度:低 - 体量:缺一句约 80 字符的说明;每个请求 diff --git a/src/context.ts b/src/context.ts index 75fc49bd..51e0311a 100644 --- a/src/context.ts +++ b/src/context.ts @@ -9,6 +9,7 @@ import { filterInjectedMemoryFiles, getClaudeMds, getClaudeMdsForSubagent, + getEmptyAutoMemoryNotice, getMemoryFiles, } from './utils/claudemd.js' import { logForDiagnosticsNoPII } from './utils/diagLogs.js' @@ -180,9 +181,15 @@ export const getUserContext = memoize( const shouldDisableClaudeMd = isClaudeMdDisabled() // Await the async I/O (readFile/readdir directory walk) so the event // loop yields naturally at the first fs.readFile. + const memoryFiles = shouldDisableClaudeMd ? [] : await getMemoryFiles() const claudeMd = shouldDisableClaudeMd ? null - : getClaudeMds(filterInjectedMemoryFiles(await getMemoryFiles())) + : [ + getClaudeMds(filterInjectedMemoryFiles(memoryFiles)), + await getEmptyAutoMemoryNotice(memoryFiles), + ] + .filter(Boolean) + .join('\n\n') // Cache for the auto-mode classifier (yoloClassifier.ts reads this // instead of importing claudemd.ts directly, which would create a // cycle through permissions/filesystem → permissions → yoloClassifier). diff --git a/src/utils/__tests__/emptyMemoryContext.test.ts b/src/utils/__tests__/emptyMemoryContext.test.ts new file mode 100644 index 00000000..e0b6561e --- /dev/null +++ b/src/utils/__tests__/emptyMemoryContext.test.ts @@ -0,0 +1,120 @@ +import { + afterEach, + beforeEach, + describe, + expect, + test, +} from 'bun:test' +import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from 'fs' +import { tmpdir } from 'os' +import { join } from 'path' +import { + getCachedClaudeMdContent, + setCachedClaudeMdContent, +} from '../../bootstrap/state.js' +import { getSubagentUserContext, getUserContext } from '../../context.js' +import { getAutoMemPath } from '../../memdir/paths.js' +import { clearMemoryFileCaches } from '../claudemd.js' + +const envNames = [ + 'CLAUDE_COWORK_MEMORY_PATH_OVERRIDE', + 'CLAUDE_CODE_DISABLE_AUTO_MEMORY', + 'CLAUDE_CODE_DISABLE_CLAUDE_MDS', + 'CLAUDE_CODE_SIMPLE', +] as const + +let memoryDir: string +let originalEnv: Map +let originalCachedClaudeMd: string | null + +function clearContextCaches() { + getAutoMemPath.cache.clear?.() + clearMemoryFileCaches() + getUserContext.cache.clear?.() +} + +beforeEach(() => { + originalEnv = new Map(envNames.map(name => [name, process.env[name]])) + originalCachedClaudeMd = getCachedClaudeMdContent() + + memoryDir = mkdtempSync(join(tmpdir(), 'dreamcoder-memory-test-')) + + process.env.CLAUDE_COWORK_MEMORY_PATH_OVERRIDE = memoryDir + process.env.CLAUDE_CODE_DISABLE_AUTO_MEMORY = '0' + process.env.CLAUDE_CODE_DISABLE_CLAUDE_MDS = '0' + process.env.CLAUDE_CODE_SIMPLE = '0' + + clearContextCaches() +}) + +afterEach(() => { + for (const name of envNames) { + const originalValue = originalEnv.get(name) + + if (originalValue === undefined) { + delete process.env[name] + } else { + process.env[name] = originalValue + } + } + + clearContextCaches() + setCachedClaudeMdContent(originalCachedClaudeMd) + rmSync(memoryDir, { recursive: true, force: true }) +}) + +describe('empty auto-memory in the main session', () => { + test('reports an empty memory when MEMORY.md does not exist', async () => { + const context = await getUserContext() + const text = context.claudeMd ?? '' + + expect(text).toContain('MEMORY.md is currently empty') + }) + + test('reports an empty memory when MEMORY.md contains only whitespace', async () => { + writeFileSync(join(memoryDir, 'MEMORY.md'), ' \n ') + + const context = await getUserContext() + const text = context.claudeMd ?? '' + + expect(text).toContain('MEMORY.md is currently empty') + }) + + test('loads a non-empty MEMORY.md without reporting it as empty', async () => { + writeFileSync( + join(memoryDir, 'MEMORY.md'), + '- [Testing preference](testing.md) — Use Bun', + ) + + const context = await getUserContext() + const text = context.claudeMd ?? '' + + expect(text).toContain('Use Bun') + expect(text).not.toContain('MEMORY.md is currently empty') + }) + + test('does not report an unreadable memory as empty', async () => { + mkdirSync(join(memoryDir, 'MEMORY.md')) + + const context = await getUserContext() + const text = context.claudeMd ?? '' + + expect(text).not.toContain('MEMORY.md is currently empty') + }) + + test('does not include the main session memory notice in subagent context', async () => { + const context = await getSubagentUserContext() + const text = context.claudeMd ?? '' + + expect(text).not.toContain('MEMORY.md is currently empty') + }) + + test('does not report an empty memory when auto-memory is disabled', async () => { + process.env.CLAUDE_CODE_DISABLE_AUTO_MEMORY = '1' + + const context = await getUserContext() + const text = context.claudeMd ?? '' + + expect(text).not.toContain('MEMORY.md is currently empty') + }) +}) diff --git a/src/utils/claudemd.ts b/src/utils/claudemd.ts index dd7d8e13..d640dffc 100644 --- a/src/utils/claudemd.ts +++ b/src/utils/claudemd.ts @@ -1197,6 +1197,31 @@ function describeMemoryFile(file: MemoryFileInfo): string { return `Contents of ${file.path}${description}:\n\n${content}` } +export async function getEmptyAutoMemoryNotice( + memoryFiles: MemoryFileInfo[], +): Promise { + if ( + !isAutoMemoryEnabled() || + getFeatureValue_CACHED_MAY_BE_STALE('tengu_moth_copse', false) + ) { + return null + } + + const emptyNotice = + 'Your MEMORY.md is currently empty. When you save new memories, they will appear here.' + const entry = memoryFiles.find(file => file.type === 'AutoMem') + if (entry) { + return entry.content.trim() ? null : emptyNotice + } + + try { + await getFsImplementation().stat(getAutoMemEntrypoint()) + return null + } catch (error) { + return getErrnoCode(error) === 'ENOENT' ? emptyNotice : null + } +} + export const getClaudeMds = ( memoryFiles: MemoryFileInfo[], filter?: (type: MemoryType) => boolean,