Skip to content

A team of long-lived bots for DeepSeek Harness, with main bots, bot-to-bot messages and group chats.

Give a group Session its group's memory as a message, not in its prompt

Fei24113a5915de8aa4
diff --git a/docs/plans/2026-10-09-context-and-memory.md b/docs/plans/2026-10-09-context-and-memory.md
index 7900fdf4..2ea05fc5 100644
--- a/docs/plans/2026-10-09-context-and-memory.md
+++ b/docs/plans/2026-10-09-context-and-memory.md
@@ -122,7 +122,7 @@ Bot 是长期的:用户和一个 Bot 聊几周、几个月,上下文一定
- 每个 Session 第一次渲染时把记忆段冻结下来,之后提示词保持逐字节不变,提供方的前缀缓存才能命中。换到新段,或者做了检查点(检查点本来就会打破缓存),会重新冻结。
- 所以一段对话开始之后才保存的条目,不会出现在这段的提示词里。提示词要求 Bot:在回答「不知道」之前先调用 `recall`。`recall` 能查到最新的条目。
- `recall` 不带参数时,列出各处存了什么;带 `query` 时搜索;带 `topic` 时整份读一个主题。默认查自己的和团队的,在群里再加上这个群的。
-- 群会话的记忆段把群记忆放在最后,前面和私聊的记忆段一样,尽量多地共用前缀缓存。
+- 群会话的系统提示词和私聊逐字节相同(提供方把工具排在整个系统提示词之后,提示词里有一处不同,后面的工具就都不能共用缓存)。群记忆不进系统提示词:群会话第一步之前作为一条快照消息放进会话,之后跟着会话冻结;检查点把它压缩掉以后,下一步再放一次当时的群记忆。改之前已经把群记忆冻结在提示词里的群会话照旧,不再加这条消息。
## 不做什么
@@ -149,3 +149,4 @@ Bot 是长期的:用户和一个 Bot 聊几周、几个月,上下文一定
- 2026-10-23:换工作文件夹触发的换段在会话尾部显示一行状态(「正在切换工作文件夹」,#486)。其他原因的换段仍然无感。
- 2026-10-23:加 `pending.json`,一次改动当成一次来做(#481)。之前文件先写、日志后写,中断会留下没有日志的条目,或没有链接的主题。
- 2026-10-23:没写进记忆的时候,`remember` / `forget` 报错,对话里显示一行失败提示和记忆面板的入口(#482)。
+- 2026-11-02:群记忆从群会话的系统提示词挪到会话开头的一条快照消息,群会话的系统提示词和工具与私聊相同,首轮能共用私聊的前缀缓存(实测首轮命中从约 2k 到约 13k token)。
diff --git a/ds-bot/src/host/memory.js b/ds-bot/src/host/memory.js
index e6a0ac20..2ac8f229 100644
--- a/ds-bot/src/host/memory.js
+++ b/ds-bot/src/host/memory.js
@@ -28,7 +28,7 @@ import { createHash, randomUUID } from 'node:crypto'
import { existsSync, mkdirSync, readFileSync, readdirSync, renameSync, truncateSync, unlinkSync, writeFileSync } from 'node:fs'
import { appendFile, mkdir, rename, unlink, writeFile } from 'node:fs/promises'
import { dirname, join } from 'node:path'
-import { BlockAssembler, HarnessError } from '@deepseek-ai/dsh-llm'
+import { BlockAssembler, HarnessError, createUserMessage } from '@deepseek-ai/dsh-llm'
import { defineTool } from '@deepseek-ai/dsh-tools'
import { replaceFile } from './files.js'
import { inside, pendingPath, read as readPending, record as pendingRecord, relativePath, serialize as serializePending } from './memory-pending.js'
@@ -334,9 +334,8 @@ const teamRule = (main, writer, privateBot) => (main
? '- The team memory is shared by every Bot. As a group admin you write it with remember scope "team", for what the whole team needs.'
: '- The team memory is shared by every Bot. Main Bots and group admins write it; when the whole team should know something, tell a Main Bot with message_bot.')
-// A group Session shows its group's memory after the rest of the section, so the text
-// before it is the same as in the Bot's own chat.
const GROUP_HEADING = '## Group memory, shared by the members of this group chat'
+const GROUP_MEMORY_SOURCE = 'ds-bot-group-memory'
const groupNote = review => [
'Only the members of this group read it, and only in their conversations here. In this group, remember and forget write it unless you pass scope "own", for what you need in all your conversations; recall searches it with yours and the team\'s.',
review ? ' The memory review after each turn here keeps in it what the group should know.' : '',
@@ -936,7 +935,7 @@ export function install(rt) {
return { lines: [], failed: true }
}
}
- function memorySection(bot, roomId) {
+ function memorySection(bot) {
const own = ownScope(bot.id)
const main = sectionRead(join(dirOf(own), MAIN))
const team = sectionRead(join(dirOf(TEAM), MAIN))
@@ -955,23 +954,15 @@ export function install(rt) {
...sectionEntries(team),
...teamTopics.lines,
]
- let failed = main.failed !== undefined || team.failed !== undefined || ownTopics.failed === true || teamTopics.failed === true
- if (roomId === undefined) return { text: lines.join('\n'), failed }
+ return { text: lines.join('\n'), failed: main.failed !== undefined || team.failed !== undefined || ownTopics.failed === true || teamTopics.failed === true }
+ }
+ function groupSection(roomId) {
const group = groupScope(roomId)
const groupRead = sectionRead(join(dirOf(group), MAIN))
const groupTopics = sectionTopics(group)
- failed = failed || groupRead.failed !== undefined || groupTopics.failed === true
return {
- text: [
- ...lines,
- '',
- GROUP_HEADING,
- groupNote(reviewOn),
- '',
- ...sectionEntries(groupRead),
- ...groupTopics.lines,
- ].join('\n'),
- failed,
+ text: [GROUP_HEADING, groupNote(reviewOn), '', ...sectionEntries(groupRead), ...groupTopics.lines].join('\n'),
+ failed: groupRead.failed !== undefined || groupTopics.failed === true,
}
}
@@ -995,10 +986,11 @@ export function install(rt) {
const bot = rt.botOf(rt.selfOf(sessionId))
if (bot === undefined) return ''
// A section frozen under another role (made a Main Bot or a group admin since, or no
- // longer one) is frozen again, so the rule it states matches what the tools allow;
- // so is a group Session's section frozen before groups kept a memory.
- const roomId = roomIdOf(sessionId)
- const marks = [teamRule(rt.isMain(bot.id), writesTeam(bot.id), rt.isPrivate(bot.id)), ...(roomId === undefined ? [] : [GROUP_HEADING])]
+ // longer one) is frozen again, so the rule it states matches what the tools allow.
+ // A group Session's section is the Bot's own; its group's memory comes as a message
+ // (see the agent/pre-step below), so both share the provider's cached prefix, tools
+ // included: the provider puts the tools after the whole system prompt.
+ const marks = [teamRule(rt.isMain(bot.id), writesTeam(bot.id), rt.isPrivate(bot.id))]
const holds = text => marks.every(mark => text.includes(mark))
const kept = frozen.get(sessionId)
if (kept !== undefined && holds(kept)) return kept
@@ -1017,7 +1009,7 @@ export function install(rt) {
// the files again.
let failed = snap.failed !== undefined
if (text === '' || !holds(text)) {
- const section = memorySection(bot, roomId)
+ const section = memorySection(bot)
text = section.text
failed = failed || section.failed
if (!failed && rt.readOnly() === undefined) {
@@ -1047,6 +1039,30 @@ export function install(rt) {
text: context => sectionFor(context?.agent?.id),
}), 'ds-bot: memory section')
+ // A group Session reads its group's memory from a snapshot message, put in before
+ // its first step and again once a checkpoint has condensed it away, so the memory
+ // stays frozen with the conversation and the prompt stays the Bot's own. A Session
+ // frozen with the group's memory in its prompt keeps it there.
+ const isGroupMemory = message => message?.source?.kind === GROUP_MEMORY_SOURCE
+ ctx.on('agent/pre-step', async ({ agent, signal }, next) => {
+ const decision = await next()
+ if (decision.kind === 'reject' || signal?.aborted || !Array.isArray(decision.messages)) return decision
+ await rt.load()
+ const roomId = roomIdOf(agent.id)
+ if (roomId === undefined || rt.botOf(rt.selfOf(agent.id)) === undefined) return decision
+ if (sectionFor(agent.id).includes(GROUP_HEADING) || decision.messages.some(isGroupMemory)) return decision
+ try {
+ if (agent.session?.deriveMessages?.().some(isGroupMemory)) return decision
+ } catch { /* no history to look in: the snapshot goes in */ }
+ const group = groupSection(roomId)
+ if (group.failed) return decision
+ const message = createUserMessage({
+ content: [{ type: 'text', text: group.text }],
+ source: { kind: GROUP_MEMORY_SOURCE, form: 'snapshot', sections: [{ name: GROUP_MEMORY_SOURCE, text: group.text }] },
+ })
+ return { ...decision, messages: [message, ...decision.messages] }
+ })
+
// ---------------------------------------------------------------------------
// Tools.
diff --git a/ds-bot/test/install.test.js b/ds-bot/test/install.test.js
index 2eb38b4d..c80a6d29 100644
--- a/ds-bot/test/install.test.js
+++ b/ds-bot/test/install.test.js
@@ -66,6 +66,7 @@ test('apply registers listeners and effects in a fixed order', (t) => {
'effect ds-bot: memory recovery',
'effect ds-bot: memory section',
'prompt bot-memory 645',
+ 'on agent/pre-step',
'on agent/status',
'effect ds-bot: memory review',
'effect ds-bot: Bot compaction',
diff --git a/ds-bot/test/memory.test.js b/ds-bot/test/memory.test.js
index 808dcfaf..74196d1e 100644
--- a/ds-bot/test/memory.test.js
+++ b/ds-bot/test/memory.test.js
@@ -187,6 +187,16 @@ function start({ home = mkdtempSync(join(tmpdir(), 'ds-bot-memory-')), replies =
const settle = () => new Promise(resolve => setTimeout(resolve, 20))
+// The group memory message the memory's pre-step puts before a Session's step, given
+// what its history holds.
+const textOf = message => (message?.content ?? []).map(block => block.text).join('')
+async function groupMemoryOf(ctx, sessionId, history = []) {
+ const step = ctx.listeners.get('agent/pre-step').find(listener => /isGroupMemory/.test(String(listener)))
+ const agent = { id: sessionId, session: { deriveMessages: () => history } }
+ const decision = await step({ agent, messages: [], step: 1, signal: AbortSignal.timeout(10_000) }, async () => ({ kind: 'enter', messages: [] }))
+ return decision.messages.find(message => message.source?.kind === 'ds-bot-group-memory')
+}
+
// The summary is written to disk after the model answers; a fixed wait is too short on
// a slow disk and leaves the write running into the next step or the cleanup.
async function summarized(call, botId = 'b2') {
@@ -1059,13 +1069,17 @@ test('the members of a group read its memory in their Sessions there, and no Bot
ctx.emit('agent/status', { agent: { id: 'g3' }, status: 'idle' })
await reviewed(call)
assert.equal(requests.length, 1)
- for (const member of ['g2', 'g3']) {
- assert.match(section(member), /\n\n## Group memory, shared by the members of this group chat\nOnly the members of this group read it[^\n]*\n\n- The user wants everything under one page \[source: group "Launch"[^\n]*\]$/)
+ for (const [member, bot] of [['g2', 'b2'], ['g3', 'b3']]) {
+ // The prompt is the Bot's own, so the provider's cached prefix holds in both; the
+ // group's memory comes in a snapshot message before the first step.
+ assert.equal(section(member), section(bot))
+ assert.match(textOf(await groupMemoryOf(ctx, member)), /^## Group memory, shared by the members of this group chat\nOnly the members of this group read it[^\n]*\n\n- The user wants everything under one page \[source: group "Launch"[^\n]*\]$/)
assert.match(await tool('recall', { query: 'page' }, member), /^Entries with "page" in your memory, the group memory, and the team memory:\n\[group · MEMORY\.md\] The user wants everything under one page/)
}
// Not in a member's own chat, nor for a Bot outside the group, a Main Bot included.
for (const elsewhere of ['b2', 'b3', 'b4', 'm1']) {
assert.doesNotMatch(section(elsewhere), /one page|Group memory/, elsewhere)
+ assert.equal(await groupMemoryOf(ctx, elsewhere), undefined, elsewhere)
assert.match(await tool('recall', { query: 'page' }, elsewhere), /^Nothing in your memory and the team memory mentions "page"/, elsewhere)
assert.match(await tool('recall', { scope: 'group' }, elsewhere), /^There is no group memory here/, elsewhere)
}
@@ -1098,12 +1112,29 @@ test('in a group, remember and forget write the group memory unless told otherwi
assert.deepEqual(parseMemory(read('groups', 'r1', 'MEMORY.md')).entries.map(entry => entry.text), ['Ship on Friday'])
})
-test('a group Session frozen before groups kept a memory is frozen again, with it', async (t) => {
- const { tool, section, home, stop } = start()
+test('a group Session gets its group\'s memory as a message once, and again after a checkpoint condensed it', async (t) => {
+ const { ctx, tool, section, home, stop } = start()
t.after(() => stop())
await tool('remember', { text: 'Ship on Friday' }, 'g3')
writeFileSync(join(home, 'memory', 'snapshots', 'g3.md'), section('b3'))
- assert.match(section('g3'), /## Group memory, shared by the members of this group chat\n[^\n]*\n\n- Ship on Friday \[/)
+ assert.equal(section('g3'), section('b3'))
+ const first = await groupMemoryOf(ctx, 'g3')
+ assert.match(textOf(first), /^## Group memory, shared by the members of this group chat\n[^\n]*\n\n- Ship on Friday \[/)
+ // Still in the history: not put in again.
+ assert.equal(await groupMemoryOf(ctx, 'g3', [first]), undefined)
+ // Condensed away by a checkpoint: it comes again, as the memory is now.
+ await tool('remember', { text: 'Demo on Monday' }, 'g3')
+ assert.match(textOf(await groupMemoryOf(ctx, 'g3', [])), /- Ship on Friday \[[^\n]*\n- Demo on Monday \[/)
+})
+
+test('a group Session frozen with its group\'s memory in its prompt keeps it there, with no message', async (t) => {
+ const { ctx, tool, section, home, stop } = start()
+ t.after(() => stop())
+ await tool('remember', { text: 'Ship on Friday' }, 'g3')
+ const frozen = `${section('b3')}\n\n## Group memory, shared by the members of this group chat\n- Ship on Friday`
+ writeFileSync(join(home, 'memory', 'snapshots', 'g3.md'), frozen)
+ assert.equal(section('g3'), frozen)
+ assert.equal(await groupMemoryOf(ctx, 'g3'), undefined)
})
test('deleting a group moves its memory to the archive, with its members\' snapshots gone', async (t) => {