Skip to content

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

Dedupe the line a replace-restore puts back across the whole scope

klarkxy9790a1daee63
diff --git a/ds-bot/src/host/memory.js b/ds-bot/src/host/memory.js
index 4b3c5609..a12cbf49 100644
--- a/ds-bot/src/host/memory.js
+++ b/ds-bot/src/host/memory.js
@@ -1566,8 +1566,14 @@ export function install(rt) {
if (found.length > 1) throw noMatch(`The line that line ${line} changed is in ${label} more than once; restore it by hand so the wrong one is not changed.`)
// The before-line passes the checks a new entry does, so a restore does not
// write — into the file or the change log — what memory no longer keeps.
- const problem = entryProblem(splitMeta(row.before.replace(/^[-*]\s+/, '')).text)
+ const back = splitMeta(row.before.replace(/^[-*]\s+/, '')).text
+ const problem = entryProblem(back)
if (problem !== undefined) throw new Error(`The entry that line ${line} changed cannot be put back: ${problem}`)
+ // Nor does it come back twice: the same fact may have been saved again since,
+ // under another topic or new metadata. The matched entry itself does not
+ // count: an after that squashes to the before text is no duplicate.
+ const same = matchEntries(allEntries(files), back).find(entry => squash(entry.text) === squash(back) && !(entry.file === target && entry.line === found[0].line))
+ if (same !== undefined) throw noMatch(`The entry that line ${line} changed is already in ${label} (${fileName(same.file)}); there is nothing to restore.`)
const lines = [...target.lines]
lines[found[0].line] = row.before
const next = joinLines(lines)
diff --git a/ds-bot/test/memory.test.js b/ds-bot/test/memory.test.js
index f3f810c0..3d873d44 100644
--- a/ds-bot/test/memory.test.js
+++ b/ds-bot/test/memory.test.js
@@ -410,6 +410,35 @@ test('a restore cannot rebuild a topic past the count the memory holds', async (
assert.match(restored.error.message, /already has 40 topics/)
})
+test('a restore of a replace does not duplicate a fact remembered again', async (t) => {
+ const { tool, call, read, journal, stop } = start()
+ t.after(() => stop())
+ await tool('remember', { text: 'Lives in Hangzhou' }, 'b2')
+ await tool('remember', { text: 'Lives in Shanghai', replace: 'Hangzhou' }, 'b2')
+ const replaced = journal().at(-1)
+ // The old fact is remembered again since, under a topic and new metadata.
+ await tool('remember', { text: 'lives in hangzhou', topic: 'Home' }, 'b2')
+ const restored = await call('memory-restore', { line: journal().findIndex(record => record.id === replaced.id) + 1 })
+ assert.equal(restored.ok, false)
+ assert.match(restored.error.message, /already in/)
+ // The entry it replaced stays put, and the remembered copy is not doubled.
+ assert.match(read('bots', 'b2', 'MEMORY.md'), /- Lives in Shanghai \[/)
+ assert.equal(parseMemory(read('bots', 'b2', 'home.md')).entries.length, 1)
+})
+
+test('a restore of a replace whose after reads like its before is no duplicate', async (t) => {
+ const { tool, call, read, journal, stop } = start()
+ t.after(() => stop())
+ await tool('remember', { text: 'Drinks tea daily' }, 'b2')
+ await tool('remember', { text: 'drinks tea daily', replace: 'tea' }, 'b2')
+ // The after-line squashes to the before text: the matched entry itself is no
+ // duplicate, so the restore still puts the recorded before-line back.
+ const restored = await call('memory-restore', { line: journal().length })
+ assert.equal(restored.ok, true, JSON.stringify(restored))
+ assert.match(read('bots', 'b2', 'MEMORY.md'), /- Drinks tea daily \[/)
+ assert.doesNotMatch(read('bots', 'b2', 'MEMORY.md'), /- drinks tea daily/)
+})
+
test('recall finds a continuous Chinese phrase, and keeps space-separated and Latin matching', async (t) => {
const { tool, stop } = start()
t.after(() => stop())