A team of long-lived bots for DeepSeek Harness, with main bots, bot-to-bot messages and group chats.
Harden IM sync, cards and the own-chat approval race
Fei241185c7e0cef4cd
diff --git a/ds-bot/src/host/approvals.js b/ds-bot/src/host/approvals.jsindex 76440578..246e48c9 100644--- a/ds-bot/src/host/approvals.js+++ b/ds-bot/src/host/approvals.js@@ -57,7 +57,7 @@ export function install(rt) {const original = request.signalconst linked = new AbortController()request.signal = linked.signal- return new Promise((resolve) => {+ return new Promise((resolve, reject) => {const complete = (outcome) => {if (!settle(outcome)) returnresolve(outcome)@@ -67,10 +67,20 @@ export function install(rt) {entry.answer = completeif (original?.aborted) entry.answer('cancelled')else original?.addEventListener('abort', () => entry.answer('cancelled'), { once: true })- Promise.resolve(next()).then((outcome) => {- if (!pending.has(id)) return- complete(outcome)- })+ Promise.resolve(next()).then(+ (outcome) => {+ if (!pending.has(id)) return+ complete(outcome)+ },+ // DSH's answerer threw: close the entry and give the caller the error+ // the way the pre-race code did, instead of hanging forever.+ (error) => {+ if (!settle('cancelled')) return+ reject(error)+ linked.abort()+ if (request.signal === linked.signal) request.signal = original+ },+ )})}rt.yieldRoomTurn?.(sessionId)diff --git a/ds-bot/src/host/im/index.js b/ds-bot/src/host/im/index.jsindex 69d1df58..ee0bde86 100644--- a/ds-bot/src/host/im/index.js+++ b/ds-bot/src/host/im/index.js@@ -6,6 +6,7 @@ import { randomBytes, randomUUID } from 'node:crypto'import { chmod, mkdir, readFile, rm, writeFile } from 'node:fs/promises'import { join } from 'node:path'import { replaceFile } from '../files.js'+import { viewCardText } from '../parts.js'import { imPlatform, IM_PLATFORMS } from '../../shared/im-platforms.js'import { adapters } from './platforms/index.js'@@ -130,11 +131,11 @@ export function install(rt) {for (const [key, name] of Object.entries(conn.secrets ?? {})) fields[key] = rt.secretValue(name)return fields}- const envFor = async (conn) => ({+ const envFor = async (conn, runtime) => ({fetch: globalThis.fetch,WebSocket: globalThis.WebSocket,config: rt.config.im?.[conn.platform],- runtime: await runtimeEnv(conn.id),+ runtime: runtime ?? await runtimeEnv(conn.id),log: { warn: (template, ...args) => rt.warn(template, ...args) },uploadFile: async (sessionId, file) => {const uploads = ctx.get?.('fileUploads')@@ -182,7 +183,9 @@ export function install(rt) {const drain = async (conn, chatKey) => {const runner = runners.get(conn.id)const queue = runner?.queues.get(chatKey)- if (runner === undefined || queue === undefined) return+ // A reserved slot has no handle until connect() lands; start() re-kicks+ // anything queued in the meantime.+ if (runner?.handle === undefined || queue === undefined) returnwhile (queue.jobs.length > 0) {const job = queue.jobs[0]try {@@ -287,6 +290,19 @@ export function install(rt) {}const sendCardReply = async (conn, chat, body) => sendText(conn, { key: chat.key, kind: chat.kind, title: chat.title }, body)+ // The chat's newest still-open card, questions and approvals together: a bare+ // number answers whatever arrived last, not always a question.+ const newestOpenCard = (conn, chatKey) => {+ const data = runtimes.get(conn.id)?.data+ if (data === undefined) return undefined+ for (const [cardKey, entries] of Object.entries(data.cards).reverse()) {+ if (!cardKey.startsWith('q:') && !cardKey.startsWith('a:')) continue+ const entry = entries.find(item => item.chatKey === chatKey && item.answer === undefined)+ if (entry !== undefined) return { cardKey, entry }+ }+ return undefined+ }+const onMessage = (conn) => async (msg) => {if (!paired(conn, msg.sender.id)) {if (msg.chat.kind === 'group' && !msg.mentioned) return@@ -320,21 +336,22 @@ export function install(rt) {}if (NUMBERS.test(body)) {const picked = body.trim().split(/\s+/).map(Number)- const held = openCard(conn, msg.chat.key, 'q') ?? openCard(conn, msg.chat.key, 'a')- if (held !== undefined) {- if (held.cardKey.startsWith('q:')) {- const found = questionById(held.cardKey.slice(2))- if (found !== undefined) {- const options = found.question.options ?? []- const labels = (found.question.multiSelect === true ? picked : picked.slice(0, 1))- .map(index => options[index - 1]?.label)- if (labels.every(label => label !== undefined) && labels.length > 0 && await answerQuestionCard(conn, msg.chat, labels)) return- if (labels.length > 0 && labels.every(label => label !== undefined)) return- if (await answerQuestionCard(conn, msg.chat, [])) return- } else if (await sendCardReply(conn, msg.chat, text('stale')).then(() => true)) return- } else if (picked.length === 1 && (picked[0] === 1 || picked[0] === 2)) {- if (await answerApprovalCard(conn, msg.chat, picked[0] === 1 ? 'allow' : 'reject')) return+ const held = newestOpenCard(conn, msg.chat.key)+ // Anything that does not clearly answer the newest open card — a stale+ // source, an out-of-range pick, several picks on a single choice — is a+ // normal message for the Bot, never swallowed and never 'stale'.+ if (held !== undefined && held.cardKey.startsWith('q:')) {+ const found = questionById(held.cardKey.slice(2))+ if (found !== undefined) {+ const options = found.question.options ?? []+ const inRange = picked.length > 0 && (found.question.multiSelect === true || picked.length === 1)+ && picked.every(index => options[index - 1] !== undefined)+ if (inRange && await answerQuestionCard(conn, msg.chat, picked.map(index => options[index - 1].label))) return}+ } else if (held !== undefined && held.cardKey.startsWith('a:')+ && picked.length === 1 && (picked[0] === 1 || picked[0] === 2)+ && rt.approvalsView().some(entry => `a:${entry.id}` === held.cardKey)) {+ if (await answerApprovalCard(conn, msg.chat, picked[0] === 1 ? 'allow' : 'reject')) return}}if (target === undefined) {@@ -438,7 +455,8 @@ export function install(rt) {const botId = rt.chatOwners.get(sessionId)const bot = rt.botOf(botId)if (bot === undefined) continue- await sendCard(conn, cardKey, listeners(conn, { botId }), `${text('viewShow', { bot: bot.name })}${view.title ? `\n${view.title}` : ''}`)+ const form = viewCardText({ title: view.title, view: view.view })+ await sendCard(conn, cardKey, listeners(conn, { botId }), `${text('viewShow', { bot: bot.name })}${form !== '' ? `\n${form}` : view.title ? `\n${view.title}` : ''}`)}}}@@ -469,12 +487,12 @@ export function install(rt) {const openIds = new Set(Object.values(state.questions ?? {}).map(question => `q:${question.id}`))const openSecret = new Set(Object.values(state.secretRequests ?? {}).flat().filter(entry => entry.status === 'pending').map(entry => `s:${entry.id}`))const openViews = new Set(Object.values(state.views ?? {}).map(view => `v:${view.id}`))+ const openCard = cardKey => cardKey.startsWith('q:') ? openIds.has(cardKey)+ : cardKey.startsWith('a:') ? rt.approvalsView().some(entry => `a:${entry.id}` === cardKey)+ : cardKey.startsWith('s:') ? openSecret.has(cardKey)+ : cardKey.startsWith('v:') ? openViews.has(cardKey) : falsefor (const [cardKey, entries] of Object.entries(data.cards)) {- const open = cardKey.startsWith('q:') ? openIds.has(cardKey)- : cardKey.startsWith('a:') ? rt.approvalsView().some(entry => `a:${entry.id}` === cardKey)- : cardKey.startsWith('s:') ? openSecret.has(cardKey)- : cardKey.startsWith('v:') ? openViews.has(cardKey) : false- if (open) continue+ if (openCard(cardKey)) continuefor (const entry of entries) {if (entry.answer === undefined) {entry.answer = text('answered')@@ -486,6 +504,20 @@ export function install(rt) {scheduleWrite(conn.id)await settledEdits(conn)}+ // A card whose source is gone stays only until its copies are settled and+ // edited where the platform can edit; keeping it forever grows the file.+ const editable = runners.get(conn.id)?.handle?.edit !== undefined+ let pruned = false+ for (const [cardKey, entries] of Object.entries(data.cards)) {+ if (openCard(cardKey)) continue+ const droppable = entries.length === 0 || entries.every(entry => entry.answer !== undefined+ && (entry.edited === true || entry.messageId === undefined || !editable || conn.chats?.[entry.chatKey] === undefined))+ if (droppable) {+ delete data.cards[cardKey]+ pruned = true+ }+ }+ if (pruned) scheduleWrite(conn.id)}}@@ -510,18 +542,20 @@ export function install(rt) {const start = async (conn) => {const adapter = adapters[conn.platform]- if (adapter === undefined) return+ if (adapter === undefined || runners.has(conn.id)) returnconst platform = imPlatform(conn.platform)if (platform?.ws === true && globalThis.WebSocket === undefined) {setStatus(conn.id, { state: 'error', error: 'This dsh runs on a Node without WebSocket', at: Date.now() })return}- setStatus(conn.id, { state: 'connecting', at: Date.now() })- const env = await envFor(conn)+ // The slot is reserved before the async work: a concurrent sync cannot+ // start a second adapter for the same connection.const runner = { handle: undefined, queues: new Map(), typing: undefined }runners.set(conn.id, runner)+ setStatus(conn.id, { state: 'connecting', at: Date.now() })try {- runner.handle = adapter.connect(fieldsOf(conn), env, {+ const env = await envFor(conn)+ const handle = adapter.connect(fieldsOf(conn), env, {onMessage: (msg) => {const current = fresh(conn.id)return current === undefined ? undefined : onMessage(current)(msg).catch(error => rt.warn('ds-bot: im message failed: %s', error))@@ -532,6 +566,14 @@ export function install(rt) {},onStatus: onStatus(conn.id),})+ // A stop() during connect took the slot back: undo the just-started+ // adapter instead of leaving it running unreachable.+ if (runners.get(conn.id) !== runner) {+ void Promise.resolve(handle?.stop?.()).catch(() => {})+ return+ }+ runner.handle = handle+ for (const chatKey of runner.queues.keys()) void drain(conn, chatKey)if (typeof runner.handle?.typing === 'function') {runner.typing = setInterval(() => {for (const { chat, target } of targetsOf(fresh(conn.id) ?? conn)) {@@ -555,40 +597,48 @@ export function install(rt) {if (statuses.get(id)?.state !== 'error') setStatus(id, { state: 'off', at: Date.now() })}- const sync = async () => {- const conns = connections()- for (const conn of Object.values(conns)) {- const wants = stopped !== true && adapters[conn.platform] !== undefined && conn.enabled !== false- && rt.readOnly() === undefined && hasSecrets(conn)- if (wants && !runners.has(conn.id)) await start(conn)- else if (!wants && runners.has(conn.id)) await stop(conn.id)- else if (wants && conn.enabled === false) await stop(conn.id)- }- for (const id of [...runners.keys()]) if (conns[id] === undefined) await stop(id)- for (const id of [...statuses.keys()]) if (conns[id] === undefined) statuses.delete(id)- for (const conn of Object.values(conns)) {- if (conn.enabled === false || !runners.has(conn.id)) {- if (statuses.get(conn.id)?.state !== 'error') setStatus(conn.id, { state: 'off', at: Date.now() })+ // Syncs run one behind the other: endpoints, onSaved and the interval can all+ // fire while a sync is still starting an adapter, and they must see its+ // reserved slots rather than start a second adapter.+ let syncing = Promise.resolve()+ const sync = () => {+ const run = syncing.then(async () => {+ const conns = connections()+ for (const conn of Object.values(conns)) {+ const wants = stopped !== true && adapters[conn.platform] !== undefined && conn.enabled !== false+ && rt.readOnly() === undefined && hasSecrets(conn)+ if (wants && !runners.has(conn.id)) await start(conn)+ else if (!wants && runners.has(conn.id)) await stop(conn.id)}- }+ for (const id of [...runners.keys()]) if (conns[id] === undefined) await stop(id)+ for (const id of [...statuses.keys()]) if (conns[id] === undefined) statuses.delete(id)+ for (const conn of Object.values(conns)) {+ if (conn.enabled === false || !runners.has(conn.id)) {+ if (statuses.get(conn.id)?.state !== 'error') setStatus(conn.id, { state: 'off', at: Date.now() })+ }+ }+ })+ syncing = run.catch(() => {})+ return run}let stopped = false// ---------------------------------------------------------------- endpointsconst secretName = (conn, key) => `IM_${conn.platform.toUpperCase()}_${conn.id.slice(3, 9).toUpperCase()}_${secretField(key)}`- const cleanFields = (platform, fields) => {+ const cleanFields = (platform, fields, base = {}) => {const given = fields !== null && typeof fields === 'object' ? fields : {}const out = {}for (const field of platform.fields) {const value = given[field.key]if (field.options !== undefined) {- out[field.key] = field.options.some(([id]) => id === value) ? value : field.options[0][0]+ out[field.key] = field.options.some(([id]) => id === value) ? value+ : (value === undefined && base[field.key] !== undefined ? base[field.key] : field.options[0][0])} else if (typeof value === 'string' && value.trim() !== '') {out[field.key] = value.trim()} else if (!field.secret) {if (value !== undefined && typeof value !== 'string') throw new Error(`${field.label} must be text`)- out[field.key] = ''+ out[field.key] = value === undefined && base[field.key] !== undefined ? base[field.key] : ''}// Missing secret fields are allowed only on update, where the saved one stays.}@@ -627,9 +677,9 @@ export function install(rt) {if (platform.login === 'qr') throw new Error('This platform signs in by scanning a code; not available yet')const fields = cleanFields(platform, body.fields)for (const field of platform.fields) {- if (!field.options && fields[field.key] === undefined) throw new Error(`${field.label} is required`)+ if (!field.options && (fields[field.key] === undefined || String(fields[field.key]).trim() === '')) throw new Error(`${field.label} is required`)}- const env = await envFor({ id: 'im_check', platform: platform.id, settings: fields, secrets: {} })+ const env = await envFor({ id: 'im_check', platform: platform.id, settings: fields, secrets: {} }, { get: () => ({}), set: () => {} })const { account } = await adapters[platform.id].check(fields, env)const conn = {id: `im_${randomBytes(4).toString('hex')}`,@@ -662,7 +712,7 @@ export function install(rt) {}if (body.enabled !== undefined) conn.enabled = body.enabled === trueif (body.fields !== undefined) {- const fields = cleanFields(platform, body.fields)+ const fields = cleanFields(platform, body.fields, conn.settings)const env = await envFor(conn)const check = { ...fields }for (const field of platform.fields) {@@ -771,13 +821,13 @@ export function install(rt) {if (botId === undefined) returnfor (const item of rt.displayItems(session.id, [event])) {if (item.kind !== 'bot' && item.kind !== 'view') continue- const body = item.kind === 'bot' ? item.text : `📋 ${item.title ?? ''}`+ const body = item.kind === 'bot' ? item.text : viewCardText({ title: item.title, view: item.view }) || (item.title ?? '')for (const conn of Object.values(connections())) {if (!runners.has(conn.id)) continuefor (const { chatKey, chat } of listeners(conn, { botId })) sendTo(conn, chatKey, { key: chatKey, kind: chat.kind, title: chat.title }, body)}}- }, { prepend: true })+ })rt.onRoomLine((room, line) => {if (line.botId === undefined || line.kind !== undefined || typeof line.text !== 'string' || line.text.trim() === '') returndiff --git a/ds-bot/src/host/im/platforms/telegram.js b/ds-bot/src/host/im/platforms/telegram.jsindex 142f1c85..af988b62 100644--- a/ds-bot/src/host/im/platforms/telegram.js+++ b/ds-bot/src/host/im/platforms/telegram.js@@ -33,13 +33,16 @@ export default {const base = `${apiBase(env)}/bot${token}`const fileBase = `${apiBase(env)}/file/bot${token}`let stopped = false- let abort+ // Every call in flight keeps its own controller, so stop() ends them all —+ // a long poll must not outlive the adapter by ~65 s and collide with the+ // restarted one (Telegram answers 409).+ const inFlight = new Set()let selfName = ''const call = async (method, params, { download = false, timeout = 30_000 } = {}) => {- // Only stop() aborts the call in flight; a send or a callback answer must not- // kill a long poll that is still waiting.- abort = new AbortController()+ const abort = new AbortController()+ inFlight.add(abort)+ if (stopped) abort.abort(stoppedError())const timer = setTimeout(() => abort.abort(new Error('timeout')), timeout)try {const response = await env.fetch(download ? `${fileBase}/${method}` : `${base}/${method}`, download ? { signal: abort.signal } : {@@ -71,6 +74,7 @@ export default {throw error} finally {clearTimeout(timer)+ inFlight.delete(abort)}}const stoppedError = () => Object.assign(new Error('stopped'), { stopped: true })@@ -223,7 +227,7 @@ export default {typing: chat => call('sendChatAction', { chat_id: chat.key, action: 'typing' }).catch(() => {}),stop: () => {stopped = true- abort?.abort()+ for (const abort of inFlight) abort.abort(stoppedError())},}},diff --git a/ds-bot/test/bot-messages.test.js b/ds-bot/test/bot-messages.test.jsindex e31275e8..b29c6e0b 100644--- a/ds-bot/test/bot-messages.test.js+++ b/ds-bot/test/bot-messages.test.js@@ -1042,7 +1042,7 @@ for (const id of ['b', 'gb']) {ctx.llm.resolveModelInfo = async () => ({ context: { contextWindow: 192_000 } })const preStep = async (agent) => {agent.session.requestHeader = () => ({ config: { provider: 'custom', model: 'model' } })- return ctx.listeners.get('agent/pre-step').at(0)({ agent, messages: [], step: 1, signal: AbortSignal.timeout(60_000) }, async () => 'next')+ return ctx.listeners.get('agent/pre-step').at(-1)({ agent, messages: [], step: 1, signal: AbortSignal.timeout(60_000) }, async () => 'next')}const overflow = agent => fire('agent/request-error', { agent, failure: { code: 'CONTEXT_WINDOW_EXCEEDED', message: 'Tools exceed the context window' }, signal: AbortSignal.timeout(60_000) })const waitForHandoff = async (sessionId) => {@@ -1122,7 +1122,7 @@ for (const id of ['b', 'gb']) {assert.ok(ctx.get('tokenMeter').measure(agent.session).totalTokens + estimateTokens(textOf(input.content)) > 50_000, 'it overflows together with the handoff')if (pending) {- const decision = await ctx.listeners.get('agent/pre-step').at(0)({ agent, messages: [input], step: 1, signal: AbortSignal.timeout(60_000) }, async () => 'next')+ const decision = await ctx.listeners.get('agent/pre-step').at(-1)({ agent, messages: [input], step: 1, signal: AbortSignal.timeout(60_000) }, async () => 'next')assert.deepEqual(decision, { kind: 'enter', messages: [] })} else {record('user/message', input)diff --git a/ds-bot/test/fake-ctx.js b/ds-bot/test/fake-ctx.jsindex 1825f1b2..9e9a995f 100644--- a/ds-bot/test/fake-ctx.js+++ b/ds-bot/test/fake-ctx.js@@ -48,7 +48,7 @@ export function fakeContext({ services = {} } = {}) {get: name => services[name],on(name, listener, options) {calls.push(`on ${name}${options?.prepend ? ' (prepend)' : ''}`)- listeners.set(name, options?.prepend ? [listener, ...(listeners.get(name) ?? [])] : [...(listeners.get(name) ?? []), listener])+ listeners.set(name, [...(listeners.get(name) ?? []), listener])return () => {}},// Calls the plain listeners of an event; middleware (with `next`) is not run.diff --git a/ds-bot/test/feed.test.js b/ds-bot/test/feed.test.jsindex 4a68e21e..d1f01b25 100644--- a/ds-bot/test/feed.test.js+++ b/ds-bot/test/feed.test.js@@ -88,8 +88,8 @@ test('the live feed tells a window about saves, new chat entries and the reply bassert.equal(saved.event, 'revision')assert.equal(saved.data.revision > TEAM.revision, true)- // The feed's own listener is the last on session/event; the others need a live dsh.- const onEvent = ctx.listeners.get('session/event').at(-1)+ // The feed's own listener sits right before im's on session/event; the others need a live dsh.+ const onEvent = ctx.listeners.get('session/event').at(-2)onEvent({ id: 'm1c' }, userMessage('hello', 60))assert.deepEqual(await next(), { event: 'message', data: { botId: 'm1', sessionId: 'm1c', item: { kind: 'user', text: 'hello', time: 60 } } })// A group Session or an earlier part is not a Bot's own chat now.@@ -127,7 +127,7 @@ test('a window that stops reading the feed is dropped', { timeout: 10_000 }, asyt.after(stop)await call('state')const response = await routes['/api/bot/feed'].fetch(new Request('http://localhost/api/bot/feed'))- const onEvent = ctx.listeners.get('session/event').at(-1)+ const onEvent = ctx.listeners.get('session/event').at(-2)for (let index = 0; index < 1000; index += 1) onEvent({ id: 'm1c' }, userMessage(`line ${index}`, index))// What was queued before the drop still arrives, then the stream ends.const next = reader(response)@@ -218,7 +218,7 @@ test('view replay omits hidden, refused and interactive calls in pages and the lconst response = await routes['/api/bot/feed'].fetch(new Request('http://localhost/api/bot/feed', { signal: abort.signal }))const next = reader(response)assert.equal((await next()).event, 'hello')- const onEvent = ctx.listeners.get('session/event').at(-1)+ const onEvent = ctx.listeners.get('session/event').at(-2)for (const event of events) onEvent({ id: 'm1c' }, event)assert.deepEqual(await next(), { event: 'message', data: { botId: 'm1', sessionId: 'm1c', item: expected } })})diff --git a/ds-bot/test/group-status.test.js b/ds-bot/test/group-status.test.jsindex 0b3a3da8..1208d628 100644--- a/ds-bot/test/group-status.test.js+++ b/ds-bot/test/group-status.test.js@@ -75,7 +75,7 @@ async function start() {const askApproval = (sessionId, extra = {}, next = () => 'from-dsh') => ctx.listeners.get('approval/request')[0]({ agent: { session: { id: sessionId } }, toolName: 'pwsh', reason: 'escalate sandbox', ...extra }, next)const stop = async () => { await ctx.dispose(); rmSync(home, { recursive: true, force: true, maxRetries: 5 }) }await tool('list_bots', {}, 'a')- return { agentOf, emit, tool, call, askApproval, stop, archived }+ return { agentOf, emit, tool, call, askApproval, stop, archived, rt }}// Waits on real I/O (state writes) without timers, which some tests mock.@@ -151,6 +151,19 @@ test('an own chat\'s approval races DSH\'s card and is listed while it waits', aassert.equal(await host.askApproval('elsewhere'), 'from-dsh')})+test('an own chat\'s approval propagates a DSH answerer failure and settles', async (t) => {+ const host = await start()+ t.after(host.stop)+ const settled = []+ host.rt.onApproval((kind, payload) => { if (kind === 'settled') settled.push(payload) })+ const outcome = host.askApproval('a', {}, () => Promise.reject(new Error('dsh card broke')))+ const [approval] = host.rt.approvalsView()+ assert.equal(approval.answerable, true)+ await assert.rejects(outcome, /dsh card broke/)+ assert.deepEqual((await host.call('state', {})).value.work.approvals, [])+ assert.deepEqual(settled.map(entry => [entry.id, entry.outcome]), [[approval.id, 'cancelled']])+})+test('a member still going keeps its turn; one quiet for the whole wait is missed, and a later [PASS] stays out of the group', async (t) => {t.mock.timers.enable({ apis: ['setTimeout'] })const host = await start()diff --git a/ds-bot/test/im-telegram.test.js b/ds-bot/test/im-telegram.test.jsindex edad89d7..3d7c6d6b 100644--- a/ds-bot/test/im-telegram.test.js+++ b/ds-bot/test/im-telegram.test.js@@ -10,6 +10,7 @@ function fakeApi({ getMe = { id: 7, username: 'fakebot', is_bot: true } } = {})const updates = []const replies = {}const files = {}+ let outconst server = createServer((req, res) => {const url = new URL(req.url, 'http://x')if (url.pathname.startsWith('/file/bot')) {@@ -26,6 +27,10 @@ function fakeApi({ getMe = { id: 7, username: 'fakebot', is_bot: true } } = {})const [, , method] = url.pathname.split('/') // /bot<token>/<method>const params = JSON.parse(body || '{}')calls.push({ method, params })+ if (method === 'getUpdates' && out?.holdPoll === true && updates.length === 0) {+ out.heldPoll.push(res)+ return+ }let answer = replies[method]if (answer === undefined && method === 'getUpdates') answer = { result: updates.splice(0) }if (answer === undefined && method === 'getMe') answer = { result: getMe }@@ -43,7 +48,8 @@ function fakeApi({ getMe = { id: 7, username: 'fakebot', is_bot: true } } = {})})return new Promise(resolve => server.listen(0, '127.0.0.1', () => {const api = `http://127.0.0.1:${server.address().port}`- resolve({ api, calls, updates, replies, files, close: () => server.close() })+ out = { api, calls, updates, replies, files, holdPoll: false, heldPoll: [], close: () => server.close() }+ resolve(out)}))}@@ -157,3 +163,19 @@ test('429 is honored with retry_after and 409 becomes a status error', async ()handle.stop()} finally { api.close() }})++test('stop ends a long poll promptly, and a send in flight does not kill it', async () => {+ const api = await fakeApi()+ api.holdPoll = true+ const handle = telegram.connect({ token: 'tok' }, env(api.api), { onMessage: () => {}, onAction: () => {}, onStatus: () => {} })+ await until(() => api.calls.some(call => call.method === 'getUpdates'))+ // A send while the poll waits goes on its own controller; the poll stays.+ await handle.send({ key: '1', kind: 'direct' }, { text: 'hi' })+ assert.ok(api.calls.some(call => call.method === 'sendMessage'))+ assert.equal(api.heldPoll.length, 1)+ const before = api.calls.length+ await handle.stop()+ await new Promise(done => setTimeout(done, 150))+ assert.equal(api.calls.length, before, 'stop aborts the poll instead of retrying')+ await api.close()+})diff --git a/ds-bot/test/im.test.js b/ds-bot/test/im.test.jsindex cbc86524..58a48f9f 100644--- a/ds-bot/test/im.test.js+++ b/ds-bot/test/im.test.js@@ -26,6 +26,7 @@ const fakeAdapter = ({ buttons = true } = {}) => {connect(fields, env, handlers) {adapter.handlers = handlersadapter.env = env+ adapter.connects = (adapter.connects ?? 0) + 1handlers.onStatus({ state: 'ready', account: { id: 'bot1', name: '@fakebot' } })return {send: async (chat, body) => {@@ -117,6 +118,38 @@ const gm = (s, senderId, text, { mentioned = true, chat = { key: 'g1', kind: 'grchat, sender: { id: senderId, name: `User ${senderId}` }, text, mentioned, ...extra,})+test('two concurrent syncs start exactly one adapter', async (t) => {+ const s = await start()+ t.after(s.stop)+ await add(s)+ const conn = Object.values(s.rt.state.im.connections)[0]+ s.fake.connects = 0+ s.rt.state.im.connections[conn.id].enabled = false+ await s.rt.save()+ await s.flush()+ assert.equal(s.fake.stopped, 1)+ s.rt.state.im.connections[conn.id].enabled = true+ await Promise.all([s.rt.save(), s.rt.save()])+ await s.flush()+ assert.equal(s.fake.connects, 1)+ assert.equal(s.rt.imStatusView()[conn.id].state, 'ready')+})++test('im-update applies only the fields it is given and keeps the rest', async (t) => {+ const s = await start({ platform: 'feishu', adapter: fakeAdapter() })+ t.after(s.stop)+ const conn = await add(s, { domain: 'lark', appId: 'app-1', appSecret: 'sec-1' })+ const one = s.rt.state.im.connections[conn.id]+ assert.deepEqual(one.settings, { domain: 'lark', appId: 'app-1' })+ await s.rt.imEndpoint('im-update', { id: conn.id, fields: { appSecret: 'sec-2' } })+ assert.deepEqual(s.rt.state.im.connections[conn.id].settings, { domain: 'lark', appId: 'app-1' }, 'untouched fields keep their values')+ const view = s.rt.imView().connections.find(entry => entry.id === conn.id)+ assert.deepEqual(view.settings, { domain: 'lark', appId: 'app-1' })+ await s.rt.imEndpoint('im-update', { id: conn.id, fields: { domain: 'feishu' } })+ assert.equal(s.rt.state.im.connections[conn.id].settings.domain, 'feishu')+ assert.equal(s.rt.state.im.connections[conn.id].settings.appId, 'app-1')+})+test('unpaired DM asks once, allow pairs it, next message prompts the Bot', async (t) => {const s = await start()t.after(s.stop)@@ -223,6 +256,27 @@ test('question card: buttons answer it, a stranger press is ignored, settled elsassert.equal(s.fake.edited.length, 2)})+test('a settled card leaves the runtime file once its source is gone; an open one stays', async (t) => {+ const s = await start()+ t.after(s.stop)+ const conn = await add(s)+ const one = s.rt.state.im.connections[conn.id]+ one.paired.push({ id: 'u1', name: 'U1', at: 1 })+ await s.rt.save()+ await dm(s, 'u1', 'hi')+ s.rt.state.questions.a = { id: 'q1', question: 'Pick', options: [{ label: 'x' }, { label: 'y' }] }+ await s.rt.save()+ await s.flush()+ await s.fake.handlers.onAction({ chat: { key: 'dm-u1', kind: 'direct', title: 'U1' }, sender: { id: 'u1', name: 'U1' }, data: 'q:q1:0', messageId: 1 })+ s.rt.state.questions.a = { id: 'q2', question: 'Still open', options: [{ label: 'x' }] }+ await s.rt.save()+ await s.flush()+ await delay(600)+ const runtime = JSON.parse((await import('node:fs')).readFileSync(join(s.home, 'im', `${conn.id}.json`), 'utf8'))+ assert.ok(runtime.cards['q:q1'] === undefined, 'settled and edited card is dropped')+ assert.ok(runtime.cards['q:q2'] !== undefined, 'open card stays')+})+test('numbered reply answers the newest open card on a no-button platform', async (t) => {const s = await start({ platform: 'dingtalk', adapter: fakeAdapter({ buttons: false }) })t.after(s.stop)@@ -241,6 +295,51 @@ test('numbered reply answers the newest open card on a no-button platform', asynassert.equal(s.agents.get('a')?.followed, 1)})+test('a bare number answers the newest card, which can be an approval after a question', async (t) => {+ const s = await start({ platform: 'dingtalk', adapter: fakeAdapter({ buttons: false }) })+ t.after(s.stop)+ const conn = await add(s)+ const one = s.rt.state.im.connections[conn.id]+ one.paired.push({ id: 'u1', name: 'U1', at: 1 })+ await s.rt.save()+ await dm(s, 'u1', 'hi')+ s.rt.state.questions.a = { id: 'q1', question: 'Pick', options: [{ label: 'x' }, { label: 'y' }] }+ await s.rt.save()+ const racing = s.fireApprovals(+ { agent: { id: 'a' }, toolName: 'bash', reason: 'run it', signal: new AbortController().signal },+ () => new Promise(() => {}),+ )+ await s.flush()+ await dm(s, 'u1', '2')+ assert.equal((await racing)[0], 'rejected', 'the newer approval card takes the number')+ assert.ok(s.rt.state.questions.a !== undefined, 'the older question stays open')+})++test('an out-of-range or stale-card number goes to the Bot as a normal message', async (t) => {+ const s = await start({ platform: 'dingtalk', adapter: fakeAdapter({ buttons: false }) })+ t.after(s.stop)+ const conn = await add(s)+ const one = s.rt.state.im.connections[conn.id]+ one.paired.push({ id: 'u1', name: 'U1', at: 1 })+ await s.rt.save()+ await dm(s, 'u1', 'hi')+ s.rt.state.questions.a = { id: 'q1', question: 'Pick', options: [{ label: 'x' }, { label: 'y' }] }+ await s.rt.save()+ await s.flush()+ s.prompts.length = 0+ await dm(s, 'u1', '9')+ assert.ok(s.rt.state.questions.a !== undefined, 'out of range answers nothing')+ assert.equal(s.prompts.length, 1, 'the number reaches the Bot as a message')+ // Answered on the computer: the card is stale; a later number is a message too.+ delete s.rt.state.questions.a+ await s.rt.save()+ await s.flush()+ s.prompts.length = 0+ await dm(s, 'u1', '1')+ assert.equal(s.prompts.length, 1, 'the stale-card number reaches the Bot')+ assert.ok(!s.fake.sent.some(entry => /already answered/.test(entry.text ?? '')), 'no stale reply for a bare number')+})+test('own-chat approval races DSH: IM allow resolves and withdraws the card; late press gets the stale text', async (t) => {const s = await start()t.after(s.stop)diff --git a/ds-bot/test/install.test.js b/ds-bot/test/install.test.jsindex 34c27b16..9d23d80a 100644--- a/ds-bot/test/install.test.js+++ b/ds-bot/test/install.test.js@@ -100,7 +100,7 @@ test('apply registers listeners and effects in a fixed order', (t) => {'effect ds-bot: live feed route','route GET /api/bot/feed','effect ds-bot: live feed',- 'on session/event (prepend)',+ 'on session/event','effect ds-bot: im','effect ds-bot: api route','route POST /api/bot',diff --git a/ds-bot/test/scoped-tools-creation.test.js b/ds-bot/test/scoped-tools-creation.test.jsindex 3943d227..9711bf27 100644--- a/ds-bot/test/scoped-tools-creation.test.js+++ b/ds-bot/test/scoped-tools-creation.test.js@@ -259,7 +259,7 @@ test('a fresh part Session has the scoped tools before its first step', async (tconst moved = await host.tool('update_bot', { work_dir: project }, 'b')assert.equal(String(moved).includes('From your next step on'), true)// The next step sees the folder change and switches to a fresh part.- const preStep = host.ctx.listeners.get('agent/pre-step').at(0)+ const preStep = host.ctx.listeners.get('agent/pre-step').at(-1)const answer = await preStep({ agent: host.agentOf('b'), messages: [{ role: 'user', content: [{ type: 'text', text: 'go on' }], source: { kind: 'user' } }], step: 2, signal: AbortSignal.timeout(60_000) },async () => 'next',diff --git a/ds-bot/test/switch-keeps-input.test.js b/ds-bot/test/switch-keeps-input.test.jsindex b88ad1e8..dc941feb 100644--- a/ds-bot/test/switch-keeps-input.test.js+++ b/ds-bot/test/switch-keeps-input.test.js@@ -156,7 +156,7 @@ async function start({ fail = {}, services = {}, home: reuse } = {}) {method: 'POST', body: JSON.stringify({ endpoint, payload }),}))).json()const tool = (name, args, sessionId) => rt.teamTools.find(d => d.name === name).execute(args, { agent: agentOf(sessionId), name, callId: 'c1' })- const preStep = (sessionId, messages = [], step = 1) => ctx.listeners.get('agent/pre-step').at(0)(+ const preStep = (sessionId, messages = [], step = 1) => ctx.listeners.get('agent/pre-step').at(-1)({ agent: agentOf(sessionId), messages, step, signal: AbortSignal.timeout(60_000) }, async () => 'next')const state = () => JSON.parse(readFileSync(join(home, 'state.json'), 'utf8'))const stop = async ({ keep = false } = {}) => {diff --git a/ds-bot/test/work-dir.test.js b/ds-bot/test/work-dir.test.jsindex d340d26f..3a6e71ff 100644--- a/ds-bot/test/work-dir.test.js+++ b/ds-bot/test/work-dir.test.js@@ -114,8 +114,7 @@ async function start({ services = {}, workspaces = [], llm, team: saved = TEAM,}))).json()const tool = (name, args, sessionId) => rt.teamTools.find(d => d.name === name).execute(args, { agent: agentOf(sessionId), name, callId: 'c1' })// Runs the parts check, registered last; 'next' means the step goes on.- // The part-transition hook registers with prepend, so it is first in the list.- const preStep = (sessionId, messages = [], step = 1) => ctx.listeners.get('agent/pre-step').at(0)(+ const preStep = (sessionId, messages = [], step = 1) => ctx.listeners.get('agent/pre-step').at(-1)({ agent: agentOf(sessionId), messages, step, signal: AbortSignal.timeout(60_000) }, async () => 'next')const state = () => JSON.parse(readFileSync(join(home, 'state.json'), 'utf8'))// Waits until the Bot's own chat runs in `dir` and its move has finished.@@ -554,7 +553,7 @@ function folderService() {}const assemble = (ctx, agent) => {let passed = false- const run = ctx.listeners.get('system-prompt/assemble').at(0)({ contexts: [] }, { agent, signal: AbortSignal.timeout(60_000) }, async () => { passed = true })+ const run = ctx.listeners.get('system-prompt/assemble').at(-1)({ contexts: [] }, { agent, signal: AbortSignal.timeout(60_000) }, async () => { passed = true })return run.then(() => passed)}