A team of long-lived bots for DeepSeek Harness, with main bots, bot-to-bot messages and group chats.
Check the whole restored line for secrets, and flat names for control characters
klarkxye0c425f00baf
diff --git a/ds-bot/src/host/memory.js b/ds-bot/src/host/memory.jsindex 65095868..48b676a0 100644--- a/ds-bot/src/host/memory.js+++ b/ds-bot/src/host/memory.js@@ -1476,9 +1476,10 @@ export function install(rt) {const noMatch = message => Object.assign(new Error(message), { code: NO_MATCH })// A row's file is a flat name — a file of the scope's folder, never a path. A// row is read, not trusted: a name with separators would reach another scope's- // folder when it is joined back for a rebuild, inside() or not. The name does- // not have to be a cleanTopic name: files renamed or created by hand still list.- const flatFile = name => name === MAIN || (typeof name === 'string' && name.endsWith('.md') && !/[/\\:]/.test(name))+ // folder when it is joined back for a rebuild, inside() or not, and a control+ // character would make the write fail after the pending record was accepted.+ // The name does not have to be a cleanTopic name: hand-renamed files still list.+ const flatFile = name => name === MAIN || (typeof name === 'string' && name.endsWith('.md') && !/[/\\:]/.test(name) && [...name].every(ch => ch >= ' ' && ch !== '\x7f'))// The line a row puts back is one whole entry, the same bullets parseMemory// reads: a heading or a line with a newline in it would corrupt the file and// make what follows it vanish from recall.@@ -1532,7 +1533,7 @@ export function install(rt) {steps.push(writeStep(target.path, joinLines(lines)))}await publish({ op: 'remove', scope: scopeKey(scope), steps, row: { op: 'remove', ...base, before: row.after, ...(empty ? { removedTopic: true } : {}) } })- return { restored: row.id, op: 'remove', scope: row.scope, file: row.file, text: splitMeta(row.after.replace(/^[-*]\s+/, '')).text }+ return { restored: row.id, op: 'remove', scope: row.scope, file: row.file, text: splitMeta(row.after.replace(/^\s*[-*]\s+/, '')).text }}if (row.op === 'remove') {@@ -1540,7 +1541,10 @@ 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 (!entryLine(row.before)) 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+ // The whole line, metadata included, must still be keepable: a value+ // anywhere in it may be a saved secret since the row was written.+ if (secretEntry(row.before)) throw new Error(`The entry that line ${line} removed cannot be put back: it looks like a key, token, password, or ID number, which memory never keeps.`)+ const back = splitMeta(row.before.replace(/^\s*[-*]\s+/, '')).textconst 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))@@ -1563,11 +1567,13 @@ export function install(rt) {steps.push(writeStep(main.path, index))}await publish({ op: 'add', scope: scopeKey(scope), steps, row: { op: 'add', ...base, after: row.before } })- return { restored: row.id, op: 'add', scope: row.scope, file: row.file, text: splitMeta(row.before.replace(/^[-*]\s+/, '')).text }+ return { restored: row.id, op: 'add', scope: row.scope, file: row.file, text: splitMeta(row.before.replace(/^\s*[-*]\s+/, '')).text }}// row.op === 'replace': the recorded after-line is put back to before.if (!entryLine(row.before)) throw new Error(`Line ${line} of the change log is not a change that can be restored.`)+ // The whole before-line, metadata included, must still be keepable.+ if (secretEntry(row.before)) throw new Error(`The entry that line ${line} changed cannot be put back: it looks like a key, token, password, or ID number, which memory never keeps.`)if (target === undefined || typeof row.after !== 'string') {throw noMatch(`The entry that line ${line} changed is not where the change log left it; it may have changed or been removed since.`)}@@ -1576,7 +1582,7 @@ 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 back = splitMeta(row.before.replace(/^[-*]\s+/, '')).text+ const back = splitMeta(row.before.replace(/^\s*[-*]\s+/, '')).textconst 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,@@ -1589,7 +1595,7 @@ export function install(rt) {const next = joinLines(lines)checkSize(target, next, label)await publish({ op: 'replace', scope: scopeKey(scope), steps: [writeStep(target.path, next)], row: { op: 'replace', ...base, before: row.after, after: row.before } })- return { restored: row.id, op: 'replace', scope: row.scope, file: row.file, text: splitMeta(row.before.replace(/^[-*]\s+/, '')).text }+ return { restored: row.id, op: 'replace', 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.jsindex e80e9604..5fab9558 100644--- a/ds-bot/test/memory.test.js+++ b/ds-bot/test/memory.test.js@@ -532,6 +532,88 @@ test('a restore follows a hand-named topic file', async (t) => {assert.match(read('bots', 'b2', 'Travel.md'), /- Prefers aisle seats \[added: 2026-01-01\]/)})+test('a restore checks the whole line for a saved secret, its metadata too', async (t) => {+ const { tool, call, read, journal, stop, home } = start()+ t.after(() => stop())+ await tool('remember', { text: 'Likes tea' }, 'b2')+ await tool('remember', { text: 'Lives in Hangzhou' }, 'b2')+ const secret = 'harbor dock meeting 9f3k'+ const saved = await call('secret-set', { name: 'DOCK_WORD', value: secret })+ assert.equal(saved.ok, true, JSON.stringify(saved))+ const journalPath = join(home, 'memory', 'journal.jsonl')+ // The value sits in the line's metadata, not its text: the text-only check a+ // new entry gets never sees it, but putting the line back would write it.+ const forge = (row) => {+ writeFileSync(journalPath, `${read('journal.jsonl')}${JSON.stringify(row)}\n`)+ return journal().length+ }+ for (const op of ['remove', 'replace']) {+ const before = `- Prefers window seats [note: ${secret}]`+ const row = { id: `sec-${op}`, time: new Date().toISOString(), op, scope: 'bot:b2', file: 'MEMORY.md', actor: 'user', actorName: 'user', before }+ if (op === 'replace') row.after = journal()[1].after+ const denied = await call('memory-restore', { line: forge(row) })+ assert.equal(denied.ok, false, op)+ assert.match(denied.error.message, /cannot be put back/, op)+ assert.doesNotMatch(denied.error.message, new RegExp(secret))+ }+ assert.doesNotMatch(read('bots', 'b2', 'MEMORY.md'), /harbor dock meeting|window seats/)+ assert.doesNotMatch(read('journal.jsonl'), /harbor dock meeting 9f3k.*"op":"add"/s)+})++test('a restore refuses a file name with a control character instead of wedging', async (t) => {+ const { tool, call, read, journal, stop, home } = start()+ t.after(() => stop())+ await tool('remember', { text: 'Likes tea' }, 'b2')+ const journalPath = join(home, 'memory', 'journal.jsonl')+ // The name passes a separators-only check, but a write of the file it names+ // fails after the pending record is accepted, so every later memory change+ // stops on it. The row is refused before any of that is reached.+ const row = { id: 'bad-ctl', time: new Date().toISOString(), op: 'remove', scope: 'bot:b2', file: 'bad\0.md', actor: 'user', actorName: 'user', removedTopic: true, before: '- forged entry' }+ writeFileSync(journalPath, `${read('journal.jsonl')}${JSON.stringify(row)}\n`)+ const denied = await call('memory-restore', { line: journal().length })+ assert.equal(denied.ok, false)+ assert.match(denied.error.message, /not a change that can be restored/)+ assert.equal(existsSync(join(home, 'memory', 'pending.json')), false)+ assert.match(read('bots', 'b2', 'MEMORY.md'), /- Likes tea \[/)+ assert.equal(journal().length, 2)+ // The memory still works: nothing half-written is waiting to be finished.+ assert.equal((await call('memory-view', { botId: 'b2' })).ok, true)+ assert.match(await tool('remember', { text: 'Also likes coffee' }, 'b2'), /Saved to your memory/)+ assert.match(read('bots', 'b2', 'MEMORY.md'), /- Also likes coffee \[/)+})++test('a restore dedupes a row whose before line is an indented bullet', async (t) => {+ const { tool, call, read, journal, stop, home } = start()+ t.after(() => stop())+ await tool('remember', { text: 'Drinks tea daily' }, 'b2')+ await tool('remember', { text: 'Lives in Hangzhou' }, 'b2')+ const journalPath = join(home, 'memory', 'journal.jsonl')+ // Rows are written by publish with one bullet, but a hand-edited or foreign+ // row may indent it; the text check and the duplicate check must read it as+ // the same entry either way.+ const forge = (row) => {+ writeFileSync(journalPath, `${read('journal.jsonl')}${JSON.stringify(row)}\n`)+ return journal().length+ }+ const removeLine = forge({ id: 'ind-rm', time: new Date().toISOString(), op: 'remove', scope: 'bot:b2', file: 'MEMORY.md', actor: 'user', actorName: 'user', before: ' - Drinks tea daily [added: 2026-01-01]' })+ const denied = await call('memory-restore', { line: removeLine })+ assert.equal(denied.ok, false)+ assert.match(denied.error.message, /already in/)+ const replaceLine = forge({ id: 'ind-rp', time: new Date().toISOString(), op: 'replace', scope: 'bot:b2', file: 'MEMORY.md', actor: 'user', actorName: 'user', before: ' - Drinks tea daily', after: journal()[1].after })+ const deniedReplace = await call('memory-restore', { line: replaceLine })+ assert.equal(deniedReplace.ok, false)+ assert.match(deniedReplace.error.message, /already in/)+ assert.match(read('bots', 'b2', 'MEMORY.md'), /- Lives in Hangzhou \[/)+ assert.equal(parseMemory(read('bots', 'b2', 'MEMORY.md')).entries.length, 2)+ // An indented before that is no duplicate still restores, and reports the+ // entry's text, not the line's raw shape.+ const clean = forge({ id: 'ind-ok', time: new Date().toISOString(), op: 'remove', scope: 'bot:b2', file: 'MEMORY.md', actor: 'user', actorName: 'user', before: ' - Prefers aisle seats' })+ const restored = await call('memory-restore', { line: clean })+ assert.equal(restored.ok, true, JSON.stringify(restored))+ assert.equal(restored.value.text, 'Prefers aisle seats')+ assert.match(read('bots', 'b2', 'MEMORY.md'), /^ {2}- Prefers aisle seats$/m)+})+test('recall finds a continuous Chinese phrase, and keeps space-separated and Latin matching', async (t) => {const { tool, stop } = start()t.after(() => stop())