Skip to content

A team of long-lived bots for DeepSeek Harness, with main bots, bot-to-bot messages and group chats.

Merge dev into fix/209-gone-folder

Fei24110763ce280959
diff --cc ds-bot/test/rounds.test.js
index 44181c15,880ab0a5..bb3ec5ee
--- a/ds-bot/test/rounds.test.js
+++ b/ds-bot/test/rounds.test.js
@@@ -348,21 -348,178 +348,197 @@@ test('a message sent between rounds sti
assert.deepEqual(result.replies.map(reply => reply.name ?? 'User'), ['Chief', 'Writer'])
})
+// #209: DSH can fail a member's turn before its first step (DSH 0.2.1 does so for a folder
+// that is gone), so the turn never takes the round's call. The round counts the failure at
+// once instead of waiting minutes for a reply that cannot come.
+test('a member turn that fails before it takes the call fails that member in the round at once', async (t) => {
+ const { call, emit, userMessage, relay, answer, stop, sent } = start()
+ t.after(stop)
+ const room = await team(call, sent)
+ await emit(room.id, userMessage('Start'))
+ const running = relay(room.id, 'relay')
+ const first = await turnFor(sent)
+ await emit(first.sessionId, { type: 'turn/start', data: { turn: 1 } })
+ await emit(first.sessionId, { type: 'turn/end', data: { reason: { kind: 'error', error: { message: 'working-directory: directory does not exist: "/gone"', code: 'UNKNOWN' } } } })
+ const second = await turnFor(sent, first)
+ assert.ok(second, 'the next member speaks without waiting for the reply timeout')
+ await answer(second.sessionId, second.message.source.exchangeId, 'Writer here.', 1)
+ const result = JSON.parse(await running)
+ assert.deepEqual(result.replies.map(reply => [reply.kind ?? 'reply', reply.name]), [['failed', 'Chief'], ['reply', 'Writer']])
+})
++
+ // A member deleted while its group Session is made sits the round out; the Session it was
+ // getting belongs to no one, so it is archived instead of left over.
+ test('a member deleted while its group Session is made is left out, and the Session is not kept', async (t) => {
+ const { call, emit, userMessage, relay, answer, stop, sent, ctx, rt } = start()
+ t.after(stop)
+ const room = await team(call, sent)
+ const writer = rt.state.rooms[room.id].members.find(id => !rt.isMain(id))
+ const archived = []
+ ctx.workspaceRegistry.archiveSession = async (sessionId) => { archived.push(sessionId) }
+ await emit(room.id, userMessage('Start'))
+ const running = relay(room.id, 'relay')
+ const first = await turnFor(sent)
+
+ 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
+ }
+ await answer(first.sessionId, first.message.source.exchangeId, 'Chief here.', 1)
+ const sessionId = await making
+ assert.equal((await call('delete-bot', { id: writer })).ok, true)
+ release()
+
+ const result = JSON.parse(await running)
+ assert.deepEqual(result.replies.map(reply => [reply.kind ?? 'reply', reply.name]), [['reply', 'Chief']])
+ assert.ok(archived.includes(sessionId))
+ assert.equal(rt.state.rooms[room.id].sessions?.[writer], undefined)
+ })
+
+ // #206: state.json is written whole on every save, so a long group line keeps its start
+ // there and its whole text in a file of its own. Members still read it whole.
+ test('a long group line keeps its start in state.json, and members and read_group_chat read it whole', async (t) => {
+ const { call, emit, userMessage, relay, answer, stop, sent, ctx, home, rt } = start()
+ t.after(() => rmSync(home, { recursive: true, force: true, maxRetries: 5 }))
+ const room = await team(call, sent)
+ const long = `${'长'.repeat(9000)}结尾`
+ await emit(room.id, userMessage('Write it all out'))
+ const running = relay(room.id, 'relay')
+ const first = await turnFor(sent)
+ await answer(first.sessionId, first.message.source.exchangeId, long, 1)
+ const second = await turnFor(sent, first)
+ assert.ok(second.message.content[0].text.includes(long), 'the next member reads the line whole')
+ await answer(second.sessionId, second.message.source.exchangeId, 'Thanks.', 2)
+ assert.equal(JSON.parse(await running).replies[0].text, long)
+
+ await rt.save()
+ const saved = JSON.parse(readFileSync(join(home, 'state.json'), 'utf8')).rooms[room.id].log.find(line => line.length !== undefined)
+ assert.equal(saved.text.length, 2000)
+ assert.equal(saved.length, long.length)
+ assert.equal(readFileSync(join(home, 'group-lines', room.id, `${saved.seq}.txt`), 'utf8'), long)
+ const read = ctx.registeredTools.get('read_group_chat')
+ const exec = { agent: { id: first.sessionId } }
+ assert.match(await read.execute({ group: 'R1' }, exec), new RegExp(`cut; ${long.length} characters in all; read_group_chat with seq ${saved.seq} reads it whole`))
+ assert.ok((await read.execute({ group: 'R1', seq: saved.seq }, exec)).endsWith(long))
+ await stop({ keep: true })
+
+ // The log is full (60 lines) when DSH starts again, the long line second oldest.
+ const file = join(home, 'state.json')
+ const onDisk = JSON.parse(readFileSync(file, 'utf8'))
+ const kept = onDisk.rooms[room.id]
+ const fillers = Array.from({ length: 60 - kept.log.length }, (_, at) => ({ who: 'User', text: `line ${at}`, seq: kept.seq + at + 1 }))
+ kept.log = [...kept.log, ...fillers]
+ kept.seq += fillers.length
+ writeFileSync(file, JSON.stringify(onDisk))
+
+ // After a restart the whole line is read back from its file.
+ const again = start({ home })
+ t.after(() => again.stop({ keep: true }))
+ await again.call('state', {})
+ const whole = await again.ctx.registeredTools.get('read_group_chat').execute({ group: 'R1', seq: saved.seq }, exec)
+ assert.ok(whole.endsWith(long))
+
+ // The file goes with the line once the log moves past it, and with the group.
+ await again.emit(room.id, again.userMessage('Next'))
+ const next = again.relay(room.id, 'relay-2')
+ const one = await turnFor(again.sent)
+ await again.answer(one.sessionId, one.message.source.exchangeId, 'One.', 3)
+ const two = await turnFor(again.sent, one)
+ await again.answer(two.sessionId, two.message.source.exchangeId, 'Two.', 4)
+ await next
+ await waitFor(() => (existsSync(join(home, 'group-lines', room.id, `${saved.seq}.txt`)) ? undefined : true))
+ assert.equal(existsSync(join(home, 'group-lines', room.id, `${saved.seq}.txt`)), false)
+ await again.call('delete-room', { id: room.id })
+ await waitFor(() => (existsSync(join(home, 'group-lines', room.id)) ? undefined : true))
+ assert.equal(existsSync(join(home, 'group-lines', room.id)), false)
+ })
+
+ test('a group line longer than a member reads is cut in its prompt, with its length, and its file keeps only that', async (t) => {
+ const { call, emit, userMessage, relay, answer, stop, sent, home, rt } = start()
+ t.after(stop)
+ const room = await team(call, sent)
+ await emit(room.id, userMessage('Go'))
+ const running = relay(room.id, 'relay')
+ const first = await turnFor(sent)
+ await answer(first.sessionId, first.message.source.exchangeId, 'x'.repeat(25_000), 1)
+ const second = await turnFor(sent, first)
+ const prompt = second.message.content[0].text
+ assert.ok(prompt.includes(`${'x'.repeat(20_000)}… (cut; 25000 characters in all)`))
+ assert.ok(!prompt.includes('x'.repeat(20_001)))
+ await answer(second.sessionId, second.message.source.exchangeId, 'Ok.', 2)
+ await running
+ const line = rt.state.rooms[room.id].log.find(entry => entry.length === 25_000)
+ assert.equal(readFileSync(join(home, 'group-lines', room.id, `${line.seq}.txt`), 'utf8').length, 20_000)
+ })
+
+ // 公开#176: a PDF the user sent in a group reached neither the log nor any member, so
+ // every Bot said it could not read it; in a Bot's own chat the same file worked.
+ test('a file the user sends in a group reaches each member the line is shown to, once', async (t) => {
+ const { call, emit, relay, answer, stop, sent, rt } = start()
+ t.after(stop)
+ const room = await team(call, sent)
+ const pdf = { attachmentId: 'sha256:39cb3dab00', name: 'report-XYZ176.pdf', bytes: 607 }
+ await emit(room.id, { type: 'user/message', data: { source: { kind: 'user' }, content: [{ type: 'text', text: 'Read this PDF' }, { type: 'file', attachment: pdf }] } })
+ let running = relay(room.id, 'r1')
+ const first = await turnFor(sent)
+ const files = message => message.content.filter(block => block.type === 'file').map(block => block.attachment)
+ assert.deepEqual(files(first.message), [pdf])
+ assert.match(first.message.content[0].text, /User: Read this PDF\n\[File "report-XYZ176\.pdf" \(607 bytes\) attached\]/)
+ await answer(first.sessionId, first.message.source.exchangeId, 'Reading it.', 1)
+ const second = await turnFor(sent, first)
+ assert.deepEqual(files(second.message), [pdf])
+ await answer(second.sessionId, second.message.source.exchangeId, 'Me too.', 2)
+ await running
+ assert.deepEqual(rt.roomOf(room.id).log.find(line => line.who === 'User').files, [pdf])
+
+ sent.length = 0
+ await emit(room.id, { type: 'user/message', data: { source: { kind: 'user' }, content: [{ type: 'text', text: 'Thanks' }] } })
+ running = relay(room.id, 'r2')
+ const next = await turnFor(sent)
+ assert.deepEqual(files(next.message), [])
+ await answer(next.sessionId, next.message.source.exchangeId, 'OK.', 3)
+ const after = await turnFor(sent, next)
+ await answer(after.sessionId, after.message.source.exchangeId, 'OK.', 4)
+ await running
+ })
+
+ // #206: a Bot's post past 20,000 characters goes as a file; the line keeps the file and
+ // each member it is shown to gets it with its prompt.
+ test('a long post goes to the group as a file, and reaches the member with its prompt', async (t) => {
+ const { call, emit, relay, answer, stop, sent, ctx, rt } = start()
+ t.after(stop)
+ const store = fakeAttachments()
+ ctx.get = name => (name === 'attachments' ? store : undefined)
+ const room = await team(call, sent)
+ const chief = rt.state.mainBotIds[0]
+ const long = `${Array.from({ length: 400 }, (_, at) => `第 ${at + 1} 行:${'报告内容。'.repeat(10)}`).join('\n')}\n结尾`
+ const posted = await ctx.registeredTools.get('post_to_group').execute({ group: 'R1', message: long }, { agent: { id: rt.chatOf(chief) } })
+ assert.match(posted, /^Posted in "R1"/)
+ const post = sent.find(entry => entry.sessionId === room.id && entry.message.source.role === 'post')
+ const file = post.message.content.find(block => block.type === 'file')?.attachment
+ assert.equal(file?.name, 'Post from Chief.txt')
+ assert.equal(post.message.source.length, long.length)
+ assert.equal(new TextDecoder().decode(store.files.get(file.attachmentId)), long)
+
+ sent.length = 0
+ await emit(room.id, { type: 'user/message', data: { source: post.message.source, content: post.message.content } })
+ const running = relay(room.id, 'relay')
+ const turn = await turnFor(sent)
+ assert.deepEqual(turn.message.content.filter(block => block.type === 'file').map(block => block.attachment), [file])
+ const prompt = turn.message.content[0].text
+ assert.ok(prompt.includes(`Chief: [A long message: ${long.length} characters.`))
+ assert.ok(prompt.includes(`[File "Post from Chief.txt" (${file.bytes} bytes) attached]`))
+ assert.ok(prompt.length < 6000, `the prompt has ${prompt.length} characters`)
+ await answer(turn.sessionId, turn.message.source.exchangeId, 'Read it.', 1)
+ await running
+ const line = rt.state.rooms[room.id].log.find(entry => entry.botId === chief)
+ assert.deepEqual(line.files, [file])
+ assert.equal(line.length, undefined, 'the line is short; no file of its own')
+ })
diff --cc ds-bot/test/work-dir.test.js
index 34c5d330,dcb54557..7c896d8f
--- a/ds-bot/test/work-dir.test.js
+++ b/ds-bot/test/work-dir.test.js
@@@ -444,57 -444,29 +444,84 @@@ test('a Bot the Main Bot creates runs o
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, [])
+})
++
+ // 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)
+ })