A team of long-lived bots for DeepSeek Harness, with main bots, bot-to-bot messages and group chats.
fix: condense a Bot conversation when DSH compacts it for a model switch
srafy9a7c49bab572
diff --git a/ds-bot/src/host/parts.js b/ds-bot/src/host/parts.jsindex 5ef8d6f4..574540a2 100644--- a/ds-bot/src/host/parts.js+++ b/ds-bot/src/host/parts.js@@ -256,10 +256,13 @@ export function install(rt) {// The backend's own entry points leave Bot Sessions alone, earlier parts and group// records included: automatic pressure and overflow compaction do nothing, and- // /compact and explicit ranges are refused. Only DS Bot condenses a Bot Session,- // when the steward says so, and then the summary is DS Bot's checkpoint. Each preset- // mount has its own compaction backend. Every other Session, subagents included, is- // as it was.+ // /compact is refused. Only DS Bot condenses a Bot Session, when the steward says so,+ // and then the summary is DS Bot's checkpoint. An explicit range is different: with+ // those entry points closed, the only range DSH still asks of a Bot Session is the one+ // a model switch needs to fit the new model's window, and a refusal there ended the+ // switch itself (MODEL_SWITCH_COMPACTION_FAILED). So DS Bot condenses that range as its+ // own checkpoint instead. Each preset mount has its own compaction backend. Every other+ // Session, subagents included, is as it was.const adoptedEngines = new Set()// Sessions DS Bot itself is condensing right now.const condensing = new Set()@@ -270,6 +273,18 @@ export function install(rt) {return id !== undefined && (rt.selfOf(id) !== undefined || rt.roomOf(id) !== undefined)}const ownedByBot = target => isBotTarget(target) && !condensing.has(target.session.id)+ // Condenses a range of a Bot Session the way DS Bot condenses its own parts: the+ // session counts as condensing, so the summary hook writes DS Bot's checkpoint rather+ // than the backend writing one of its own.+ async function condenseRange(target, run) {+ const sessionId = target.session.id+ condensing.add(sessionId)+ try {+ return await run()+ } finally {+ condensing.delete(sessionId)+ }+ }// The engine that serves this agent, when DS Bot can condense through it.const engineOf = agent => [ctx.get('agentPresets')?.serviceFor(agent, 'compaction'), ctx.get('compaction')].find(engine => typeof engine?.[ORIGINAL_COMPACT]?.summarize === 'function')@@ -297,8 +312,11 @@ export function install(rt) {return original.compactNow.call(this, target, signal, commandId)}engine.compactRegion = function compactRegionOutsideBots(start, end, target, signal) {- if (ownedByBot(target)) return refuseCompaction()- return original.compactRegion.call(this, start, end, target, signal)+ if (!ownedByBot(target)) return original.compactRegion.call(this, start, end, target, signal)+ // Without the summary hook the backend would summarize a Bot conversation itself,+ // so that case keeps the refusal.+ if (original.summarize === undefined) return refuseCompaction()+ return condenseRange(target, () => original.compactRegion.call(this, start, end, target, signal))}if (original.summarize !== undefined) {engine.summarize = function summarizeForBots(input, agent, signal) {diff --git a/ds-bot/test/model-switch-compaction.test.js b/ds-bot/test/model-switch-compaction.test.jsnew file mode 100644index 00000000..8851ece7--- /dev/null+++ b/ds-bot/test/model-switch-compaction.test.js@@ -0,0 +1,113 @@+import assert from 'node:assert/strict'+import { mkdtempSync, rmSync, writeFileSync } from 'node:fs'+import { tmpdir } from 'node:os'+import { join } from 'node:path'+import { test } from 'node:test'+import { createRuntime } from '../index.js'+import { fakeContext } from './fake-ctx.js'++// DSH asks for an explicit range before a request that would not fit, and a model switch+// is one of those. A Bot conversation is condensed by DS Bot itself, so the range goes+// through DS Bot's checkpoint instead of being refused: a refusal there ended the switch+// with MODEL_SWITCH_COMPACTION_FAILED and the Bot stayed on the model it had.++const TEAM = {+ version: 1,+ mainBotIds: ['m1'],+ revision: 1,+ bots: {+ m1: { id: 'm1', sessionId: 'm1', name: 'Chief', role: 'Chief of Staff', instructions: '', createdAt: 1 },+ b2: { id: 'b2', sessionId: 'b2', name: 'Writer', role: 'Writing', instructions: '', createdAt: 2 },+ },+ rooms: {+ r1: { id: 'r1', name: 'Launch', named: true, members: ['m1', 'b2'], admin: 'm1', mode: 'admin', notice: '', createdAt: 4 },+ },+ exchanges: [],+ exchangeTurns: {},+ questions: {},+}++function start({ summarize = true } = {}) {+ const home = mkdtempSync(join(tmpdir(), 'ds-bot-compaction-'))+ writeFileSync(join(home, 'state.json'), JSON.stringify(TEAM))+ const bench = { regions: [], nows: [], summarised: 0 }+ const compaction = {+ async compactIfNeeded() { return null },+ async compactNow(target, signal, commandId) { bench.nows.push([target?.session?.id, commandId]) },+ async compactRegion(start, end, target) { bench.regions.push([start, end, target?.session?.id]) },+ }+ if (summarize) compaction.summarize = () => { bench.summarised += 1; return { summary: [] } }+ const ctx = fakeContext({+ services: { compaction, tokenMeter: { measure: () => ({ totalTokens: 10 }) } },+ })+ Object.assign(ctx, {+ llm: {+ listProviders: () => [{ id: 'deepseek', name: 'DeepSeek' }],+ listModels: async () => [{ id: 'v4' }],+ resolveModelInfo: async () => ({ context: { contextWindow: 100_000 }, defaultMaxTokens: 8_000 }),+ stream() {+ throw new Error('the checkpoint model is not called by these tests')+ },+ },+ sessions: { messageProjections: [] },+ workspaceRegistry: { archiveSession: async () => {} },+ })+ const rt = createRuntime(ctx, { home })+ const adopt = ctx.listeners.get('agent/created').find(listener => /adoptCompaction/.test(String(listener)))+ const agentOf = sessionId => ({+ id: sessionId,+ session: {+ id: sessionId,+ seq: 2,+ requestHeader: () => ({ config: { provider: 'deepseek', model: 'v4' } }),+ deriveMessages: () => [],+ toolHistory: () => [],+ },+ })+ const adoptFor = sessionId => adopt({ agent: agentOf(sessionId) })+ const stop = async () => {+ await ctx.dispose()+ rmSync(home, { recursive: true, force: true, maxRetries: 5 })+ }+ return { ctx, rt, bench, compaction, adoptFor, agentOf, stop }+}++test('a model switch compacts a Bot conversation instead of refusing the range', async (t) => {+ const { bench, compaction, adoptFor, agentOf, stop } = start()+ t.after(stop)+ adoptFor('b2')+ const signal = new AbortController().signal+ // DSH's model-switch pressure calls exactly this entry point.+ await compaction.compactRegion(3, 9, agentOf('b2'), signal)+ assert.deepEqual(bench.regions, [[3, 9, 'b2']])+})++test('a Session outside the team keeps the backend compaction', async (t) => {+ const { bench, compaction, adoptFor, agentOf, stop } = start()+ t.after(stop)+ adoptFor('b2')+ await compaction.compactRegion(1, 2, agentOf('outside'), new AbortController().signal)+ assert.deepEqual(bench.regions, [[1, 2, 'outside']])+})++test('without a summary hook of its own a Bot conversation is still refused', async (t) => {+ const { bench, compaction, adoptFor, agentOf, stop } = start({ summarize: false })+ t.after(stop)+ adoptFor('b2')+ await assert.rejects(+ () => compaction.compactRegion(3, 9, agentOf('b2'), new AbortController().signal),+ /A Bot conversation makes room on its own, and read_own_chat searches all of it\./,+ )+ assert.deepEqual(bench.regions, [])+})++test('an explicit /compact on a Bot conversation is still refused', async (t) => {+ const { bench, compaction, adoptFor, agentOf, stop } = start()+ t.after(stop)+ adoptFor('b2')+ await assert.rejects(+ () => compaction.compactNow(agentOf('b2'), new AbortController().signal, 'command-1'),+ /A Bot conversation makes room on its own/,+ )+ assert.deepEqual(bench.nows, [])+})