A team of long-lived bots for DeepSeek Harness, with main bots, bot-to-bot messages and group chats.
Re-select every Bot pinned to a refused route
Fei24113e4d94c862cb
diff --git a/ds-bot/src/host/models.js b/ds-bot/src/host/models.jsindex 10a79502..cbf4c876 100644--- a/ds-bot/src/host/models.js+++ b/ds-bot/src/host/models.js@@ -13,13 +13,27 @@ export function install(rt) {let catalogCache = { at: 0, value: [] }// Routes DSH serves that cannot answer a request at all — a hosted loop engine// publishes one so its Sessions can be prompted. Once one refuses with- // HOSTED_ENGINE_ROUTE it is remembered, and nothing chooses it again this run.- const refused = new Set()+ // HOSTED_ENGINE_ROUTE it is remembered with the models its provider served then,+ // and nothing chooses it again until that provider serves different models.+ const refused = new Map()const answerable = entries => (refused.size === 0 ? entries : entries.filter(entry => !refused.has(entry.ref)))+ // The models a ref's provider serves, as a string to compare; undefined when it+ // cannot be listed, e.g. the provider is not registered.+ async function servedModels(ref) {+ try { return JSON.stringify(await ctx.llm.listModels(splitRef(ref).provider)) } catch { return undefined }+ }+ // An adapter update may have replaced a refused placeholder with a real route, which+ // serves other models; the same models again are the same route, still refused.+ async function forgetChangedRefusals() {+ for (const [ref, served] of refused) {+ const now = await servedModels(ref)+ if (now !== undefined && now !== served) refused.delete(ref)+ }+ }// A model's thinking levels, resolved once and kept for a short while: they change// only with the provider, which also drops the catalog cache.const reasoningCache = new Map()- ctx.on?.('llm/adapters-updated', () => { catalogCache = { at: 0, value: [] }; reasoningCache.clear() })+ ctx.on?.('llm/adapters-updated', () => { catalogCache = { at: 0, value: [] }; reasoningCache.clear(); return forgetChangedRefusals() })for (const name of ['credentials/reference-updated', 'settings/document-updated']) ctx.on?.(name, () => { catalogCache = { at: 0, value: [] } })// The thinking levels a model serves: `{efforts: [{id, name, description?}],@@ -202,11 +216,11 @@ export function install(rt) {}}- // Sequential, and again whenever the served models change: a provider that- // registers late still gets its Bots' models selected.+ // Sequential, and again whenever the answerable models change (a refused route is+ // not one of them): a provider that registers late still gets its Bots' models selected.let syncedKeyconst syncModels = () => {- const key = catalogCache.value.map(entry => entry.ref).join(',')+ const key = answerable(catalogCache.value).map(entry => entry.ref).join(',')if (key !== syncedKey) { syncedKey = key; modelsSynced = modelsSynced?.then(syncAll, syncAll) ?? syncAll() }return modelsSynced}@@ -218,18 +232,23 @@ export function install(rt) {// A route that refused a request for not being a model endpoint: it stays out of// the catalog from now on, the Bots pinned to it move to what they would be given- // today, and the Bot whose turn failed re-selects — its group Session may carry- // another Bot's route, so `force` re-selects even when its record looks applied.+ // today, and each of them re-selects even when its record looks applied (`force`):+ // its Session may still be on the route, or on another Bot's route in a group.async function routeRefused(ref, bot) {+ const served = await servedModels(ref)if (!refused.has(ref)) {- refused.add(ref)+ refused.set(ref, served)rt.warn('ds-bot: %s is not a model endpoint, so it is not chosen again', ref)const entries = await catalog()- for (const other of Object.values(state.bots)) {+ const pinned = Object.values(state.bots).filter(other => other.model === ref || other.appliedModel === ref)+ for (const other of pinned) {if (other.model === ref) other.model = pickDefault(entries)if (other.appliedModel === ref) delete other.appliedModel}await rt.save()+ for (const other of pinned) {+ if (other !== bot) await applyModel(other, { force: true }).catch(error => rt.warn('ds-bot: model for %s not applied: %s', other.name, error))+ }}if (bot !== undefined) await applyModel(bot, { force: true })}diff --git a/ds-bot/test/models.test.js b/ds-bot/test/models.test.jsindex c27b2237..d88c2eba 100644--- a/ds-bot/test/models.test.js+++ b/ds-bot/test/models.test.js@@ -194,6 +194,54 @@ test('a Bot on a clean record but a poisoned Session re-selects its own model',assert.deepEqual(selected, ['b1:deepseek/v4-flash'])})+// Every Bot the route was pinned to may have its Session still on it, even one whose+// record already names the replacement; re-pinning the record alone leaves that+// Session failing on every turn.+test('every Bot re-pinned off a refused route re-selects its Session', async () => {+ const { rt, selected } = bench({ providers: { external: ['default'], deepseek: ['v4-flash'] }, fallback: { provider: 'external', model: 'default' } })+ const failed = { id: 'b1', model: 'external/default', appliedModel: 'external/default' }+ const alsoPinned = { id: 'b2', model: 'external/default', appliedModel: 'deepseek/v4-flash' }+ rt.state.bots = { b1: failed, b2: alsoPinned }+ await rt.routeRefused('external/default', failed)+ assert.equal(alsoPinned.model, 'deepseek/v4-flash')+ assert.equal(alsoPinned.appliedModel, 'deepseek/v4-flash')+ assert.deepEqual([...selected].sort(), ['b1:deepseek/v4-flash', 'b2:deepseek/v4-flash'])+})++// The sync key must see the filtered catalog: the catalog itself does not change+// on a refusal, so an unfiltered key would never run the sync again.+test('a refusal changes the sync key, so the next sync applies what the records say', async () => {+ const { rt, selected } = bench({ providers: { external: ['default'], deepseek: ['v4-flash'] } })+ const bot = { id: 'b1', model: 'deepseek/v4-flash', appliedModel: 'deepseek/v4-flash' }+ rt.state.bots = { b1: bot }+ await rt.catalog()+ await rt.syncModels()+ await rt.routeRefused('external/default')+ // The Session drifted meanwhile — its own picker can move it without telling us.+ delete bot.appliedModel+ await rt.syncModels()+ assert.deepEqual(selected, ['b1:deepseek/v4-flash'])+})++// A refused route is chosen again only once its provider serves other models: the same+// placeholder registered again, even after it was unregistered, is still not an endpoint.+test('an adapter update ends a refusal only when its provider serves other models', async () => {+ const { rt, providers, listeners } = bench({ providers: { external: ['default'], deepseek: ['v4-flash'] }, fallback: { provider: 'external', model: 'default' } })+ const adaptersUpdated = () => listeners.get('llm/adapters-updated')()+ await rt.routeRefused('external/default')+ await adaptersUpdated()+ assert.equal(rt.defaultRef(), undefined)+ delete providers.external+ await adaptersUpdated()+ providers.external = ['default']+ await adaptersUpdated()+ assert.equal(rt.defaultRef(), undefined)+ providers.external = ['default', 'real']+ await adaptersUpdated()+ assert.equal(rt.defaultRef(), 'external/default')+ assert.ok((await rt.catalog()).some(entry => entry.ref === 'external/default'))+})+test('a greeting waits for the provider of its Bot model to register', async () => {const { rt, providers, listeners } = bench({ providers: {} })const waiting = rt.waitForModel({ model: 'stepfun/step-5' }, 5000)