Skip to content

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

Keep a rebuilt topic from overwriting its case-folded file or leaking a secret name

klarkxyc5a6ecb98fbd
diff --git a/ds-bot/src/host/memory.js b/ds-bot/src/host/memory.js
index 48b676a0..0284275a 100644
--- a/ds-bot/src/host/memory.js
+++ b/ds-bot/src/host/memory.js
@@ -1551,10 +1551,24 @@ export function install(rt) {
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 = target
if (file === undefined) {
- const topic = row.removedTopic === true && row.file.endsWith('.md') && row.file !== MAIN ? row.file.slice(0, -3) : undefined
- if (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`) }
+ // The file may still be there under another case: a file system that folds
+ // case reports row.file as existing and a fresh file would replace it,
+ // losing what was saved since. Folded names differ only in case, so the
+ // loaded variant is the same file; a sensitive file system keeps them
+ // apart and rebuilds. The journal names what is written, so a restore of
+ // this row finds it again.
+ if (existsSync(join(dirOf(scope), row.file))) {
+ file = files.find(item => item.file.toLowerCase() === row.file.toLowerCase())
+ if (file === undefined) throw noMatch(`A file named ${row.file} is already in ${label}; the entry that line ${line} took out can be restored by hand.`)
+ } else {
+ const topic = row.removedTopic === true && row.file.endsWith('.md') && row.file !== MAIN ? row.file.slice(0, -3) : undefined
+ if (topic === undefined) throw noMatch(`The file that line ${line} took the entry from is gone; restore it by hand.`)
+ // The rebuilt topic carries its name into a heading and the Index link:
+ // a name that is a saved secret now must not come back either.
+ if (secretEntry(topic)) throw new Error(`The topic that line ${line} removed cannot be put back: its name looks like a key, token, password, or ID number, which memory never keeps.`)
+ 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`) }
+ }
}
const lines = file.lines.length > 0 ? file.lines : parseMemory(`# ${file.topic}\n`).lines
const next = joinLines(insertEntry(lines, row.before))
@@ -1566,7 +1580,7 @@ export function install(rt) {
checkSize(main, index, label)
steps.push(writeStep(main.path, index))
}
- await publish({ op: 'add', scope: scopeKey(scope), steps, row: { op: 'add', ...base, after: row.before } })
+ await publish({ op: 'add', scope: scopeKey(scope), steps, row: { op: 'add', ...base, file: file.file, after: row.before } })
return { restored: row.id, op: 'add', scope: row.scope, file: row.file, text: splitMeta(row.before.replace(/^\s*[-*]\s+/, '')).text }
}
diff --git a/ds-bot/test/memory.test.js b/ds-bot/test/memory.test.js
index 5fab9558..9c7b21f1 100644
--- a/ds-bot/test/memory.test.js
+++ b/ds-bot/test/memory.test.js
@@ -614,6 +614,62 @@ test('a restore dedupes a row whose before line is an indented bullet', async (t
assert.match(read('bots', 'b2', 'MEMORY.md'), /^ {2}- Prefers aisle seats$/m)
})
+test('a restore puts a removed topic back into its case-folded file, never over it', async (t) => {
+ const { tool, call, read, journal, stop, home } = start()
+ t.after(() => stop())
+ // A topic file made by hand is removed whole with its last entry.
+ mkdirSync(join(home, 'memory', 'bots', 'b2'), { recursive: true })
+ writeFileSync(join(home, 'memory', 'bots', 'b2', 'Travel.md'), '# Travel\n\n- Prefers aisle seats [added: 2026-01-01]\n')
+ writeFileSync(join(home, 'memory', 'bots', 'b2', 'MEMORY.md'), '# Memory\n\n## Index\n- [[Travel]]\n')
+ await tool('forget', { entry: 'aisle seats' }, 'b2')
+ const line = journal().length
+ const dir = join(home, 'memory', 'bots', 'b2')
+ // Remembering under the same words makes travel.md; on a file system that
+ // folds case it is the file Travel.md named. A fresh Travel.md written over
+ // it would lose the new fact.
+ await tool('remember', { text: 'Flies on Fridays', topic: 'Travel' }, 'b2')
+ const folded = existsSync(join(dir, 'TRAVEL.MD'))
+ const restored = await call('memory-restore', { line })
+ assert.equal(restored.ok, true, JSON.stringify(restored))
+ if (folded) {
+ assert.match(read('bots', 'b2', 'travel.md'), /- Flies on Fridays \[/)
+ assert.match(read('bots', 'b2', 'travel.md'), /- Prefers aisle seats \[added: 2026-01-01\]/)
+ assert.equal(journal().at(-1).file, 'travel.md')
+ // Undoing the restore finds the line in the file that holds it.
+ const undone = await call('memory-restore', { line: journal().length })
+ assert.equal(undone.ok, true, JSON.stringify(undone))
+ assert.doesNotMatch(read('bots', 'b2', 'travel.md'), /Prefers aisle seats/)
+ } else {
+ // A case-sensitive file system keeps the two files apart: Travel.md is
+ // rebuilt next to travel.md.
+ assert.match(read('bots', 'b2', 'Travel.md'), /- Prefers aisle seats \[added: 2026-01-01\]/)
+ assert.match(read('bots', 'b2', 'travel.md'), /- Flies on Fridays \[/)
+ }
+})
+
+test('a restore refuses a removed topic whose name is a saved secret now', async (t) => {
+ const { tool, call, read, journal, stop, home } = start()
+ t.after(() => stop())
+ await tool('remember', { text: 'Meet at the dock', topic: 'harbor-dock-meeting-9f3k' }, 'b2')
+ await tool('forget', { entry: 'dock', topic: 'harbor-dock-meeting-9f3k' }, 'b2')
+ const line = journal().length
+ const lines = journal().length
+ const dir = join(home, 'memory', 'bots', 'b2')
+ assert.equal(existsSync(join(dir, 'harbor-dock-meeting-9f3k.md')), false)
+ // The name became a saved secret since the row was written: rebuilding the
+ // topic would write it into a heading and the Index link, in the clear.
+ const saved = await call('secret-set', { name: 'COVE_WORD', value: 'harbor-dock-meeting-9f3k' })
+ assert.equal(saved.ok, true, JSON.stringify(saved))
+ const denied = await call('memory-restore', { line })
+ assert.equal(denied.ok, false)
+ assert.match(denied.error.message, /cannot be put back/)
+ assert.doesNotMatch(denied.error.message, /harbor-dock-meeting-9f3k/)
+ assert.equal(existsSync(join(dir, 'harbor-dock-meeting-9f3k.md')), false)
+ assert.equal(existsSync(join(home, 'memory', 'pending.json')), false)
+ assert.equal(journal().length, lines)
+ assert.doesNotMatch(read('bots', 'b2', 'MEMORY.md'), /harbor-dock-meeting-9f3k/)
+})
+
test('recall finds a continuous Chinese phrase, and keeps space-separated and Latin matching', async (t) => {
const { tool, stop } = start()
t.after(() => stop())