Skip to content

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

Write through a short temporary name, and keep journaled lines out of secrets

klarkxyda6e449a0a2e
diff --git a/ds-bot/src/host/memory.js b/ds-bot/src/host/memory.js
index 0284275a..f928ec4e 100644
--- a/ds-bot/src/host/memory.js
+++ b/ds-bot/src/host/memory.js
@@ -27,7 +27,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 { basename, dirname, join } from 'node:path'
import { BlockAssembler, HarnessError } from '@deepseek-ai/dsh-llm'
import { defineTool } from '@deepseek-ai/dsh-tools'
import { replaceFile } from './files.js'
@@ -535,7 +535,10 @@ export function install(rt) {
async function writeText(path, text) {
await mkdir(dirname(path), { recursive: true })
- const temp = `${path}.${process.pid}.tmp`
+ // The temporary name sits next to the file and stays short however long the
+ // file's own name is: a legal name near the system's per-name limit must
+ // still write, and one made of the whole path would not.
+ const temp = join(dirname(path), `.${basename(path).slice(0, 32)}.${process.pid}.${randomUUID()}.tmp`)
await writeFile(temp, text, 'utf8')
await replaceFile(temp, path)
}
@@ -1523,6 +1526,9 @@ export function install(rt) {
const found = exactLines(target, row.after)
if (found.length === 0) throw noMatch(`The entry that line ${line} added is no longer in ${label}; it may have changed or been removed since.`)
if (found.length > 1) throw noMatch(`The line that line ${line} added is in ${label} more than once; restore it by hand so the wrong one is not removed.`)
+ // The line comes out of the file but goes into the change log as this
+ // row's before: a value that became a saved secret since is not journaled.
+ if (secretEntry(row.after)) throw new Error(`The entry that line ${line} added cannot be taken out: it looks like a key, token, password, or ID number, which memory never keeps.`)
const lines = target.lines.filter((_line, index) => index !== found[0].line)
const empty = target.topic !== undefined && lines.every(item => item.trim() === '' || HEADING.test(item))
if (empty) {
@@ -1604,6 +1610,9 @@ export function install(rt) {
// 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.`)
+ // The recorded after-line is journaled as this row's before, so it faces
+ // the same check the line going back did.
+ if (secretEntry(row.after)) throw new Error(`The entry that line ${line} wrote cannot be taken out: it looks like a key, token, password, or ID number, which memory never keeps.`)
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 9c7b21f1..3e3004f4 100644
--- a/ds-bot/test/memory.test.js
+++ b/ds-bot/test/memory.test.js
@@ -670,6 +670,59 @@ test('a restore refuses a removed topic whose name is a saved secret now', async
assert.doesNotMatch(read('bots', 'b2', 'MEMORY.md'), /harbor-dock-meeting-9f3k/)
})
+test('a restore rebuilds a removed topic whose file name is near the name limit', async (t) => {
+ const { tool, call, read, journal, stop, home } = start()
+ t.after(() => stop())
+ const dir = join(home, 'memory', 'bots', 'b2')
+ mkdirSync(dir, { recursive: true })
+ for (const stem of ['a'.repeat(250), 'a'.repeat(251)]) {
+ // The topic's own name is legal; a temporary name made of the whole path
+ // beside it would pass the system's per-name limit and the write would fail
+ // after the pending record accepted the change, wedging every later one.
+ writeFileSync(join(dir, `${stem}.md`), `# ${stem}\n\n- A fact kept for ${stem.length} characters [added: 2026-01-01]\n`)
+ writeFileSync(join(dir, 'MEMORY.md'), `# Memory\n\n## Index\n- [[${stem}]]\n`)
+ assert.match(await tool('forget', { entry: `${stem.length} characters` }, 'b2'), /was empty/)
+ const restored = await call('memory-restore', { line: journal().length })
+ assert.equal(restored.ok, true, JSON.stringify(restored))
+ assert.match(read('bots', 'b2', `${stem}.md`), /- A fact kept for /)
+ assert.equal(existsSync(join(home, 'memory', 'pending.json')), false)
+ // Nothing half-written: the memory still reads and writes.
+ assert.equal((await call('memory-view', { botId: 'b2' })).ok, true)
+ assert.match(await tool('remember', { text: 'Still works' }, 'b2'), /Saved to your memory/)
+ }
+})
+
+test('a restore does not journal a line that is a saved secret now', async (t) => {
+ const { tool, call, read, journal, stop, home } = start()
+ t.after(() => stop())
+ await tool('remember', { text: 'Lives in Hangzhou' }, 'b2')
+ const addLine = journal().length
+ await tool('remember', { text: 'Likes tea' }, 'b2')
+ await tool('remember', { text: 'Likes green tea', replace: 'tea' }, 'b2')
+ const replaceLine = journal().length
+ const rows = journal().length
+ // Each restore copies the line its row wrote into a row of its own: a value
+ // that became a saved secret since must not come back to the change log.
+ const kept = await call('secret-set', { name: 'HOME_CITY', value: 'Lives in Hangzhou' })
+ assert.equal(kept.ok, true, JSON.stringify(kept))
+ const keptTea = await call('secret-set', { name: 'TEA_CHOICE', value: 'Likes green tea' })
+ assert.equal(keptTea.ok, true, JSON.stringify(keptTea))
+
+ const undoAdd = await call('memory-restore', { line: addLine })
+ assert.equal(undoAdd.ok, false)
+ assert.match(undoAdd.error.message, /cannot be taken out/)
+ assert.doesNotMatch(undoAdd.error.message, /Lives in Hangzhou/)
+ const undoReplace = await call('memory-restore', { line: replaceLine })
+ assert.equal(undoReplace.ok, false)
+ assert.match(undoReplace.error.message, /cannot be taken out/)
+ assert.doesNotMatch(undoReplace.error.message, /Likes green tea/)
+
+ assert.equal(journal().length, rows)
+ assert.match(read('bots', 'b2', 'MEMORY.md'), /- Lives in Hangzhou \[/)
+ assert.match(read('bots', 'b2', 'MEMORY.md'), /- Likes green tea \[/)
+ assert.equal(existsSync(join(home, 'memory', 'pending.json')), false)
+})
+
test('recall finds a continuous Chinese phrase, and keeps space-separated and Latin matching', async (t) => {
const { tool, stop } = start()
t.after(() => stop())