A team of long-lived bots for DeepSeek Harness, with main bots, bot-to-bot messages and group chats.
Stop a deleted Bot's or group's Sessions even when the save fails
FeiZhuLulu3663e87e5f5a
diff --git a/ds-bot/src/host/roster.js b/ds-bot/src/host/roster.jsindex 309173c3..8e4a5da8 100644--- a/ds-bot/src/host/roster.js+++ b/ds-bot/src/host/roster.js@@ -108,6 +108,9 @@ export function install(rt) {delete state.bots[id]state.mainBotIds = state.mainBotIds.filter(other => other !== id)rt.chatOwners.delete(id)+ // A save that another change queued meanwhile still carries this Bot; a later+ // one writes the team without it.+ void rt.save().catch(() => {})await ctx.workspaceRegistry.archiveSession(id).catch(archiveError => rt.warn('ds-bot: archiving the Session for %s failed: %s', cleanName, archiveError))throw error@@ -269,6 +272,7 @@ export function install(rt) {} catch (error) {// The group's Session was already made: archive it instead of leaving an orphan.delete state.rooms[id]+ void rt.save().catch(() => {})await ctx.workspaceRegistry.archiveSession(id).catch(archiveError => rt.warn('ds-bot: archiving the Session for %s failed: %s', title, archiveError))throw error@@ -346,11 +350,12 @@ export function install(rt) {for (const botId of Object.keys(room.sessions ?? {})) rt.dropGroupSession(room, botId)delete state.rooms[id]rt.roomInbox.delete(id)- await rt.save()+ const failed = await saveForDelete()// Armed scheduled tasks refuse a plain archive and would keep delivering into// the deleted group; the session-stop that stopActivity runs removes them.await ctx.workspaceRegistry.archiveSession(id, { stopActivity: true }).catch(error => rt.warn('ds-bot: group chat %s not archived: %s', room.name, error))+ if (failed !== undefined) throw failed}// The first Main Bot is the sidebar tile. `replace` gives a Main Bot's place to@@ -409,7 +414,7 @@ export function install(rt) {await followMemberNames(touched)await rt.forgetBotSecrets(id)await archived- await rt.save()+ const failed = await saveForDelete()// Same as deleteRoom: stopActivity removes the Session's armed scheduled tasks.// Past parts are archived too: a part move can leave tasks on the old Session,// which otherwise stays armed and invisible. Already-archived ids are a no-op.@@ -417,6 +422,21 @@ export function install(rt) {await ctx.workspaceRegistry.archiveSession(part, { stopActivity: true }).catch(error => rt.warn('ds-bot: Bot %s not archived: %s', bot.name, error))}+ if (failed !== undefined) throw failed+ }++ // A deletion whose save fails stays in memory, and the next save writes it, so its+ // Sessions are stopped anyway: left armed, their scheduled tasks would keep running+ // with nothing in the team to show or delete them. Only a host that lost the team+ // to another dsh keeps them, since the other host's copy still has them.+ async function saveForDelete() {+ try {+ await rt.save()+ return undefined+ } catch (error) {+ if (error?.code === 'ELOCKLOST' || rt.readOnly() !== undefined) throw error+ return error+ }}async function bootstrap() {diff --git a/ds-bot/test/roster.test.js b/ds-bot/test/roster.test.jsindex ca4cf91c..2b68ff81 100644--- a/ds-bot/test/roster.test.js+++ b/ds-bot/test/roster.test.js@@ -1,5 +1,7 @@import assert from 'node:assert/strict'import { mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from 'node:fs'+import fsp from 'node:fs/promises'+import { syncBuiltinESMExports } from 'node:module'import { hostname, tmpdir } from 'node:os'import { join } from 'node:path'import { test } from 'node:test'@@ -368,6 +370,65 @@ 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 delete whose save fails still stops the Sessions, and the next save writes it', async (t) => {+ const { call, stop, home, archived } = start(TEAM_WITH_PART, { models: ['deepseek-v4-flash'] })+ t.after(stop)+ await call('state', {})+ archived.length = 0+ const blocker = join(home, `state.json.${process.pid}.tmp`)+ mkdirSync(blocker)+ const answer = await call('delete-bot', { id: 'w1' })+ assert.equal(answer.ok, false)+ // The Bot is gone from the team, so nothing could stop its tasks later.+ assert.deepEqual(archived, [+ ['w1', { stopActivity: true }],+ ['w2', { stopActivity: true }],+ ])+ rmSync(blocker, { recursive: true })+ await call('set-prefs', { theme: 'dark' })+ const saved = JSON.parse(readFileSync(join(home, 'state.json'), 'utf8'))+ assert.equal(saved.bots.w1, undefined)+})++test('a rolled-back Bot does not come back with a save queued before the rollback', async (t) => {+ const { call, stop, home } = start({ version: 1, mainBotIds: ['chief'], bots: { chief: { id: 'chief', name: 'Chief' } } })+ t.after(async () => {+ fsp.rename = rename+ syncBuiltinESMExports()+ stop()+ })+ // The first Bot also saves the team's workspace; later ones save only themselves.+ await call('create-bot', { name: 'Warm' })+ const statePath = join(home, 'state.json')+ const rename = fsp.rename+ let entered+ let release+ const first = new Promise(resolve => { entered = resolve })+ const gate = new Promise(resolve => { release = resolve })+ let failed = false+ fsp.rename = async (from, to) => {+ if (to === statePath && !failed) {+ failed = true+ entered()+ await gate+ throw Object.assign(new Error('rename failed'), { code: 'EIO' })+ }+ return rename(from, to)+ }+ syncBuiltinESMExports()+ const phantom = call('create-bot', { name: 'Phantom' })+ await first+ // A second Bot's save queues a snapshot that still holds Phantom.+ const kept = call('create-bot', { name: 'Kept' })+ await settle()+ release()+ assert.equal((await phantom).ok, false)+ assert.equal((await kept).ok, true)+ await settle()+ const names = Object.values(JSON.parse(readFileSync(statePath, 'utf8')).bots).map(bot => bot.name)+ assert.deepEqual(names.sort(), ['Chief', 'Kept', 'Warm'])+})+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)