A team of long-lived bots for DeepSeek Harness, with main bots, bot-to-bot messages and group chats.
Count only the changes a memory review kept toward another pass
Fei241128ac07b671dc
diff --git a/ds-bot/src/host/memory.js b/ds-bot/src/host/memory.jsindex 6e617440..0eb00ac6 100644--- a/ds-bot/src/host/memory.js+++ b/ds-bot/src/host/memory.js@@ -1542,10 +1542,11 @@ export function install(rt) {async function applyReview({ keeper, name, home, numbered, changes, source, teamWrites }) {const actor = { id: keeper.id, name: keeper.name, auto: true }const done = []+ const counts = () => ({ kept: done.length, changed: done.filter(result => result.changed).length })let failedfor (const change of changes) {// A keeper deleted mid-review keeps no more changes, its team ones included.- if (rt.botOf(keeper.id) === undefined) return 0+ if (rt.botOf(keeper.id) === undefined) return counts()// Its role may change mid-review too, so each change asks again.const team = teamWrites()const op = String(change?.op ?? '')@@ -1576,7 +1577,7 @@ export function install(rt) {else if (op === 'edit') done.push(await replaceEntry({ scope, topic: target.file.topic, match: target.text, text, meta, actor, label }))else if (op === 'remove') done.push(await removeEntry({ scope, topic: target.file.topic, match: target.text, actor, label }))} catch (failure) {- if (failure?.code === GONE) return 0+ if (failure?.code === GONE) return counts()// The change is named by its text and where it was headed, so the// diagnostics say what was not kept, not only why.const held = op === 'remove' ? `the removal of "${clip(target.text, 160)}"` : `the entry "${clip(text, 160)}"`@@ -1588,7 +1589,7 @@ export function install(rt) {// A write that broke keeps the review's place: it may work when the lines are// next read. What memory could not keep was set aside above, said already.if (failed !== undefined) throw failed- return done.filter(result => result.changed).length+ return counts()}const roomCopy = room => ({ id: room.id, name: room.name, seq: room.seq, log: [...room.log ?? []] })@@ -1690,13 +1691,15 @@ export function install(rt) {const shown = clip(JSON.stringify(change), 160)rt.warn('ds-bot: memory review of %s left out a change: %s', name, secretEntry(shown) ? 'it looks like a key, token, password, or ID number, which memory never keeps' : `it is not a well-formed change: ${shown}`)}- const changed = await applyReview({ keeper, name, home, numbered, changes: changes.slice(0, REVIEW_CHANGES_MAX), source: batch.source(read), teamWrites })+ const { kept, changed } = await applyReview({ keeper, name, home, numbered, changes: changes.slice(0, REVIEW_CHANGES_MAX), source: batch.source(read), teamWrites })if (!alive()) return- // An answer at the limit may have left some out, so the same lines are read- // again, a few times at most, while each pass still changes something.+ // An answer that kept all one answer may hold may have left some out, so+ // the same lines are read again, a few times at most, while each pass+ // still changes something. What was set aside is no part of the limit: it+ // was handled, and the same lines read again would meet it the same way.const prior = overflowed.get(key)const passes = prior !== undefined && prior.since === since ? prior.passes : 0- if (changes.length + leftOut.length >= REVIEW_CHANGES_MAX && changed > 0 && passes < REVIEW_PASSES_MAX) {+ if (kept >= REVIEW_CHANGES_MAX && changed > 0 && passes < REVIEW_PASSES_MAX) {overflowed.set(key, { since, passes: passes + 1 })through = sinceagain = truediff --git a/ds-bot/test/memory.test.js b/ds-bot/test/memory.test.jsindex acec5489..00f404e2 100644--- a/ds-bot/test/memory.test.js+++ b/ds-bot/test/memory.test.js@@ -841,6 +841,34 @@ test('an answer that reaches the change limit exactly is followed by another pasassert.deepEqual(JSON.parse(read('reviewed.json')), { 'chat:b2': 11 })})+test('a change set aside is no part of the change limit, so the same lines are not read again for it', async (t) => {+ const home = mkdtempSync(join(tmpdir(), 'ds-bot-memory-'))+ const dir = join(home, 'memory', 'bots', 'b2')+ mkdirSync(dir, { recursive: true })+ for (let at = 1; at <= MEMORY_LIMITS.topics; at += 1) writeFileSync(join(dir, `topic-${at}.md`), `# topic-${at}\n\n- fact ${at}\n`)+ writeFileSync(join(dir, 'MEMORY.md'), '# Memory\n\n## Index\n')+ // Seven changes land; two more reach the limit only by counting what was set+ // aside: an add that names a 41st topic the memory has no room for, and a+ // change that is not well formed. Neither makes the review ask again.+ const replies = [JSON.stringify({+ changes: [+ ...Array.from({ length: 7 }, (_, at) => ({ op: 'add', text: `The user likes thing ${at + 1}` })),+ { op: 'add', text: 'The user likes oolong', topic: 'tea' },+ { op: 'add' },+ ],+ })]+ const { ctx, call, read, requests, warnings, stop } = start({ home, replies, config: { memoryReviewDelayMs: 0 } })+ t.after(() => stop())+ await call('state', {})+ ctx.emit('agent/status', { agent: { id: 'b2' }, status: 'idle' })+ await reviewed(call)+ assert.equal(requests.length, 1, 'a change set aside made the review read the same lines again')+ assert.equal(parseMemory(read('bots', 'b2', 'MEMORY.md')).entries.length, 7)+ assert.ok(warnings.some(line => line.includes('left out a change') && line.includes('the most it holds')), warnings.join('\n'))+ assert.ok(warnings.some(line => line.includes('left out a change') && line.includes('not a well-formed change')), warnings.join('\n'))+ assert.deepEqual(JSON.parse(read('reviewed.json')), { 'chat:b2': 11 })+})+test('a full MEMORY.md sets an add aside, says so, and an add with a topic still lands', async (t) => {const home = mkdtempSync(join(tmpdir(), 'ds-bot-memory-'))mkdirSync(join(home, 'memory', 'bots', 'b2'), { recursive: true })