A team of long-lived bots for DeepSeek Harness, with main bots, bot-to-bot messages and group chats.
Guard restored memory lines against new secrets, duplicates, and the topic cap
klarkxy937a435c16f6
diff --git a/ds-bot/src/host/memory.js b/ds-bot/src/host/memory.jsindex 2803ab6d..4b3c5609 100644--- a/ds-bot/src/host/memory.js+++ b/ds-bot/src/host/memory.js@@ -1459,7 +1459,9 @@ export function install(rt) {// left and the one it replaced, so one row can be undone with its inverse: an add// is taken out, a remove or replace puts its line back. A restore is applied only// while what it reads still reads as the row left it — an entry that changed- // since, or a line already back, is a NO_MATCH rather than a silent delete. The+ // since, or a line already back, is a NO_MATCH rather than a silent delete. A+ // line going back passes the checks a new entry does, so nothing the memory no+ // longer keeps returns and a fact remembered again is not duplicated. The// restore itself is a change: it goes through publish like the rest, and its row// is the inverse op with the id of the row it undid, so it can be undone again.// Archive rows move whole folders and are not undone this way.@@ -1529,13 +1531,18 @@ export function install(rt) {// row, so it goes where a new entry would; a topic the remove took away// is made again with its link, and a line already back is not duplicated.if (typeof row.before !== 'string') throw new Error(`Line ${line} of the change log is not a change that can be restored.`)+ const back = splitMeta(row.before.replace(/^[-*]\s+/, '')).text+ const problem = entryProblem(back)+ if (problem !== undefined) throw new Error(`The entry that line ${line} removed cannot be put back: ${problem}`)+ const [same] = matchEntries(allEntries(files), back).filter(entry => squash(entry.text) === squash(back))+ if (same !== undefined) throw noMatch(`The entry that line ${line} removed is already in ${label} (${fileName(same.file)}); there is nothing to restore.`)let file = targetif (file === undefined) {const topic = row.removedTopic === true && row.file.endsWith('.md') && row.file !== MAIN ? row.file.slice(0, -3) : undefinedif (topic === undefined) throw noMatch(`The file that line ${line} took the entry from is gone; restore it by hand.`)+ if (files.length - 1 >= MEMORY_LIMITS.topics) throw Object.assign(new Error(`${capital(label)} already has ${MEMORY_LIMITS.topics} topics, the most it holds. Add to one of them (recall lists them), or forget one.`), { code: FULL })file = { topic, file: row.file, path: join(dirOf(scope), row.file), content: '', ...parseMemory(`# ${topic}\n`) }}- if (file.lines.includes(row.before)) throw noMatch(`The entry that line ${line} removed is already in ${label}; there is nothing to restore.`)const lines = file.lines.length > 0 ? file.lines : parseMemory(`# ${file.topic}\n`).linesconst next = joinLines(insertEntry(lines, row.before))checkSize(file, next, label)@@ -1557,6 +1564,10 @@ export function install(rt) {const found = exactLines(target, row.after)if (found.length === 0) throw noMatch(`The entry that line ${line} changed is no longer in ${label}; it may have changed or been removed since.`)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)+ if (problem !== undefined) throw new Error(`The entry that line ${line} changed cannot be put back: ${problem}`)const lines = [...target.lines]lines[found[0].line] = row.beforeconst next = joinLines(lines)diff --git a/ds-bot/test/memory.test.js b/ds-bot/test/memory.test.jsindex 2674b8c5..f3f810c0 100644--- a/ds-bot/test/memory.test.js+++ b/ds-bot/test/memory.test.js@@ -326,6 +326,90 @@ test('restoring a remove that took a topic with it makes the topic again', asyncassert.match(read('bots', 'b2', 'MEMORY.md'), /- \[\[travel\]\]/)})+test('a restore does not put back a value that is a saved secret now', async (t) => {+ const { tool, call, read, journal, stop } = start()+ t.after(() => stop())+ await tool('remember', { text: 'The clubhouse code word is bricksparrow72' }, 'b2')+ await tool('remember', { text: 'Picks bricksparrow72 for the draw' }, 'b2')+ await tool('remember', { text: 'Picks plain tickets for the draw', replace: 'for the draw' }, 'b2')+ await tool('forget', { entry: 'code word' }, 'b2')+ const [replaced, removed] = journal().slice(-2)+ const lines = journal().length+ assert.match(read('bots', 'b2', 'MEMORY.md'), /plain tickets/)++ // The value is a saved secret since the change log kept it: putting the line+ // back would write it in the clear, to the file and to the restore's own row.+ const saved = await call('secret-set', { name: 'CLUB_CODE', value: 'bricksparrow72' })+ assert.equal(saved.ok, true, JSON.stringify(saved))++ const back = await call('memory-restore', { line: journal().findIndex(record => record.id === removed.id) + 1 })+ assert.equal(back.ok, false)+ assert.match(back.error.message, /cannot be put back/)+ assert.match(back.error.message, /never keeps/)+ assert.doesNotMatch(back.error.message, /bricksparrow72/)+ const backReplace = await call('memory-restore', { line: journal().findIndex(record => record.id === replaced.id) + 1 })+ assert.equal(backReplace.ok, false)+ assert.match(backReplace.error.message, /cannot be put back/)+ assert.doesNotMatch(backReplace.error.message, /bricksparrow72/)++ assert.doesNotMatch(read('bots', 'b2', 'MEMORY.md'), /bricksparrow72/)+ assert.equal(journal().length, lines)+})++test('the row a restore wrote passes the same guards when it is restored', async (t) => {+ const { tool, call, journal, stop } = start()+ t.after(() => stop())+ await tool('remember', { text: 'Wears a green coat' }, 'b2')+ // Undoing the add leaves a remove row of its own.+ const undone = await call('memory-restore', { line: journal().length })+ assert.equal(undone.ok, true, JSON.stringify(undone))+ assert.equal(journal().at(-1).op, 'remove')+ const line = journal().length++ // The fact is back under another topic: the restore row is not duplicated back.+ await tool('remember', { text: 'wears a green coat', topic: 'Style' }, 'b2')+ const again = await call('memory-restore', { line })+ assert.equal(again.ok, false)+ assert.match(again.error.message, /already in/)++ // And once the value is a saved secret, the same row cannot put it back either.+ const saved = await call('secret-set', { name: 'COAT_COLOR', value: 'green coat' })+ assert.equal(saved.ok, true, JSON.stringify(saved))+ const refused = await call('memory-restore', { line })+ assert.equal(refused.ok, false)+ assert.match(refused.error.message, /cannot be put back/)+ assert.doesNotMatch(refused.error.message, /green coat/)+ assert.equal(journal().length, line + 1)+})++test('a restore does not duplicate a fact remembered again', async (t) => {+ const { tool, call, read, journal, stop, home } = start()+ t.after(() => stop())+ await tool('remember', { text: 'Lives in Hangzhou', topic: 'Home' }, 'b2')+ await tool('forget', { entry: 'Hangzhou' }, 'b2')+ const line = journal().length+ // The same fact is back, in MEMORY.md this time and under new metadata.+ await tool('remember', { text: 'lives in hangzhou' }, 'b2')+ const restored = await call('memory-restore', { line })+ assert.equal(restored.ok, false)+ assert.match(restored.error.message, /already in/)+ assert.equal(parseMemory(read('bots', 'b2', 'MEMORY.md')).entries.length, 1)+ assert.equal(existsSync(join(home, 'memory', 'bots', 'b2', 'home.md')), false)+})++test('a restore cannot rebuild a topic past the count the memory holds', async (t) => {+ const { tool, call, journal, stop } = start()+ t.after(() => stop())+ for (let i = 1; i <= 40; i += 1) await tool('remember', { text: `detail ${i}`, topic: `topic-${i}` }, 'b2')+ // topic-40 goes when its last entry does; a fresh topic takes the room meanwhile.+ await tool('forget', { entry: 'detail 40', topic: 'topic-40' }, 'b2')+ const line = journal().length+ await tool('remember', { text: 'a fresh fact', topic: 'fresh' }, 'b2')+ const restored = await call('memory-restore', { line })+ assert.equal(restored.ok, false)+ assert.match(restored.error.message, /already has 40 topics/)+})+test('recall finds a continuous Chinese phrase, and keeps space-separated and Latin matching', async (t) => {const { tool, stop } = start()t.after(() => stop())