Skip to content

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

Show a group post at once while a round is running

Fei24114f59f6ffd74c
diff --git a/ds-bot/client.js b/ds-bot/client.js
index 484938ff..fef31201 100644
--- a/ds-bot/client.js
+++ b/ds-bot/client.js
@@ -4569,6 +4569,7 @@ function TriggerCell({ node, sessionId, useRoster, useSessions, useTurnClock, us
if (source.role === "kickoff" || source.role === "room") return null;
if (source.role === "secret" || deferred) return null;
if (source.role === "post") {
+ if (source.logged !== void 0) return null;
if (source.failed !== void 0 || source.resumed === true || source.stopped === true || source.done === true) return null;
const room2 = roster.roomsById[sessionId];
const poster = roster.byId[source.senderSessionId] ?? { id: source.senderSessionId, name: source.senderName ?? t("a Bot"), color: "gray" };
diff --git a/ds-bot/src/client/cells.js b/ds-bot/src/client/cells.js
index 02d8aa2c..9e9abe0d 100644
--- a/ds-bot/src/client/cells.js
+++ b/ds-bot/src/client/cells.js
@@ -413,6 +413,8 @@ export function TriggerCell({ node, sessionId, useRoster, useSessions, useTurnCl
// The secret card itself shows what the user did; the note to the Bot stays out.
if (source.role === 'secret' || deferred) return null
if (source.role === 'post') {
+ // A post logged into a running round already shows there, not again as a trigger.
+ if (source.logged !== undefined) return null
// A member's late failure, its work picked up after a restart, its work the user
// stopped, or the end of its work shows as an event of the round it opens.
if (source.failed !== undefined || source.resumed === true || source.stopped === true || source.done === true) return null
diff --git a/ds-bot/src/host/groups.js b/ds-bot/src/host/groups.js
index 6a2625c3..6149573b 100644
--- a/ds-bot/src/host/groups.js
+++ b/ds-bot/src/host/groups.js
@@ -500,12 +500,13 @@ export function install(rt) {
const queued = rt.roomInbox.get(room.id) ?? []
rt.roomInbox.delete(room.id)
const posts = queued.length > 0 ? queued : [{ text: '' }]
- const startSeq = room.seq + 1
+ // A post a running round already logged stays inside the round this relay opens.
+ const startSeq = Math.min(room.seq + 1, ...posts.map(post => post.logged).filter(seq => typeof seq === 'number'))
// The opening greeting is a cue for the admin, not a line of the group; a member's
// late failure, its work picked up after a restart, its work the user stopped, or
// the end of work whose result it already posted is an event of the round.
const event = item => item.failed || item.resumed || item.stopped || item.done
- for (const post of posts.filter(item => !item.kickoff && !event(item))) {
+ for (const post of posts.filter(item => !item.kickoff && !event(item) && item.logged === undefined)) {
// Image readings go into the log only; `@` names are picked from what the user typed.
// A file stays with its line and reaches each member the line is shown to.
const readings = post.images === undefined ? [] : await rt.imagesForGroup(post.images, room.id)
@@ -542,14 +543,14 @@ export function install(rt) {
failed: [],
started: [],
}
- for (const post of posts.filter(item => item.failed)) {
+ for (const post of posts.filter(item => item.failed && item.logged === undefined)) {
const bot = rt.botOf(post.botId)
const { code, status, message } = post.failed
round.failed.push(bot?.name ?? post.name)
round.replies.push({ kind: 'failed', botId: post.botId, name: bot?.name ?? post.name, color: bot?.color, code, ...(status ? { status } : {}), text: message, time: Date.now() })
}
// The log line tells the admin and the other members on their next turn.
- for (const post of posts.filter(item => item.resumed || item.stopped || item.done)) {
+ for (const post of posts.filter(item => (item.resumed || item.stopped || item.done) && item.logged === undefined)) {
const bot = rt.botOf(post.botId)
const name = bot?.name ?? post.name
const kind = post.done ? 'done' : post.stopped ? 'stopped' : post.again ? 'cut' : 'resumed'
@@ -684,11 +685,37 @@ export function install(rt) {
}
// A Bot's message into the group's own Session, which relays it as a new round.
+ // While a round runs, the post goes into the group's log and that round's replies at
+ // once, so it is not held back until the round ends; the relay it still opens does not
+ // write it again (source `logged`).
async function postToRoom(room, sender, text, extra = {}, files = []) {
+ const running = [...rt.roomProgress.values()].find(round => round.roomId === room.id)
+ let logged
+ if (running !== undefined) {
+ ensureRoomLog(room)
+ if (extra.failed !== undefined) {
+ // A failed model call is an event of the round only: no log line, as the relay does it.
+ const { code, status, message } = extra.failed
+ running.failed.push(sender.name)
+ running.replies.push({ kind: 'failed', botId: sender.id, name: sender.name, color: sender.color, code, ...(status ? { status } : {}), text: message, time: Date.now() })
+ logged = true
+ } else if (extra.resumed === true || extra.stopped === true || extra.done === true) {
+ const kind = extra.done === true ? 'done' : extra.stopped === true ? 'stopped' : extra.again === true ? 'cut' : 'resumed'
+ appendRoom(room, { who: sender.name, botId: sender.id, kind, text: '' })
+ running.replies.push({ kind, botId: sender.id, name: sender.name, color: sender.color, time: Date.now() })
+ logged = room.seq
+ } else {
+ const line = [text, ...files.map(fileLine)].filter(Boolean).join('\n')
+ appendRoom(room, { who: sender.name, botId: sender.id, text: line, ...(files.length > 0 ? { files } : {}) })
+ running.replies.push({ botId: sender.id, name: sender.name, color: sender.color, text: line, time: Date.now() })
+ logged = room.seq
+ }
+ await rt.save()
+ }
const agent = await rt.resolveAgent(room.id)
agent.followup(createUserMessage({
content: [{ type: 'text', text: `[Group post from ${sender.name}]\n${text}` }, ...files.map(attachment => ({ type: 'file', attachment }))],
- source: { kind: SOURCE_KIND, form: 'relay', role: 'post', exchangeId: randomUUID(), senderName: sender.name, senderSessionId: sender.id, roomId: room.id, ...extra },
+ source: { kind: SOURCE_KIND, form: 'relay', role: 'post', exchangeId: randomUUID(), senderName: sender.name, senderSessionId: sender.id, roomId: room.id, ...extra, ...(logged !== undefined ? { logged } : {}) },
}))
await ctx.sessions.flush(agent.session)
}
diff --git a/ds-bot/src/host/turns.js b/ds-bot/src/host/turns.js
index 141ba8e2..dbdb4c23 100644
--- a/ds-bot/src/host/turns.js
+++ b/ds-bot/src/host/turns.js
@@ -445,6 +445,9 @@ export function install(rt) {
...(source.stopped ? { stopped: true } : {}),
...(source.done ? { done: true } : {}),
...(source.askedBy ? { askedBy: source.askedBy } : {}),
+ // A post written into a running round already carries its log seq, or true
+ // when its event needed none; the relay must not write it again.
+ ...(source.logged !== undefined ? { logged: source.logged } : {}),
})
} else if (source?.kind === SOURCE_KIND && source.role === 'kickoff' && rt.roomOf(id)) {
queueRoomMessage(id, { text: '', kickoff: true })
diff --git a/ds-bot/test/group-work.test.js b/ds-bot/test/group-work.test.js
index ee54fbeb..afdad5b7 100644
--- a/ds-bot/test/group-work.test.js
+++ b/ds-bot/test/group-work.test.js
@@ -4,8 +4,8 @@ import { tmpdir } from 'node:os'
import { join } from 'node:path'
import { test } from 'node:test'
import { apply } from '../index.js'
-import { fakeContext } from './fake-ctx.js'
-import { ROOM_LATE_ROUNDS, ROOM_STEP_GAP_MS, ROOM_WORK_GRACE_MS } from '../src/host/constants.js'
+import { fakeAttachments, fakeContext } from './fake-ctx.js'
+import { MESSAGE_INLINE_MAX, ROOM_LATE_ROUNDS, ROOM_STEP_GAP_MS, ROOM_WORK_GRACE_MS } from '../src/host/constants.js'
// Chief, the admin of group 'r1', speaks there from 'ga'; Writer from 'gb'; Tester from 'gc'.
const seed = (room = {}) => ({
@@ -25,10 +25,10 @@ const seed = (room = {}) => ({
revision: 0,
})
-async function start(t, room) {
+async function start(t, room, services) {
const home = mkdtempSync(join(tmpdir(), 'ds-bot-work-'))
writeFileSync(join(home, 'state.json'), JSON.stringify(seed(room)))
- const ctx = fakeContext()
+ const ctx = fakeContext({ services })
const agents = new Map()
const agentOf = (sessionId) => {
if (!agents.has(sessionId)) {
@@ -391,6 +391,158 @@ test('the late round limit makes a late post quiet', async (t) => {
assert.equal(host.takePost().source.quiet, true)
})
+// #198: a post during a running round waited for the round's own relay before
+// anyone could see it. It is written into the log and the running round at once,
+// and the relay it still opens shows it again nowhere.
+const logLines = host => JSON.parse(readFileSync(join(host.home, 'state.json'), 'utf8')).rooms.r1.log
+const busyRound = async (t, room, services) => {
+ const host = await start(t, room, services)
+ const first = host.round('c1', host.say('@Writer 整理文档'))
+ await settle(() => host.agentOf('gb').calls.length === 1)
+ host.begin('gb')
+ host.emit('gb', 'tool/call', { callId: 'k1', name: 'pwsh', arguments: '{}' })
+ await settle(() => true)
+ return { host, first }
+}
+
+test('a post during a running round is in the log at once, once, and shows in the round', async (t) => {
+ const { host, first } = await busyRound(t)
+ await host.tool('post_to_group', { group: 'Ops', message: '@Tester 看一下文档' }, 'a')
+ const during = logLines(host)
+ const logged = during.at(-1)
+ assert.deepEqual([logged.who, logged.botId, logged.text], ['Chief', 'a', '@Tester 看一下文档'])
+ const post = host.takePost()
+ assert.equal(post.source.logged, logged.seq)
+ host.finish('gb', '整理好了。')
+ await settle(() => first() !== undefined)
+ assert.ok(first().replies.some(reply => reply.botId === 'a' && reply.text === '@Tester 看一下文档'))
+
+ // The relay the post opened skips its line but still calls the member it named.
+ const second = host.round('c2', post)
+ await settle(() => host.agentOf('gc').calls.length === 1)
+ assert.equal(host.begin('gc').source.askedBy, 'a')
+ assert.match(text(host.agentOf('gc').calls[0]), /Chief: @Tester 看一下文档/)
+ host.finish('gc', '看过了。')
+ await settle(() => second() !== undefined)
+ assert.equal(logLines(host).filter(line => line.text === '@Tester 看一下文档').length, 1)
+})
+
+test('a post during a running round with report_back reports only the round it opened', async (t) => {
+ const { host, first } = await busyRound(t)
+ await host.tool('post_to_group', { group: 'Ops', message: '@Tester 看一下文档', report_back: true }, 'a')
+ const post = host.takePost()
+ assert.equal(typeof post.source.logged, 'number')
+ host.finish('gb', '整理好了。')
+ await settle(() => first() !== undefined)
+ const second = host.round('c2', post)
+ await settle(() => host.agentOf('gc').calls.length === 1)
+ host.begin('gc')
+ host.finish('gc', '看过了。')
+ await settle(() => second() !== undefined)
+ await settle(() => host.agentOf('a').calls.length === 1)
+ const report = host.agentOf('a').calls[0]
+ assert.match(text(report), /Tester: 看过了。/)
+ assert.doesNotMatch(text(report), /看一下文档/)
+})
+
+test('a late reply during a running round is logged at once and not repeated', async (t) => {
+ t.mock.timers.enable({ apis: ['setTimeout'] })
+ const host = await start(t)
+ const first = host.round('c1', host.say('@Chief 安排一下文档'))
+ await settle(() => host.agentOf('ga').calls.length === 1)
+ host.begin('ga')
+ host.finish('ga', '@Writer 把文档整理好')
+ await settle(() => host.agentOf('gb').calls.length === 1)
+ host.begin('gb')
+ host.emit('gb', 'tool/call', { callId: 'k1', name: 'pwsh', arguments: '{}' })
+ // The round moves on without Writer; its result lands while a second round runs.
+ t.mock.timers.tick(ROOM_WORK_GRACE_MS)
+ await settle(() => first() !== undefined)
+ const second = host.round('c2', host.say('@Chief 再确认一下'))
+ await settle(() => host.agentOf('ga').calls.length === 2)
+ host.begin('ga')
+ host.emit('gb', 'tool/result', { callId: 'k1' })
+ host.finish('gb', '整理好了,在 docs/ 下。')
+ await settle(() => host.agentOf('r1').calls.length === 1)
+ const post = host.takePost()
+ assert.equal(post.source.late, true)
+ assert.equal(typeof post.source.logged, 'number')
+ assert.ok(logLines(host).some(line => line.who === 'Writer' && line.text === '整理好了,在 docs/ 下。'))
+ host.finish('ga', '确认过了。')
+ await settle(() => second() !== undefined)
+ assert.ok(second().replies.some(reply => reply.botId === 'b' && reply.text === '整理好了,在 docs/ 下。'))
+
+ // The relay it opens still calls the member that asked, and writes the line nowhere again.
+ const third = host.round('c3', post)
+ await settle(() => host.agentOf('ga').calls.length === 3)
+ host.begin('ga')
+ host.finish('ga', '[PASS]')
+ await settle(() => third() !== undefined)
+ assert.equal(logLines(host).filter(line => line.text === '整理好了,在 docs/ 下。').length, 1)
+})
+
+test('a member stopped during a running round is logged at once and not repeated', async (t) => {
+ const host = await start(t)
+ const first = host.round('c1', host.say('@Writer @Tester 整理文档'))
+ await settle(() => host.agentOf('gb').calls.length === 1)
+ host.begin('gb')
+ host.emit('gb', 'tool/call', { callId: 'k1', name: 'pwsh', arguments: '{}' })
+ // The user stops Writer while its turn still runs inside the round.
+ const stopped = await host.call('stop-member-work', { roomId: 'r1', botId: 'b' })
+ assert.deepEqual([stopped.ok, stopped.value], [true, true])
+ const last = logLines(host).at(-1)
+ assert.deepEqual([last.who, last.botId, last.kind], ['Writer', 'b', 'stopped'])
+ const post = host.takePost()
+ assert.equal(post.source.stopped, true)
+ assert.equal(typeof post.source.logged, 'number')
+ host.emit('gb', 'turn/end', { turn: 1, reason: { kind: 'aborted' } })
+ await settle(() => host.agentOf('gc').calls.length === 1)
+ host.begin('gc')
+ host.finish('gc', '在整理了。')
+ await settle(() => first() !== undefined)
+ assert.ok(first().replies.some(reply => reply.kind === 'stopped' && reply.botId === 'b'))
+
+ // The relay the stopped post opens writes no second event line.
+ const second = host.round('c2', post)
+ await settle(() => second() !== undefined)
+ assert.equal(logLines(host).filter(line => line.kind === 'stopped').length, 1)
+})
+
+test('a Main Bot outside the group posts into its running round, and a long post keeps its file line', async (t) => {
+ const { host, first } = await busyRound(t,
+ { members: ['b', 'c'], admin: 'b', sessions: { b: 'gb', c: 'gc' } },
+ { attachments: fakeAttachments() })
+ const long = '很长的一段。'.repeat(MESSAGE_INLINE_MAX)
+ await host.tool('post_to_group', { group: 'Ops', message: long }, 'a')
+ const line = logLines(host).at(-1)
+ assert.deepEqual([line.who, line.botId], ['Chief', 'a'])
+ assert.equal(line.files?.length, 1)
+ assert.match(line.text, /\[File "Post from Chief\.txt"/)
+ const post = host.takePost()
+ assert.equal(typeof post.source.logged, 'number')
+ host.finish('gb', '整理好了。')
+ await settle(() => first() !== undefined)
+ const second = host.round('c2', post)
+ await settle(() => host.agentOf('gb').calls.length === 2)
+ host.begin('gb')
+ host.finish('gb', '收到,看一下。')
+ await settle(() => second() !== undefined)
+ assert.equal(logLines(host).filter(entry => entry.files?.length === 1).length, 1)
+})
+
+test('a post with no round running keeps the old way: queued, no logged mark', async (t) => {
+ const host = await start(t)
+ await host.tool('post_to_group', { group: 'Ops', message: '@Writer 看一下文档' }, 'a')
+ const post = host.takePost()
+ assert.equal(post.source.logged, undefined)
+ const first = host.round('c1', post)
+ await settle(() => host.agentOf('gb').calls.length === 1)
+ host.begin('gb')
+ host.finish('gb', '看了。')
+ await settle(() => first() !== undefined)
+ assert.equal(logLines(host).filter(line => line.text === '@Writer 看一下文档').length, 1)
+})
+
// Such a message was sent before message_bot from a group stayed in that group.
test('a reply to a message a group Session sent comes back there, and its turn posts in the group', async (t) => {
const host = await start(t)