Skip to content

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

Wait for Bot capability mounts before turns

lulu feizhu8950eb6aaa2e
diff --git a/ds-bot/src/host/capabilities.js b/ds-bot/src/host/capabilities.js
index ba03a667..5c9c0748 100644
--- a/ds-bot/src/host/capabilities.js
+++ b/ds-bot/src/host/capabilities.js
@@ -3,6 +3,7 @@ import { createHash } from 'node:crypto'
const MCP_CLIENT = '@deepseek-ai/dsh-mcp-client'
const NAME = /^[a-z][a-z0-9]*(?:-[a-z0-9]+)*$/
const SECRET_REFERENCE = /\$DSH_SECRET_([A-Z][A-Z0-9_]*)/g
+const ENSURE_TIMEOUT_MS = 15_000
export function validateCapabilityName(value) {
const name = String(value ?? '').trim()
@@ -348,6 +349,26 @@ export function install(rt) {
}
return scheduled
}
+ async function ensureCapabilities(sessionId) {
+ await rt.load()
+ if (!rt.isBotSession(sessionId)) return
+ const bot = rt.botOf(rt.selfOf(sessionId))
+ if (!bot || (bot.capabilities ?? []).length === 0) return
+ try {
+ trackAgent(await rt.resolveAgent(sessionId))
+ } catch (error) {
+ rt.warn('ds-bot: capability Agent for %s could not be resolved: %s', sessionId, rt.redactSecrets(String(error)))
+ return
+ }
+ const timeoutMs = rt.capabilityEnsureTimeoutMs ?? ENSURE_TIMEOUT_MS
+ let timer
+ const completed = await Promise.race([
+ scheduleReconcile().then(() => true),
+ new Promise(resolve => { timer = setTimeout(() => resolve(false), timeoutMs) }),
+ ])
+ clearTimeout(timer)
+ if (!completed) rt.warn('ds-bot: capability mount for %s did not finish within %dms; continuing', sessionId, timeoutMs)
+ }
const trackAgent = agent => {
const sessionId = agent?.session?.id
if (typeof sessionId !== 'string' || !agent.ctx) return
@@ -420,6 +441,10 @@ export function install(rt) {
if (!loadingSecrets) void scheduleReconcile()
}
rt.capabilitiesStateSaved = () => { void scheduleReconcile() }
+ ctx.on('agent/pre-step', async function ensureCapabilitiesBeforeStep({ agent }, next) {
+ await ensureCapabilities(agent.session.id)
+ return next()
+ })
ctx.on('agent/created', ({ agent }) => {
trackAgent(agent)
void rt.load().then(scheduleReconcile).catch(error => rt.warn('ds-bot: capability load failed: %s', error))
@@ -441,6 +466,7 @@ export function install(rt) {
capabilityStatusesView,
reconcileCapabilities: reconcile,
scheduleCapabilitiesReconcile: scheduleReconcile,
+ ensureCapabilities,
expandCapabilities: names => expandCapabilityNames(names, library()),
changeCapabilityAttachment: changeAttachment,
})
diff --git a/ds-bot/test/capabilities.test.js b/ds-bot/test/capabilities.test.js
index 957f36ab..b3187784 100644
--- a/ds-bot/test/capabilities.test.js
+++ b/ds-bot/test/capabilities.test.js
@@ -29,7 +29,7 @@ const team = () => ({
})
const mcp = (name, command = 'node') => ({ name, kind: 'mcp', description: 'Echo service', transport: 'stdio', command, args: [], env: {} })
-function testAgent(id, mounts, skillsBySession) {
+function testAgent(id, mounts, skillsBySession, { awaitMount = async () => {} } = {}) {
const disposers = []
const skills = new Map()
skillsBySession.set(id, skills)
@@ -49,13 +49,13 @@ function testAgent(id, mounts, skillsBySession) {
plugin.apply(c)
}
return {
- await: async () => {},
+ await: () => awaitMount(record),
dispose: () => { record.disposed = true; record.cleanup?.() },
}
},
dispose() { for (const dispose of disposers.reverse()) dispose() },
}
- return { session: { id }, ctx: agentCtx }
+ return { id, session: { id }, ctx: agentCtx }
}
async function setup({ state = team(), secret, beforeLoad } = {}) {
@@ -206,6 +206,83 @@ test('startup and empty-to-nonempty attachments track existing Bot Agents', asyn
assert.deepEqual(env.mounts.filter(mount => !mount.disposed).map(mount => mount.sessionId), ['chief', 'room-chief', 'worker', 'helper'])
})
+test('a Bot turn waits for a slow capability mount before its first model step', async t => {
+ const state = team()
+ state.capabilities = { items: { echo: { ...mcp('echo'), approved: true, createdBy: 'chief', createdAt: 1, updatedAt: 1 } } }
+ state.bots.chief.capabilities = ['echo']
+ let startMount
+ let releaseMount
+ const mountStarted = new Promise(resolve => { startMount = resolve })
+ const mountGate = new Promise(resolve => { releaseMount = resolve })
+ let startRequest = false
+ const env = await setup({
+ state,
+ beforeLoad: ({ rt, mounts, skills }) => {
+ const agent = testAgent('chief', mounts, skills, { awaitMount: async () => { startMount(); await mountGate } })
+ rt.chatOf = () => 'chief'
+ rt.groupSessionsOf = () => []
+ rt.resolveAgent = async () => agent
+ },
+ })
+ const agent = await env.rt.resolveAgent('chief')
+ t.after(async () => {
+ releaseMount()
+ agent.ctx.dispose()
+ await env.rt.scheduleCapabilitiesReconcile()
+ await new Promise(resolve => setImmediate(resolve))
+ env.dispose()
+ })
+ const beforeStep = env.ctx.listeners.get('agent/pre-step').find(listener => listener.name === 'ensureCapabilitiesBeforeStep')
+ const turn = beforeStep({ agent, messages: [], step: 1 }, () => { startRequest = true })
+ await mountStarted
+ await new Promise(resolve => setImmediate(resolve))
+ assert.equal(startRequest, false)
+ releaseMount()
+ await turn
+ assert.equal(startRequest, true)
+ assert.equal(env.rt.capabilityStatusesView().chief.echo.state, 'ready')
+})
+
+test('a hung capability mount releases the Bot turn after its readiness timeout', async t => {
+ const state = team()
+ state.capabilities = { items: { echo: { ...mcp('echo'), approved: true, createdBy: 'chief', createdAt: 1, updatedAt: 1 } } }
+ state.bots.chief.capabilities = ['echo']
+ const warnings = []
+ let startMount
+ let releaseMount
+ const mountStarted = new Promise(resolve => { startMount = resolve })
+ const mountGate = new Promise(resolve => { releaseMount = resolve })
+ let startRequest = false
+ const env = await setup({
+ state,
+ beforeLoad: ({ ctx, rt, mounts, skills }) => {
+ ctx.logger.warn = (...args) => warnings.push(args.join(' '))
+ const agent = testAgent('chief', mounts, skills, { awaitMount: async () => { startMount(); await mountGate } })
+ rt.chatOf = () => 'chief'
+ rt.groupSessionsOf = () => []
+ rt.resolveAgent = async () => agent
+ rt.capabilityEnsureTimeoutMs = 10
+ },
+ })
+ const agent = await env.rt.resolveAgent('chief')
+ t.after(async () => {
+ releaseMount()
+ agent.ctx.dispose()
+ await env.rt.scheduleCapabilitiesReconcile()
+ await new Promise(resolve => setImmediate(resolve))
+ env.dispose()
+ })
+ const beforeStep = env.ctx.listeners.get('agent/pre-step').find(listener => listener.name === 'ensureCapabilitiesBeforeStep')
+ const turn = beforeStep({ agent, messages: [], step: 1 }, () => { startRequest = true })
+ await mountStarted
+ await turn
+ assert.equal(startRequest, true)
+ assert.equal(env.rt.capabilityStatusesView().chief.echo.state, 'connecting')
+ assert.match(warnings.join('\n'), /capability mount for chief did not finish within 10ms; continuing/)
+ releaseMount()
+ await env.rt.scheduleCapabilitiesReconcile()
+})
+
test('unset capability statuses are omitted from the view and tool listing', async t => {
const env = await setup()
t.after(() => env.dispose())
diff --git a/ds-bot/test/install.test.js b/ds-bot/test/install.test.js
index 247f0ede..800aba55 100644
--- a/ds-bot/test/install.test.js
+++ b/ds-bot/test/install.test.js
@@ -86,6 +86,7 @@ test('apply registers listeners, tools, and effects in a fixed order', (t) => {
'on llm/stream',
'effect ds-bot: load secrets',
'effect ds-bot: connectors',
+ 'on agent/pre-step',
'on agent/created',
'effect ds-bot: capabilities',
'effect ds-bot: usage ledger',