A team of long-lived bots for DeepSeek Harness, with main bots, bot-to-bot messages and group chats.
Change a Bot to a folder that exists before DSH checks its folder
Fei2411d1e3a993ee45
diff --git a/ds-bot/src/host/parts.js b/ds-bot/src/host/parts.jsindex f20643eb..e4cef2a0 100644--- a/ds-bot/src/host/parts.js+++ b/ds-bot/src/host/parts.js@@ -659,8 +659,11 @@ export function install(rt) {await rt.ensureCapabilities(target)const agent = await rt.resolveAgent(target)for (const message of messages) {- const copy = createUserMessage({ content: message.content, ...(message.source ? { source: message.source } : {}) })const kind = message.source?.kind+ // DSH's notice of a folder change is about the Session it was queued in; the part+ // that goes on has its own folder, which its handoff names.+ if (kind === 'working-directory') continue+ const copy = createUserMessage({ content: message.content, ...(message.source ? { source: message.source } : {}) })if (kind === 'user' || kind === SOURCE_KIND || kind === REPLY_KIND) agent.followup(copy)else agent.inject(copy)}@@ -875,6 +878,31 @@ export function install(rt) {return target === undefined || target === cwd ? undefined : target}+ // DSH 0.2.1 checks a Session's folder as it puts each model request together, before+ // agent/pre-step, and fails the turn when both the folder and the one the Session was+ // made in are gone. The team folder is DS Bot's own, so it is made again; a Bot whose+ // own folder went away first changes to the folder it would move to, through DSH's+ // documented way out (`workingDirectory.set`), and the pre-step then moves it to a+ // fresh part there, as on DSH 0.2.0.+ ctx.on('system-prompt/assemble', async (assembly, context, next) => {+ const agent = context.agent+ const folders = ctx.get?.('workingDirectory')+ if (agent === undefined || typeof folders?.get !== 'function' || readOnly()) return next()+ const current = folders.get(agent.session)+ if (current === undefined || await rt.isFolder(current)) return next()+ await rt.load()+ const team = await rt.keepTeamFolder()+ if (current === team || !rt.isBotSession(agent.id)) return next()+ const place = placeOf(agent.id)+ const wanted = rt.workDirOf(place)+ const target = wanted !== undefined && await rt.isFolder(wanted) ? wanted : team+ if (target !== undefined) {+ await folders.set(agent, target, context.signal)+ .catch(error => rt.warn('ds-bot: %s works in %s, which is gone, and could not change to %s: %s', rt.botOf(place?.botId)?.name ?? agent.id, current, target, error))+ }+ return next()+ }, { prepend: true })+// A Bot's own chat moves to its new folder now when it is idle; a running turn// moves at its next step, and each group conversation at its next turn there.async function followFolder(botId) {diff --git a/ds-bot/src/host/roster.js b/ds-bot/src/host/roster.jsindex 981bc34a..4f26c2fb 100644--- a/ds-bot/src/host/roster.js+++ b/ds-bot/src/host/roster.js@@ -11,8 +11,18 @@ export function install(rt) {let bootstrapping = null+ // The team folder is DS Bot's own: deleted, it is made again before a Session needs it.+ async function keepTeamFolder() {+ const team = rt.teamFolder()+ if (team !== undefined) await mkdir(team, { recursive: true }).catch(error => rt.warn('ds-bot: team folder %s not made again: %s', team, error))+ return team+ }+async function ensureWorkspace() {- if (state.workspaceId && ctx.workspaceRegistry.get(state.workspaceId)) return state.workspaceId+ if (state.workspaceId && ctx.workspaceRegistry.get(state.workspaceId)) {+ await keepTeamFolder()+ return state.workspaceId+ }const path = config.workspace ?? join(home, 'workspace')await mkdir(path, { recursive: true })const workspace = await ctx.workspaceRegistry.resolveByPath(path) ?? await ctx.workspaceRegistry.create(path, 'DS Bot')@@ -504,7 +514,7 @@ export function install(rt) {}Object.assign(rt, {- ensureWorkspace, createSession, createBot, duplicateBot, updateBot, setFlags,+ ensureWorkspace, keepTeamFolder, createSession, createBot, duplicateBot, updateBot, setFlags,roomBusy, autoRoomName, retitleRoom, createRoom, updateRoom, deleteRoom,setMain, deleteBot, bootstrap, dropKickoff,})diff --git a/ds-bot/test/install.test.js b/ds-bot/test/install.test.jsindex 1f3d7522..bf1331a0 100644--- a/ds-bot/test/install.test.js+++ b/ds-bot/test/install.test.js@@ -81,6 +81,7 @@ test('apply registers listeners, tools, and effects in a fixed order', (t) => {'effect ds-bot: Bot compaction','on agent/created','on agent/pre-step (prepend)',+ 'on system-prompt/assemble (prepend)','on agent/request-error (prepend)','on agent/request-error (prepend)','on agent/status',diff --git a/ds-bot/test/work-dir.test.js b/ds-bot/test/work-dir.test.jsindex 8cb98a1e..34c5d330 100644--- a/ds-bot/test/work-dir.test.js+++ b/ds-bot/test/work-dir.test.js@@ -1,5 +1,5 @@import assert from 'node:assert/strict'-import { mkdirSync, mkdtempSync, readFileSync, realpathSync, rmSync, writeFileSync } from 'node:fs'+import { existsSync, mkdirSync, mkdtempSync, readFileSync, realpathSync, rmSync, writeFileSync } from 'node:fs'import { homedir, tmpdir } from 'node:os'import { join, resolve } from 'node:path'import { test } from 'node:test'@@ -443,3 +443,58 @@ test('a Bot the Main Bot creates runs on its creator\'s model unless one is nameassert.equal(await env.tool('create_bot', { name: 'Tester', role: 'Test', instructions: 'Test', model: 'official/pro' }, 'a'), 'Created Tester. It is greeting the user now.')assert.equal(made('Tester').model, 'official/pro')})++// #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, [])+})