Skip to content

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

Re-scope live Agents' team tools after a plugin reload

Fei2411439be1bb5be0
diff --git a/ds-bot/src/host/steer.js b/ds-bot/src/host/steer.js
index 31668e98..e17cdb34 100644
--- a/ds-bot/src/host/steer.js
+++ b/ds-bot/src/host/steer.js
@@ -133,7 +133,7 @@ export function install(rt) {
return
}
shadowed.add(agent)
- agent.ctx.tools.register({
+ const lift = agent.ctx.tools.register({
...def,
execute: async (args, exec) => {
const native = def.execute(args, exec)
@@ -194,17 +194,23 @@ export function install(rt) {
return { kind: 'promoted', cwd, jobId, timeoutMs: Date.now() - startedAt, output: renderJobRead(read) }
},
})
+ // tools.js keeps this with the Session's scope; unmarking lets a later
+ // scope register the shadow again.
+ return () => { shadowed.delete(agent); lift?.() }
}
rt.shadowShells = (agent) => {
if (agent === undefined || !rt.isBotSession(agent.session?.id)) return
if (shadowed.has(agent) || typeof agent.ctx?.tools?.register !== 'function') return
+ const disposers = []
for (const name of SHELL_TOOLS) {
try {
- shadowShell(agent, name)
+ const lift = shadowShell(agent, name)
+ if (typeof lift === 'function') disposers.push(lift)
} catch (error) {
rt.warn('ds-bot: shadowing %s failed: %s', name, error)
}
}
+ return disposers
}
ctx.on('session/event', (session, event) => {
diff --git a/ds-bot/src/host/store.js b/ds-bot/src/host/store.js
index d13b73d6..18f08153 100644
--- a/ds-bot/src/host/store.js
+++ b/ds-bot/src/host/store.js
@@ -374,8 +374,9 @@ export function install(rt) {
}
for (const [botId, sessionId] of Object.entries(room.sessions ?? {})) groupOwners.set(sessionId, { botId, roomId: room.id })
}
- // The agents module owns the DSH Agent session index, and tools.js drops the
- // scoped tools of Sessions that left the team; both install after this.
+ // The agents module owns the DSH Agent session index, and tools.js re-syncs
+ // the scoped tools with this ownership (drops Sessions that left, scopes
+ // live Agents that joined); both install after this.
rt.indexAgents?.()
rt.syncScopedTools?.()
}
diff --git a/ds-bot/src/host/tools.js b/ds-bot/src/host/tools.js
index 6bf40426..f2c2085a 100644
--- a/ds-bot/src/host/tools.js
+++ b/ds-bot/src/host/tools.js
@@ -711,7 +711,10 @@ export function install(rt) {
// disposers and goes with the ownership: indexSessions (store.js) runs
// syncScopedTools on every save and adopt, so a deleted Bot's adopted
// Session, a member who left, or a deleted group shows the plain dsh surface
- // again — and the unload effect lifts whatever is left.
+ // again — and the unload effect lifts whatever is left. The same sync scopes
+ // 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, disposers: Array<() => void>}>} */
const scoped = new Map()
const unscope = (id) => {
@@ -731,6 +734,11 @@ export function install(rt) {
for (const id of scoped.keys()) {
if (!rt.isBotSession(id) && rt.roomOf(id) === undefined) unscope(id)
}
+ // A reload lifts the scope of Agents that are still live, and an adopted
+ // file can newly own one that already runs: scope each live Agent the team
+ // owns before its next request. hideBotUnusedTools does nothing for a
+ // Session that stays unowned, and nothing twice for one already scoped.
+ for (const agent of ctx.get?.('agents')?.list?.() ?? []) hideBotUnusedTools(agent)
}
ctx.effect(() => () => { for (const id of scoped.keys()) unscope(id) }, 'ds-bot: scoped team tools')
@@ -777,8 +785,16 @@ export function install(rt) {
// goal tools. The restrict is kept with the Session's scope, so it lifts with it.
const hideBotUnusedTools = (agent) => {
shareBotTools(agent)
- rt.shadowShells?.(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, disposers: [] }
+ held.agent = agent
+ held.disposers.push(...shadows)
+ scoped.set(id, held)
+ }
if (agent === undefined || !rt.isBotSession(id) || scoped.get(id)?.restricted === true) return
if (typeof agent.ctx?.tools?.restrict !== 'function') return
try {
diff --git a/ds-bot/test/scoped-tools-creation.test.js b/ds-bot/test/scoped-tools-creation.test.js
index d7b0fc15..65b1468c 100644
--- a/ds-bot/test/scoped-tools-creation.test.js
+++ b/ds-bot/test/scoped-tools-creation.test.js
@@ -32,9 +32,16 @@ const TEAM = {
}
let rt
-const start = (team = TEAM, services = {}, globals = []) => {
- const home = mkdtempSync(join(tmpdir(), 'ds-bot-scoped-'))
- writeFileSync(join(home, 'state.json'), JSON.stringify(team))
+const start = (team = TEAM, services = {}, globals = [], shared = {}) => {
+ const home = shared.home ?? mkdtempSync(join(tmpdir(), 'ds-bot-scoped-'))
+ if (shared.home === undefined) writeFileSync(join(home, 'state.json'), JSON.stringify(team))
+ // `scoped` and `restricted` model each Session's own tool layer, and
+ // `liveAgents` the DSH registry: shared across two runtimes they keep a
+ // Session's Agent alive through a disable and re-enable.
+ const scoped = shared.scoped ?? []
+ const restricted = shared.restricted ?? []
+ const liveAgents = shared.liveAgents ?? new Map()
+ services.agents ??= { list: () => [...liveAgents.values()], get: id => liveAgents.get(id) }
const ctx = fakeContext({ services })
// Another plugin's global show_view, in place before ds-bot loads.
ctx.tools.register({ name: 'show_view', description: 'global', execute: async () => 'global' })
@@ -46,13 +53,13 @@ const start = (team = TEAM, services = {}, globals = []) => {
if (ctx.registeredTools.has(tool.name)) throw new Error(`tool "${tool.name}" is already registered`)
return plainRegister(tool)
}
- const scoped = []
- const restricted = []
let sessions = 0
const agentOf = (sessionId) => {
+ const held = liveAgents.get(sessionId)
+ if (held !== undefined) return held
const nextStep = []
const nextTurn = []
- return {
+ const agent = {
id: sessionId,
session: { id: sessionId, header: { cwd: '/team' }, requestHeader: () => undefined, deriveMessages: () => [] },
ctx: {
@@ -96,6 +103,8 @@ const start = (team = TEAM, services = {}, globals = []) => {
},
},
}
+ liveAgents.set(sessionId, agent)
+ return agent
}
// ask_user_question is served, so the deny list is not empty in a Bot scope.
ctx.tools.schemas = () => [{ name: 'ask_user_question' }, { name: 'show_view' }]
@@ -134,11 +143,12 @@ const start = (team = TEAM, services = {}, globals = []) => {
}
const tool = (name, args, sessionId, exec = {}) =>
rt.teamTools.find(d => d.name === name).execute(args, { agent: agentOf(sessionId), name, callId: 'c1', ...exec })
+ const unload = () => ctx.dispose()
const stop = async () => {
- await ctx.dispose()
+ await unload()
rmSync(home, { recursive: true, force: true, maxRetries: 5 })
}
- return { ctx, call, tool, emit, agentCreated, agentOf, scoped, restricted, stop }
+ return { ctx, call, tool, emit, agentCreated, agentOf, scoped, restricted, home, unload, stop }
}
const until = async (check, ms = 3000) => {
@@ -315,3 +325,25 @@ test('a Session that becomes a Bot\'s again gets the scope back', async (t) => {
await host.agentCreated('b')
assert.ok(hasShowView(host, 'b'))
})
+
+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
+ // them before their next request.
+ const shared = { scoped: [], restricted: [], liveAgents: new Map() }
+ const first = start(TEAM, {}, [], shared)
+ shared.home = first.home
+ t.after(first.unload)
+ await first.call('state', {})
+ await first.agentCreated('a')
+ assert.ok(hasShowView(first, 'a'))
+ assert.ok(denied(first, 'a'))
+ await first.unload()
+ assert.deepEqual(scopedNames(first, 'a'), [])
+ assert.equal(denied(first, 'a'), false)
+ const second = start(TEAM, {}, [], shared)
+ t.after(second.stop)
+ await second.call('state', {})
+ assert.ok(hasShowView(second, 'a'), 'the scoped team tools are back')
+ assert.ok(denied(second, 'a'), 'the blocking question tool is denied again')
+})