A team of long-lived bots for DeepSeek Harness, with main bots, bot-to-bot messages and group chats.
Withdraw surface groups safely when a disposer throws
Fei241165c25acd7804
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex be0739b5..908bb447 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -4,6 +4,10 @@ All notable changes to DS Bot are listed here. The format follows [Keep a Change## [Unreleased]+### Fixed++- Switching from Bot mode back to Agent mode no longer leaves parts of the Bot interface on screen, such as the chat header or Bot messages without their styles, when one part fails to close. The other parts still close, and Bot mode opens normally again.+## [0.1.4] - 2026-10-13### Addeddiff --git a/ds-bot/client.js b/ds-bot/client.jsindex ea2b7877..b852abec 100644--- a/ds-bot/client.js+++ b/ds-bot/client.js@@ -12797,6 +12797,40 @@ function holdFlatTranscript(form) {function groupsFor({ mode, plain = false }) {return { shell: mode === "bot", conversation: mode === "bot" && !plain };}+var GROUPS = ["shell", "conversation"];+function disposeAll(disposers, label) {+ for (const dispose of [...disposers].reverse()) {+ try {+ dispose?.();+ } catch (error) {+ console.warn(`[ds-bot] ${label}`, error);+ }+ }+}+function surfaceGroups(install) {+ const mounted = { shell: [], conversation: [] };+ const withdraw = (group) => {+ const disposers = mounted[group];+ mounted[group] = [];+ disposeAll(disposers, `withdraw ${group}`);+ };+ return {+ reconcile(wanted) {+ for (const group of [...GROUPS].reverse()) if (!wanted[group]) withdraw(group);+ for (const group of GROUPS) {+ if (!wanted[group] || mounted[group].length > 0) continue;+ try {+ mounted[group] = install[group]();+ } catch (error) {+ console.warn(`[ds-bot] install ${group}`, error);+ }+ }+ },+ dispose() {+ for (const group of [...GROUPS].reverse()) withdraw(group);+ }+ };+}function readMode(storage = globalThis.localStorage) {try {return storage.getItem(STORAGE.surface) === "agent" ? "agent" : "bot";@@ -13246,9 +13280,12 @@ function apply(ctx) {return () => {offRoster();offRegistry();- if (typeof releaseTokens === "function") releaseTokens();- delete document.body.dataset.btMotion;- style.remove();+ try {+ if (typeof releaseTokens === "function") releaseTokens();+ } finally {+ delete document.body.dataset.btMotion;+ style.remove();+ }};};const installBubbleTokens = () => {@@ -13852,26 +13889,13 @@ function apply(ctx) {};}, "ds-bot: agent surface");ctx.effect(() => {- const mounted = { shell: [], conversation: [] };- const reconcile = () => {- const wanted = groupsFor(surface.getSnapshot());- for (const group of ["shell", "conversation"]) {- if (wanted[group] === mounted[group].length > 0) continue;- if (wanted[group]) mounted[group] = (group === "shell" ? installShell : installConversation)();- else {- for (const dispose of mounted[group]) dispose?.();- mounted[group] = [];- }- }- };+ const groups = surfaceGroups({ shell: installShell, conversation: installConversation });+ const reconcile = () => groups.reconcile(groupsFor(surface.getSnapshot()));reconcile();const unsub = surface.subscribe(reconcile);return () => {unsub();- for (const group of ["shell", "conversation"]) {- for (const dispose of mounted[group]) dispose?.();- mounted[group] = [];- }+ groups.dispose();};}, "ds-bot: surface");}diff --git a/ds-bot/src/client/index.js b/ds-bot/src/client/index.jsindex e14e7191..1c520fd2 100644--- a/ds-bot/src/client/index.js+++ b/ds-bot/src/client/index.js@@ -25,7 +25,7 @@ import { GROUP_SETTINGS_CSS } from './group-settings.js'import { USAGE_CSS } from './usage-page.js'import { SCHEDULE_CSS } from './schedules.js'import { STORAGE, migrateStorage } from './storage.js'-import { groupsFor, readMode, writeMode } from './surface.js'+import { groupsFor, readMode, surfaceGroups, writeMode } from './surface.js'import { installLocale, language } from './i18n.js'import { noteClientError, quietly } from './diagnostics.js'import { ErrorToast, FEEDBACK_CSS } from './feedback.js'@@ -299,9 +299,12 @@ export function apply(ctx) {return () => {offRoster()offRegistry()- if (typeof releaseTokens === 'function') releaseTokens()- delete document.body.dataset.btMotion- style.remove()+ try {+ if (typeof releaseTokens === 'function') releaseTokens()+ } finally {+ delete document.body.dataset.btMotion+ style.remove()+ }}}@@ -889,20 +892,13 @@ export function apply(ctx) {}, 'ds-bot: agent surface')ctx.effect(() => {- const mounted = { shell: [], conversation: [] }- const reconcile = () => {- const wanted = groupsFor(surface.getSnapshot())- for (const group of ['shell', 'conversation']) {- if (wanted[group] === (mounted[group].length > 0)) continue- if (wanted[group]) mounted[group] = (group === 'shell' ? installShell : installConversation)()- else { for (const dispose of mounted[group]) dispose?.(); mounted[group] = [] }- }- }+ const groups = surfaceGroups({ shell: installShell, conversation: installConversation })+ const reconcile = () => groups.reconcile(groupsFor(surface.getSnapshot()))reconcile()const unsub = surface.subscribe(reconcile)return () => {unsub()- for (const group of ['shell', 'conversation']) { for (const dispose of mounted[group]) dispose?.(); mounted[group] = [] }+ groups.dispose()}}, 'ds-bot: surface')}diff --git a/ds-bot/src/client/surface.js b/ds-bot/src/client/surface.jsindex c312a57c..2c1c54a6 100644--- a/ds-bot/src/client/surface.js+++ b/ds-bot/src/client/surface.js@@ -8,6 +8,39 @@ export function groupsFor({ mode, plain = false }) {return { shell: mode === 'bot', conversation: mode === 'bot' && !plain }}+const GROUPS = ['shell', 'conversation']++// Runs every disposer even when one throws, last installed first: later entries+// lean on earlier ones (the theme's styles and tokens), never the other way round.+function disposeAll(disposers, label) {+ for (const dispose of [...disposers].reverse()) {+ try { dispose?.() } catch (error) { console.warn(`[ds-bot] ${label}`, error) }+ }+}++// Keeps the mounted groups in step with `groupsFor`. The conversation group sits on+// the shell's styles and tokens, so it comes off before the shell and goes on after.+// A group's list is cleared before it is disposed, and a failed install leaves it+// empty, so a throw never makes a later reconcile believe the group is mounted.+export function surfaceGroups(install) {+ const mounted = { shell: [], conversation: [] }+ const withdraw = (group) => {+ const disposers = mounted[group]+ mounted[group] = []+ disposeAll(disposers, `withdraw ${group}`)+ }+ return {+ reconcile(wanted) {+ for (const group of [...GROUPS].reverse()) if (!wanted[group]) withdraw(group)+ for (const group of GROUPS) {+ if (!wanted[group] || mounted[group].length > 0) continue+ try { mounted[group] = install[group]() } catch (error) { console.warn(`[ds-bot] install ${group}`, error) }+ }+ },+ dispose() { for (const group of [...GROUPS].reverse()) withdraw(group) },+ }+}+export function readMode(storage = globalThis.localStorage) {try { return storage.getItem(STORAGE.surface) === 'agent' ? 'agent' : 'bot' } catch { return 'bot' }}diff --git a/ds-bot/test/surface.test.js b/ds-bot/test/surface.test.jsindex ea77bab6..02e3a5d0 100644--- a/ds-bot/test/surface.test.js+++ b/ds-bot/test/surface.test.js@@ -1,6 +1,6 @@import assert from 'node:assert/strict'import { test } from 'node:test'-import { groupsFor, readMode, writeMode } from '../src/client/surface.js'+import { groupsFor, readMode, surfaceGroups, writeMode } from '../src/client/surface.js'import { STORAGE } from '../src/client/storage.js'test('Bot mode mounts both groups; Agent mode mounts neither', () => {@@ -30,6 +30,68 @@ test('the saved surface mode survives reloads and defaults to bot', () => {assert.equal(readMode(storage), 'bot')})+const recordingGroups = (log, { failing = null } = {}) => {+ const entry = (name) => () => {+ log.push(name)+ if (name === failing) throw new Error(`could not release ${name}`)+ }+ return surfaceGroups({+ shell: () => { log.push('install shell'); return [entry('shell styles'), entry('shell theme')] },+ conversation: () => { log.push('install conversation'); return [entry('conversation styles'), entry('conversation tokens'), entry('conversation cells')] },+ })+}++test('switching to Agent mode withdraws the conversation before the shell, last entry first', (t) => {+ t.mock.method(console, 'warn', () => {})+ const log = []+ const groups = recordingGroups(log)+ groups.reconcile(groupsFor({ mode: 'bot' }))+ assert.deepEqual(log, ['install shell', 'install conversation'])+ log.length = 0+ groups.reconcile(groupsFor({ mode: 'agent' }))+ assert.deepEqual(log, ['conversation cells', 'conversation tokens', 'conversation styles', 'shell theme', 'shell styles'])+})++test('a throwing disposer does not stop the rest, and the group can be installed again', (t) => {+ const warn = t.mock.method(console, 'warn', () => {})+ const log = []+ const groups = recordingGroups(log, { failing: 'conversation tokens' })+ groups.reconcile(groupsFor({ mode: 'bot' }))+ log.length = 0+ assert.doesNotThrow(() => groups.reconcile(groupsFor({ mode: 'agent' })))+ assert.deepEqual(log, ['conversation cells', 'conversation tokens', 'conversation styles', 'shell theme', 'shell styles'])+ assert.equal(warn.mock.callCount(), 1)+ log.length = 0+ groups.reconcile(groupsFor({ mode: 'bot' }))+ assert.deepEqual(log, ['install shell', 'install conversation'])+})++test('unloading the plugin disposes every group even when a disposer throws', (t) => {+ t.mock.method(console, 'warn', () => {})+ const log = []+ const groups = recordingGroups(log, { failing: 'conversation cells' })+ groups.reconcile(groupsFor({ mode: 'bot' }))+ log.length = 0+ assert.doesNotThrow(() => groups.dispose())+ assert.deepEqual(log, ['conversation cells', 'conversation tokens', 'conversation styles', 'shell theme', 'shell styles'])+ log.length = 0+ groups.dispose()+ assert.deepEqual(log, [])+})++test('a group whose install throws stays unmounted and is tried again', (t) => {+ t.mock.method(console, 'warn', () => {})+ let attempts = 0+ const groups = surfaceGroups({+ shell: () => [() => {}],+ conversation: () => { attempts += 1; if (attempts === 1) throw new Error('slot refused'); return [() => {}] },+ })+ assert.doesNotThrow(() => groups.reconcile(groupsFor({ mode: 'bot' })))+ groups.reconcile(groupsFor({ mode: 'bot', plain: true }))+ groups.reconcile(groupsFor({ mode: 'bot' }))+ assert.equal(attempts, 2)+})+test('readMode falls back to bot when storage throws', () => {assert.equal(readMode({ getItem: () => { throw new Error('denied') } }), 'bot')})