A team of long-lived bots for DeepSeek Harness, with main bots, bot-to-bot messages and group chats.
Keep read-only saves non-fatal and log rollback failures
lulu feizhuc18ae16bcf86
diff --git a/ds-bot/src/host/roster.js b/ds-bot/src/host/roster.jsindex 9df0fc43..971bdc53 100644--- a/ds-bot/src/host/roster.js+++ b/ds-bot/src/host/roster.js@@ -108,7 +108,8 @@ export function install(rt) {delete state.bots[id]state.mainBotIds = state.mainBotIds.filter(other => other !== id)rt.chatOwners.delete(id)- await ctx.workspaceRegistry.archiveSession(id).catch(() => {})+ await ctx.workspaceRegistry.archiveSession(id)+ .catch(archiveError => rt.warn('ds-bot: archiving the Session for %s failed: %s', cleanName, archiveError))throw error}}@@ -268,7 +269,8 @@ export function install(rt) {} catch (error) {// The group's Session was already made: archive it instead of leaving an orphan.delete state.rooms[id]- await ctx.workspaceRegistry.archiveSession(id).catch(() => {})+ await ctx.workspaceRegistry.archiveSession(id)+ .catch(archiveError => rt.warn('ds-bot: archiving the Session for %s failed: %s', title, archiveError))throw error}}diff --git a/ds-bot/src/host/store.js b/ds-bot/src/host/store.jsindex 77d07b2f..1cea5ddf 100644--- a/ds-bot/src/host/store.js+++ b/ds-bot/src/host/store.js@@ -222,11 +222,7 @@ export function install(rt) {if (stopped) return writingindexSessions()// A reading host keeps the file's revision, so its window does not skip the next change.- if (!writable()) {- const error = new Error(readOnly())- error.code = 'bot/read-only'- return Promise.reject(error)- }+ if (!writable()) return writingstate.revision += 1state.exchanges = state.exchanges.slice(-MAX_EXCHANGES)const snapshot = JSON.stringify(state, null, 2)diff --git a/ds-bot/test/roster.test.js b/ds-bot/test/roster.test.jsindex 8d6d4f68..83eba63b 100644--- a/ds-bot/test/roster.test.js+++ b/ds-bot/test/roster.test.js@@ -1,5 +1,5 @@import assert from 'node:assert/strict'-import { mkdtempSync, readFileSync, rmSync, writeFileSync } from 'node:fs'+import { mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from 'node:fs'import { hostname, tmpdir } from 'node:os'import { join } from 'node:path'import { test } from 'node:test'@@ -307,7 +307,7 @@ test('a host that loses its lock to another dsh refuses changes instead of makinassert.equal(team.bots.some(bot => bot.name === 'Phantom'), false)})-test('a write refused at save time archives the Session it made', async (t) => {+test('a write that fails at save time archives the Session it made', async (t) => {const { call, stop, home, ctx } = start({ version: 1, mainBotIds: ['chief'], bots: { chief: { id: 'chief', name: 'Chief' } } })t.after(stop)await call('state', {})@@ -316,8 +316,8 @@ test('a write refused at save time archives the Session it made', async (t) => {ctx.sessionController.create = async (...args) => {const made = await create(...args)created.push(made.sessionId)- // The lock goes to another live dsh after this request's refresh, before its save.- takeover(home)+ // The disk write fails after this request's refresh, before its save.+ mkdirSync(join(home, `state.json.${process.pid}.tmp`))return made}const archived = []@@ -325,10 +325,9 @@ test('a write refused at save time archives the Session it made', async (t) => {ctx.workspaceRegistry.archiveSession = async (id) => { archived.push(id); return archive(id) }const answer = await call('create-bot', { name: 'Phantom' })assert.equal(answer.ok, false)- assert.equal(answer.error.code, 'bot/read-only')+ assert.equal(answer.error.code, 'bot/internal')assert.deepEqual(archived, created)const team = (await call('state', {})).value- assert.match(team.readOnly, /another dsh/)assert.equal(team.bots.some(bot => bot.name === 'Phantom'), false)const saved = JSON.parse(readFileSync(join(home, 'state.json'), 'utf8'))assert.equal(Object.values(saved.bots).some(bot => bot.name === 'Phantom'), false)diff --git a/ds-bot/test/store.test.js b/ds-bot/test/store.test.jsindex 3c0a8a2b..44176c18 100644--- a/ds-bot/test/store.test.js+++ b/ds-bot/test/store.test.js@@ -112,7 +112,7 @@ test('malformed state stays read-only when its backup cannot be copied', async (await rt.load()assert.match(rt.readOnly(), /could not be read/)rt.state.prefs.marker = 'not saved'- await assert.rejects(rt.save(), { code: 'bot/read-only' })+ await rt.save()assert.equal(fs.readFileSync(statePath, 'utf8'), original)assert.deepEqual(fs.readdirSync(rt.home).filter(name => name.startsWith('state.json.broken-')), [])} finally {@@ -126,7 +126,7 @@ test('a state from a newer version remains untouched', async (t) => {await rt.load()assert.match(rt.readOnly(), /saved by a newer DS Bot; update DS Bot/)rt.state.prefs.marker = 'not saved'- await assert.rejects(rt.save(), { code: 'bot/read-only' })+ await rt.save()assert.equal(fs.readFileSync(statePath, 'utf8'), original)})@@ -143,7 +143,7 @@ test('a read error keeps the existing state read-only', async (t) => {restore()}assert.match(rt.readOnly(), /could not be read \(EACCES\)/)- await assert.rejects(rt.save(), { code: 'bot/read-only' })+ await rt.save()assert.equal(fs.readFileSync(statePath, 'utf8'), original)})@@ -172,7 +172,7 @@ test('an unusable state lock fails closed', async (t) => {await rt.load()assert.match(rt.readOnly(), /cannot lock the Bot team.*EACCES/)rt.state.prefs.marker = 'not saved'- await assert.rejects(rt.save(), { code: 'bot/read-only' })+ await rt.save()assert.equal(fs.existsSync(statePath), false)} finally {restore()@@ -215,6 +215,20 @@ test('a queued save rejects after another host takes the lock', async (t) => {}})+test('a read-only host resolves awaited saves without writing', async (t) => {+ const { rt, home, statePath } = runtime(t)+ await rt.load()+ await rt.save()+ fs.writeFileSync(join(home, 'state.lock'), JSON.stringify({ pid: process.ppid, host: hostname(), token: 'new-owner' }))+ const ownerState = JSON.stringify({ version: 1, revision: 900, prefs: { marker: 'new owner' } })+ fs.writeFileSync(statePath, ownerState)+ await rt.refresh()+ assert.match(rt.readOnly(), /another dsh/)+ rt.state.prefs.marker = 'not saved'+ await rt.save()+ assert.equal(fs.readFileSync(statePath, 'utf8'), ownerState)+})+test('dispose waits for a pending save and prevents later writes', async (t) => {const { rt, home, statePath, dispose } = runtime(t)await rt.load()