A team of long-lived bots for DeepSeek Harness, with main bots, bot-to-bot messages and group chats.
Count physical change-log lines and refuse non-entry lines on restore
klarkxy64be9492ed77
diff --git a/ds-bot/src/host/memory.js b/ds-bot/src/host/memory.jsindex bc8916ed..65095868 100644--- a/ds-bot/src/host/memory.js+++ b/ds-bot/src/host/memory.js@@ -1474,10 +1474,15 @@ export function install(rt) {// less than a whole line would undo a different change than the one it reads.const exactLines = (file, line) => file.entries.filter(entry => file.lines[entry.line] === line)const noMatch = message => Object.assign(new Error(message), { code: NO_MATCH })- // A row's file is a flat name — MEMORY.md or a topic file, 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.- const flatFile = name => name === MAIN || (typeof name === 'string' && name.endsWith('.md') && cleanTopic(name) === name.slice(0, -3))+ // 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))+ // 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.+ const entryLine = line => typeof line === 'string' && !/[\r\n]/.test(line) && /^\s*[-*]\s+\S/.test(line)async function memoryRestore(body) {const line = Number(body.line)@@ -1487,7 +1492,7 @@ export function install(rt) {// `botId` picks a private Bot's own log; every other change is in the shared// log, which journalFor resolves for a public or unknown Bot too.const { path } = journalFor(body.botId === undefined ? 'team' : `bot:${body.botId}`, 'restore')- const raw = readJournal(path).split('\n').filter(row => row !== '')[line - 1]+ const raw = readJournal(path).split('\n')[line - 1]let rowtry { row = raw === undefined ? undefined : JSON.parse(raw) } catch { row = null }if (row === undefined || row === null || typeof row !== 'object') {@@ -1534,7 +1539,7 @@ export function install(rt) {// Undoing a remove puts the line back. The original place is not in the// 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.`)+ 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+/, '')).textconst problem = entryProblem(back)if (problem !== undefined) throw new Error(`The entry that line ${line} removed cannot be put back: ${problem}`)@@ -1562,7 +1567,8 @@ export function install(rt) {}// row.op === 'replace': the recorded after-line is put back to before.- if (target === undefined || typeof row.before !== 'string' || typeof row.after !== 'string') {+ if (!entryLine(row.before)) throw new Error(`Line ${line} of the change log is not a change that can be restored.`)+ 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.`)}const found = exactLines(target, row.after)diff --git a/ds-bot/test/memory.test.js b/ds-bot/test/memory.test.jsindex d436bb3d..e80e9604 100644--- a/ds-bot/test/memory.test.js+++ b/ds-bot/test/memory.test.js@@ -464,6 +464,74 @@ test('a restore refuses a change-log row whose file is not a flat name', async (}})+test('a restore line counts physical lines of the change log, blanks included', async (t) => {+ const { tool, call, read, stop, home } = start()+ t.after(() => stop())+ await tool('remember', { text: 'Fact A' }, 'b2')+ await tool('remember', { text: 'Fact B' }, 'b2')+ await tool('forget', { entry: 'Fact A' }, 'b2')+ const journalPath = join(home, 'memory', 'journal.jsonl')+ const physical = () => read('journal.jsonl').split('\n')++ // A blank first line does not renumber the rows: line 2 is the remove of A,+ // not the add of B, so restoring it cannot delete B.+ writeFileSync(journalPath, `\n${read('journal.jsonl')}`)+ const blank = await call('memory-restore', { line: 1 })+ assert.equal(blank.ok, false)+ assert.match(blank.error.message, /not a change that can be restored/)+ const removed = await call('memory-restore', { line: 2 })+ assert.equal(removed.ok, false)+ assert.match(removed.error.message, /not where the change log left it|no longer in/)+ assert.match(read('bots', 'b2', 'MEMORY.md'), /- Fact B \[/)+ assert.doesNotMatch(read('bots', 'b2', 'MEMORY.md'), /Fact A/)++ // The add of A is the third physical line: restoring it takes A out again.+ const undone = await call('memory-restore', { line: 3 })+ assert.equal(undone.ok, true, JSON.stringify(undone))+ assert.doesNotMatch(read('bots', 'b2', 'MEMORY.md'), /Fact A/)+ // The restore rows appended behind still count physical lines.+ assert.equal(physical().filter(line => line !== '').length, 4)+})++test('a restore refuses a row whose before is not one entry line', async (t) => {+ const { tool, call, read, journal, stop, home } = start()+ t.after(() => stop())+ await tool('remember', { text: 'Real fact' }, 'b2')+ const journalPath = join(home, 'memory', 'journal.jsonl')+ const forge = (row) => {+ writeFileSync(journalPath, `${read('journal.jsonl')}${JSON.stringify(row)}\n`)+ return journal().length+ }+ // A heading or several lines put back would corrupt the file or hide what+ // follows from recall; a real remove row always held one bullet.+ for (const before of ['## Index', '- one\n- two', 'plain text', '* ', '']) {+ const removeLine = forge({ id: `rm-${JSON.stringify(before)}`, time: new Date().toISOString(), op: 'remove', scope: 'bot:b2', file: 'MEMORY.md', actor: 'user', actorName: 'user', before })+ const denied = await call('memory-restore', { line: removeLine })+ assert.equal(denied.ok, false, JSON.stringify(before))+ assert.match(denied.error.message, /not a change that can be restored/, JSON.stringify(before))+ const replaceLine = forge({ id: `rp-${JSON.stringify(before)}`, time: new Date().toISOString(), op: 'replace', scope: 'bot:b2', file: 'MEMORY.md', actor: 'user', actorName: 'user', before, after: journal()[0].after })+ const deniedReplace = await call('memory-restore', { line: replaceLine })+ assert.equal(deniedReplace.ok, false, JSON.stringify(before))+ assert.match(deniedReplace.error.message, /not a change that can be restored/, JSON.stringify(before))+ }+ assert.match(read('bots', 'b2', 'MEMORY.md'), /- Real fact \[/)+})++test('a restore follows a hand-named topic file', async (t) => {+ const { tool, call, read, journal, stop, home } = start()+ t.after(() => stop())+ // A file renamed or created by hand still lists as a topic; a replace that+ // finds its entry journals the name as it is, not a cleanTopic form.+ 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')+ assert.match(await tool('remember', { text: 'Prefers window seats', replace: 'aisle' }, 'b2'), /is now "Prefers window seats"/)+ assert.equal(journal().at(-1).file, 'Travel.md')+ const restored = await call('memory-restore', { line: journal().length })+ assert.equal(restored.ok, true, JSON.stringify(restored))+ assert.match(read('bots', 'b2', 'Travel.md'), /- Prefers aisle seats \[added: 2026-01-01\]/)+})+test('recall finds a continuous Chinese phrase, and keeps space-separated and Latin matching', async (t) => {const { tool, stop } = start()t.after(() => stop())