A team of long-lived bots for DeepSeek Harness, with main bots, bot-to-bot messages and group chats.
Merge remote-tracking branch 'origin/dev' into fix/159-fresh-part-model
Fei2411309051d6c0d8
diff --cc ds-bot/src/host/parts.jsindex 12ad5510,3ec6e779..66b0bcac--- a/ds-bot/src/host/parts.js+++ b/ds-bot/src/host/parts.js@@@ -776,10 -767,24 +779,19 @@@ export function install(rt)const dir = rt.workDirOf(job)const intended = dir !== undefined && await rt.isFolder(dir) ? dir : rt.teamFolder()const freshId = await rt.createSession(room ? `${bot.name} · ${room.name}` : bot.name, { dir })- const route = session.requestHeader()?.config- const ref = route?.provider && route?.model ? `${route.provider}/${route.model}` : await rt.desiredModel(bot)- if (ref !== undefined) {- const effort = route?.reasoningEffort ?? await rt.desiredEffort(bot, ref)- await rt.selectSessionModel(freshId, { ...splitRef(ref), ...(effort === undefined ? {} : { reasoningEffort: effort }) })- .catch(error => rt.warn('ds-bot: model for the fresh part of %s not applied: %s', bot.name, error))- }+ const picked = await freshRoute(bot, session)+ if (picked !== undefined) await selectRoute(bot, freshId, picked)await rt.applyPermission(bot, freshId, room).catch(error => rt.warn('ds-bot: permission for the fresh part of %s not applied: %s', bot.name, error))+ // The fresh part took several awaits: a Bot or group deleted meanwhile, a member that+ // left, or a part that is no longer the current one gets no fresh part.+ const current = room !== undefined ? room.sessions?.[bot.id] : rt.chatOf(bot.id)+ const gone = rt.botOf(bot.id) === undefined || (room !== undefined && (rt.roomOf(room.id) !== room || !room.members.includes(bot.id)))+ if (gone || current !== agent.id) {+ await ctx.workspaceRegistry.archiveSession(freshId, { stopActivity: true })+ .catch(error => rt.warn('ds-bot: unused fresh part of %s not archived: %s', bot.name, error))+ return+ }const segment = { sessionId: agent.id, endedAt: Date.now(), ...(tokens !== undefined ? { tokens } : {}), ...noted }if (room !== undefined) {room.segments ??= {}diff --cc ds-bot/test/work-dir.test.jsindex b4dd6d66,7c896d8f..9b960624--- a/ds-bot/test/work-dir.test.js+++ b/ds-bot/test/work-dir.test.js@@@ -444,57 -444,84 +444,139 @@@ test('a Bot the Main Bot creates runs oassert.equal(made('Tester').model, 'official/pro')})+// 公开#159: the fresh part took the model of the earlier part's last request, so a model+// the user picked during the handoff never reached it, nor any part after it.+const MODELS = {+ listProviders: () => [{ id: 'deepseek', name: 'DeepSeek' }],+ listModels: async () => [{ id: 'flash' }, { id: 'v4-pro' }],+ resolveModelInfo: async () => ({}),+}+const onModel = (model, appliedModel = model) => ({ ...TEAM, bots: { ...TEAM.bots, b: { ...TEAM.bots.b, model, appliedModel } } })+const recordSelections = (env) => {+ const selected = []+ env.ctx.sessionController.selectModel = async (request) => { selected.push([request.sessionId, request.model]) }+ // The earlier part's last request went to flash.+ env.agentOf('b').session.requestHeader = () => ({ config: { provider: 'deepseek', model: 'flash' } })+ return selected+}++test('a fresh part runs on the Bot\'s model, not on the earlier part\'s last request', async (t) => {+ const env = await start({ llm: MODELS, team: onModel('deepseek/v4-pro') })+ t.after(env.stop)+ const selected = recordSelections(env)+ assert.equal((await env.call('update-bot', { id: 'b', workDir: env.project })).ok, true)+ await until(() => env.state().bots.b.sessionId === 's1')+ await until(() => handoffOf(env.agentOf('s1')) !== undefined)+ assert.deepEqual(selected.filter(([sessionId]) => sessionId === 's1'), [['s1', 'v4-pro']])+})++test('a model the user picks while the fresh part is made reaches the fresh part', async (t) => {+ const env = await start({ llm: MODELS, team: onModel('deepseek/flash') })+ t.after(env.stop)+ const selected = recordSelections(env)+ const select = env.ctx.sessionController.selectModel+ env.ctx.sessionController.selectModel = async (request) => {+ await select(request)+ if (request.sessionId !== 's1' || selected.length > 1) return+ // The user switches models after the fresh part got the old one, before it is the Bot's.+ void env.call('update-bot', { id: 'b', model: 'deepseek/v4-pro' })+ await until(() => env.state().bots.b.model === 'deepseek/v4-pro')+ }+ assert.equal((await env.call('update-bot', { id: 'b', workDir: env.project })).ok, true)+ await until(() => env.state().bots.b.sessionId === 's1')+ await until(() => selected.some(([sessionId, model]) => sessionId === 'b' && model === 'v4-pro'))+ await until(() => handoffOf(env.agentOf('s1')) !== undefined)+ assert.deepEqual(selected.filter(([sessionId]) => sessionId === 's1'), [['s1', 'flash'], ['s1', 'v4-pro']])+})++test('a Bot without a model of its own goes on with the earlier part\'s route', async (t) => {+ const env = await start({ llm: MODELS })+ t.after(env.stop)+ const selected = recordSelections(env)+ assert.equal((await env.call('update-bot', { id: 'b', workDir: env.project })).ok, true)+ await until(() => env.state().bots.b.sessionId === 's1')+ await until(() => handoffOf(env.agentOf('s1')) !== undefined)+ assert.deepEqual(selected.filter(([sessionId]) => sessionId === 's1'), [['s1', 'flash']])+})+++ // #209: DSH 0.2.1 checks a Session's folder as it puts a model request together, before+ // agent/pre-step, and fails the turn when the folder and the one the Session was made in+ // are gone. Its documented way out is to change the Session's folder first.+ function folderService() {+ const dirs = new Map()+ const sets = []+ return {+ sets,+ service: {+ get: session => dirs.get(session.id) ?? session.header.cwd,+ set: async (agent, path) => { sets.push([agent.id, path]); dirs.set(agent.session.id, path) },+ },+ }+ }+ const assemble = (ctx, agent) => {+ let passed = false+ const run = ctx.listeners.get('system-prompt/assemble').at(-1)({ contexts: [] }, { agent, signal: AbortSignal.timeout(60_000) }, async () => { passed = true })+ return run.then(() => passed)+ }++ test('a Bot whose folder is gone changes to the team folder before DSH checks it, then moves to a fresh part there', async (t) => {+ const { sets, service } = folderService()+ const { ctx, call, agentOf, created, project, team, state, preStep, stop } = await start({ services: { workingDirectory: service } })+ t.after(stop)+ assert.equal((await call('update-bot', { id: 'b', workDir: project })).ok, true)+ await until(() => state().bots.b.sessionId === 's1')+ await until(() => handoffOf(agentOf('s1')) !== undefined)+ rmSync(project, { recursive: true, force: true })++ assert.equal(await assemble(ctx, agentOf('s1')), true)+ assert.deepEqual(sets, [['s1', team]])+ // The pre-step then moves the Bot on to a fresh part in the team workspace. DSH's notice+ // of the change is about the old part, so it stays behind.+ const notice = { id: 'm0', role: 'user', content: [{ type: 'text', text: 'The working directory is now the team folder.' }], source: { kind: 'working-directory' } }+ const input = { id: 'm1', role: 'user', content: [{ type: 'text', text: 'Still there?' }], source: { kind: 'user' } }+ assert.deepEqual(await preStep('s1', [notice, input]), { kind: 'reject' })+ await until(() => state().bots.b.sessionId === 's2')+ assert.deepEqual(created.at(-1), { workspaceId: 'w1' })+ await until(() => agentOf('s2').calls.length >= 2)+ assert.deepEqual(agentOf('s2').calls.map(([kind, message]) => [kind, message.source.role ?? textOf(message)]), [['followup', 'handoff'], ['followup', 'Still there?']])+ // A folder that is there is left as it is.+ assert.equal(await assemble(ctx, agentOf('a')), true)+ assert.equal(sets.length, 1)+ })++ test('a team folder that was deleted is made again before DSH checks it', async (t) => {+ const { sets, service } = folderService()+ const { ctx, agentOf, team, stop } = await start({ services: { workingDirectory: service } })+ t.after(stop)+ rmSync(team, { recursive: true, force: true })+ assert.equal(await assemble(ctx, agentOf('a')), true)+ assert.equal(existsSync(team), true)+ assert.deepEqual(sets, [])+ })++ // A fresh part takes several awaits; a Bot deleted meanwhile must not leave it behind.+ test('a Bot deleted while its chat moves to a new folder leaves no fresh part behind', async (t) => {+ const { ctx, call, agentOf, project, state, stop } = await start()+ t.after(stop)+ const archived = []+ ctx.workspaceRegistry.archiveSession = async (sessionId) => { archived.push(sessionId) }+ let release+ const gate = new Promise(resolve => { release = resolve })+ let made+ const making = new Promise(resolve => { made = resolve })+ const create = ctx.sessionController.create+ ctx.sessionController.create = async (options) => {+ const result = await create(options)+ made(result.sessionId)+ await gate+ return result+ }+ assert.equal((await call('update-bot', { id: 'b', workDir: project })).ok, true)+ const fresh = await making+ assert.equal((await call('delete-bot', { id: 'b' })).ok, true)+ release()+ await until(() => archived.includes(fresh))+ await settle()+ assert.equal(state().bots.b, undefined)+ assert.equal(handoffOf(agentOf(fresh)), undefined)+ })