Skip to content

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

fix: keep recovered team tools pending until their first collected list

Fei2411554e0eab726b
diff --git a/ds-bot/src/host/tools.js b/ds-bot/src/host/tools.js
index 60168324..ab166cb9 100644
--- a/ds-bot/src/host/tools.js
+++ b/ds-bot/src/host/tools.js
@@ -753,7 +753,7 @@ export function install(rt) {
// the live Agents back: a reload lifts their scope while they are still
// live, and agent/created does not fire for them again, so without the walk
// their next request would come without the team tools.
- /** @type {Map<string, {agent: any, relay: boolean, tools: boolean, restricted: boolean, filtered: boolean, disposers: Array<() => void>}>} */
+ /** @type {Map<string, {agent: any, relay: boolean, tools: boolean, restricted: boolean, filtered: boolean, disposers: Array<() => void>, unlisted: Set<string>}>} */
const scoped = new Map()
const unscope = (id) => {
const held = scoped.get(id)
@@ -783,11 +783,8 @@ export function install(rt) {
// A wanted Session whose Agent cannot take scoped tools yet is warned about
// once; held.tools stays unset, so the next step's check tries again.
const registerless = new WeakSet()
- // Returns the names this call registered, so a caller that missed the request's
- // tool list can add them to it.
const shareBotTools = (agent) => {
- const added = []
- if (agent === undefined) return added
+ if (agent === undefined) return
const id = agent.session?.id
const owns = rt.isBotSession(id)
const relayOnly = !owns && rt.roomOf(id) !== undefined
@@ -799,36 +796,39 @@ export function install(rt) {
unscope(id)
held = undefined
}
- if (!wanted || held?.tools === true) return added
+ if (!wanted || held?.tools === true) return
if (typeof agent.ctx?.tools?.register !== 'function') {
if (!registerless.has(agent)) {
registerless.add(agent)
rt.warn('ds-bot: %s needs the team tools but its Agent cannot register them yet', id)
}
- return added
+ return
}
const disposers = []
const failed = []
+ const registered = []
for (const def of rt.teamTools) {
if (relayOnly && def.name !== 'group_relay') continue
try {
const dispose = agent.ctx.tools.register(def)
if (typeof dispose === 'function') disposers.push(dispose)
- added.push(def.name)
+ registered.push(def.name)
} catch (error) {
// A tool another plugin already scoped onto this agent stays missing
// rather than stopping the team; the rest still land.
failed.push(`${def.name}: ${error.message ?? error}`)
}
}
- held ??= { relay: relayOnly, tools: false, restricted: false, filtered: false, disposers: [] }
+ held ??= { relay: relayOnly, tools: false, restricted: false, filtered: false, disposers: [], unlisted: new Set() }
held.agent = agent
held.relay = relayOnly
held.tools = true
held.disposers.push(...disposers)
scoped.set(id, held)
+ // A scope made outside creation never reached a collected tool list; the
+ // assemble adds the pending names to the step's list once, then clears them.
+ for (const name of registered) held.unlisted.add(name)
if (failed.length > 0) rt.warn('ds-bot: team tools not served to %s: %s', id, failed.join('; '))
- return added
}
// The standard preset registers `subagent` (model selection) in each Agent's own
@@ -884,28 +884,25 @@ export function install(rt) {
// through ask_user instead, so it is taken out of their tool list, as are the goal
// and delegation tools. The restricts are kept with the Session's scope, so they
// lift with it.
- // Returns the team tool names shareBotTools registered on this call — empty
- // when the scope was already in place or the Session is not the team's.
const hideBotUnusedTools = (agent) => {
- const added = shareBotTools(agent)
+ shareBotTools(agent)
const id = agent?.session?.id
// The shell shadows go with the scope, so a reload or a Session that leaves
// the team lifts them like the rest, and the Agent can be scoped again.
const shadows = rt.shadowShells?.(agent) ?? []
if (shadows.length > 0) {
- const held = scoped.get(id) ?? { relay: false, tools: false, restricted: false, filtered: false, disposers: [] }
+ const held = scoped.get(id) ?? { relay: false, tools: false, restricted: false, filtered: false, disposers: [], unlisted: new Set() }
held.agent = agent
held.disposers.push(...shadows)
scoped.set(id, held)
}
- if (agent === undefined || !rt.isBotSession(id)) return added
- const held = scoped.get(id) ?? { relay: false, tools: false, restricted: false, filtered: false, disposers: [] }
+ if (agent === undefined || !rt.isBotSession(id)) return
+ const held = scoped.get(id) ?? { relay: false, tools: false, restricted: false, filtered: false, disposers: [], unlisted: new Set() }
held.agent = agent
scoped.set(id, held)
filterBotAssembly(agent, held)
- if (held.restricted === true || typeof agent.ctx?.tools?.restrict !== 'function') return added
+ if (held.restricted === true || typeof agent.ctx?.tools?.restrict !== 'function') return
maskHiddenTools(agent, held, new Set(ctx.tools.schemas(agent).map(schema => schema.name)))
- return added
}
// A new Bot, part, or group Session gets its Agent before ds-bot records it, so the
// recording path resolves it again once the session is known as a Bot's; meanwhile
@@ -921,36 +918,55 @@ export function install(rt) {
try {
await rt.load()
hideBotUnusedTools(agent)
+ // Creation runs before the first step collects the tool list, so the
+ // names just registered will already be in it; nothing stays pending.
+ scoped.get(agent?.session?.id)?.unlisted?.clear()
} catch (error) {
rt.warn('ds-bot: hiding %s on agent/created failed: %s', HIDDEN_TOOLS.join(', '), error)
}
})
// A live Agent the creation path missed — after a restart, agent/created may
- // run before this plugin's listeners are back — still gets its scope here,
- // before the step's request. The request's tool list is collected before this
- // waterfall, so names registered now are appended to it by hand (#826).
+ // run before this plugin's listeners are back — is scoped by the load or
+ // here, before the step's request. The request's tool list was collected
+ // before this waterfall, so names scoped since are added now: a same-named
+ // global entry is replaced by the scoped schema, and a PTC list that reaches
+ // team tools through run_code is left alone.
ctx.on('system-prompt/assemble', async function scopeTeamTools(_assembly, context, next) {
const agent = context.agent
if (agent === undefined) return next()
- let added = []
try {
await rt.load()
- added = hideBotUnusedTools(agent)
+ hideBotUnusedTools(agent)
} catch (error) {
rt.warn('ds-bot: scoping the tools of %s failed: %s', agent.session?.id ?? agent.id, error)
}
const assembled = await next()
- if (added.length === 0 || typeof ctx.tools?.schemas !== 'function') return assembled
+ const held = scoped.get(agent.session?.id)
+ if (held?.unlisted === undefined || held.unlisted.size === 0) return assembled
+ const pending = [...held.unlisted]
+ // Cleared whatever happens below: the names are offered once, not on
+ // every step.
+ held.unlisted.clear()
+ if ((assembled.tools ?? []).some(tool => tool.name === 'run_code')) return assembled
+ if (typeof ctx.tools?.schemas !== 'function') return assembled
try {
+ const wanted = new Set(pending)
+ const current = new Map(ctx.tools.schemas(agent).filter(schema => wanted.has(schema.name)).map(schema => [schema.name, schema]))
+ let changed = false
+ const tools = (assembled.tools ?? []).map((tool) => {
+ const schema = current.get(tool.name)
+ if (schema === undefined) return tool
+ changed = true
+ return schema
+ })
const present = new Set((assembled.tools ?? []).map(tool => tool.name))
- const fresh = []
- for (const schema of ctx.tools.schemas(agent)) {
- if (!added.includes(schema.name) || present.has(schema.name)) continue
- present.add(schema.name)
- fresh.push(schema)
+ for (const name of pending) {
+ if (present.has(name) || !current.has(name)) continue
+ changed = true
+ tools.push(current.get(name))
}
- return fresh.length === 0 ? assembled : { ...assembled, tools: [...(assembled.tools ?? []), ...fresh] }
+ return changed ? { ...assembled, tools } : assembled
} catch (error) {
rt.warn('ds-bot: listing the scoped tools of %s failed: %s', agent.session?.id ?? agent.id, error)
return assembled
diff --git a/ds-bot/test/scoped-tools-creation.test.js b/ds-bot/test/scoped-tools-creation.test.js
index cbdf932a..9711bf27 100644
--- a/ds-bot/test/scoped-tools-creation.test.js
+++ b/ds-bot/test/scoped-tools-creation.test.js
@@ -139,6 +139,9 @@ const start = (team = TEAM, services = {}, globals = [], shared = {}) => {
sessionTitle: { rename: async () => {} },
sessions: { flush: async () => {} },
})
+ // Sessions named in shared.live get their Agent into the registry before the
+ // runtime and its first load — the state a restart leaves behind (#826).
+ for (const liveId of shared.live ?? []) agentOf(liveId)
let route
const register = ctx.connection.fetch.register
ctx.connection.fetch.register = (options) => { route = options; return register(options) }
@@ -396,6 +399,50 @@ test('the assemble passes an unowned Session through unchanged', async (t) => {
assert.deepEqual(scopedNames(host, 'plain1'), [])
})
+test('a Session the load itself scoped still gets its tools into the step\'s list', async (t) => {
+ // The restart case of #826: the Agent was already live when the runtime's
+ // first load scoped it — the names were never on a collected tool list, so
+ // the assemble must still add them to this step's.
+ const host = start(TEAM, {}, [], { live: ['r1'] })
+ t.after(host.stop)
+ const assembled = await assemble(host, host.agentOf('r1'), { tools: [{ name: 'read' }], sections: [] })
+ assert.deepEqual(scopedNames(host, 'r1'), ['group_relay'])
+ assert.deepEqual(assembled.tools.map(tool => tool.name), ['read', 'group_relay'])
+})
+
+test('a PTC group Session gets its relay scoped but keeps run_code alone in the list', async (t) => {
+ // A group made under the PTC preset relays through run_code; adding the scoped
+ // group_relay to the list would make the hook pick the call it cannot declare.
+ const host = start()
+ t.after(host.stop)
+ const agent = host.agentOf('r1')
+ const assembly = { tools: [{ name: 'run_code' }], sections: [] }
+ assert.equal(await assemble(host, agent, assembly), assembly)
+ assert.deepEqual(scopedNames(host, 'r1'), ['group_relay'])
+})
+
+test('a same-named global in the list is replaced by the scoped schema', async (t) => {
+ // Another plugin's global show_view was collected for the request; the scoped
+ // one that shadows it at execution replaces its schema in the list.
+ const host = start()
+ t.after(host.stop)
+ const agent = host.agentOf('a')
+ const assembled = await assemble(host, agent, { tools: [{ name: 'show_view', description: 'global' }], sections: [] })
+ const named = assembled.tools.filter(tool => tool.name === 'show_view')
+ assert.equal(named.length, 1)
+ assert.notEqual(named[0].description, 'global')
+})
+
+test('the assemble adds the recovered names once, not on every step', async (t) => {
+ const host = start()
+ t.after(host.stop)
+ const agent = host.agentOf('r1')
+ const first = await assemble(host, agent, { tools: [{ name: 'read' }], sections: [] })
+ assert.deepEqual(first.tools.map(tool => tool.name), ['read', 'group_relay'])
+ const second = { tools: [{ name: 'read' }], sections: [] }
+ assert.equal(await assemble(host, agent, second), second)
+})
+
test('a live Bot Session gets its team tools back after a disable and re-enable', async (t) => {
// A hot reload lifts the scope of Agents that are still live, and
// agent/created does not fire again: the sync at the next load must scope