A team of long-lived bots for DeepSeek Harness, with main bots, bot-to-bot messages and group chats.
Drop the scoped team tools when a Session leaves the team
Fei24111897455aa8c1
diff --git a/ds-bot/src/host/store.js b/ds-bot/src/host/store.jsindex a20a0b3e..d13b73d6 100644--- a/ds-bot/src/host/store.js+++ b/ds-bot/src/host/store.js@@ -374,8 +374,10 @@ 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; it installs after this.+ // 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.rt.indexAgents?.()+ rt.syncScopedTools?.()}// The Bot a Session speaks for: its own chat, one of its group Sessions, or an// earlier segment of either.diff --git a/ds-bot/src/host/tools.js b/ds-bot/src/host/tools.jsindex de727737..6bf40426 100644--- a/ds-bot/src/host/tools.js+++ b/ds-bot/src/host/tools.js@@ -707,48 +707,93 @@ export function install(rt) {// Session gets only its relay, and a Session ds-bot does not own gets none —// so they never appear in a plain dsh conversation, and another plugin's// same-named global tool can neither collide with them at boot nor shadow- // them inside these Sessions. Once per agent; the agent ctx carries the session.- const scopedTools = new WeakSet()+ // them inside these Sessions. What is scoped onto a Session is kept by its+ // 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.+ /** @type {Map<string, {agent: any, relay: boolean, tools: boolean, restricted: boolean, disposers: Array<() => void>}>} */+ const scoped = new Map()+ const unscope = (id) => {+ const held = scoped.get(id)+ if (held === undefined) return+ scoped.delete(id)+ for (const dispose of held.disposers) {+ try {+ dispose()+ } catch (error) {+ rt.warn('ds-bot: a scoped tool of %s was not removed: %s', id, error)+ }+ }+ }+ const syncScopedTools = () => {+ // unscope() deletes as it goes, which a Map keys iterator takes in stride.+ for (const id of scoped.keys()) {+ if (!rt.isBotSession(id) && rt.roomOf(id) === undefined) unscope(id)+ }+ }+ ctx.effect(() => () => { for (const id of scoped.keys()) unscope(id) }, 'ds-bot: scoped team tools')+const shareBotTools = (agent) => {- if (agent === undefined || scopedTools.has(agent)) return+ if (agent === undefined) returnconst id = agent.session?.id- const relayOnly = !rt.isBotSession(id) && rt.roomOf(id) !== undefined- if (!rt.isBotSession(id) && !relayOnly) return+ const owns = rt.isBotSession(id)+ const relayOnly = !owns && rt.roomOf(id) !== undefined+ const wanted = owns || relayOnly+ let held = scoped.get(id)+ // A Session that left the team, a scope of the wrong kind, or a fresh Agent+ // for one Session all start over; a wanted scope is registered again below.+ if (held !== undefined && (!wanted || held.agent !== agent || held.relay !== relayOnly)) {+ unscope(id)+ held = undefined+ }+ if (!wanted || held?.tools === true) returnif (typeof agent.ctx?.tools?.register !== 'function') return+ const disposers = []const failed = []for (const def of rt.teamTools) {if (relayOnly && def.name !== 'group_relay') continuetry {- agent.ctx.tools.register(def)+ const dispose = agent.ctx.tools.register(def)+ if (typeof dispose === 'function') disposers.push(dispose)} 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}`)}}- scopedTools.add(agent)- if (failed.length > 0) rt.warn('ds-bot: team tools not served to %s: %s', agent.session?.id, failed.join('; '))+ held ??= { relay: relayOnly, tools: false, restricted: false, disposers: [] }+ held.agent = agent+ held.relay = relayOnly+ held.tools = true+ held.disposers.push(...disposers)+ scoped.set(id, held)+ if (failed.length > 0) rt.warn('ds-bot: team tools not served to %s: %s', id, failed.join('; '))}// dsh's own question tool holds the turn open until the user answers, and every// message from another Bot or a group round queues behind it. Bot Sessions ask// through ask_user instead, so the tool is taken out of their tool list, as are the- // goal tools.- const hiddenTools = new WeakSet()+ // goal tools. The restrict is kept with the Session's scope, so it lifts with it.const hideBotUnusedTools = (agent) => {shareBotTools(agent)rt.shadowShells?.(agent)- if (agent === undefined || hiddenTools.has(agent) || !rt.isBotSession(agent.session?.id)) return+ const id = agent?.session?.id+ if (agent === undefined || !rt.isBotSession(id) || scoped.get(id)?.restricted === true) returnif (typeof agent.ctx?.tools?.restrict !== 'function') returntry {// restrict() throws on a name this profile does not serve.const served = new Set(ctx.tools.schemas(agent).map(schema => schema.name))const deny = HIDDEN_TOOLS.filter(name => served.has(name))if (deny.length === 0) return- agent.ctx.tools.restrict({ deny })- hiddenTools.add(agent)+ const lift = agent.ctx.tools.restrict({ deny })+ const held = scoped.get(id) ?? { relay: false, tools: false, restricted: false, disposers: [] }+ held.agent = agent+ if (typeof lift === 'function') held.disposers.push(lift)+ held.restricted = true+ scoped.set(id, held)} catch (error) {- rt.warn('ds-bot: %s stay in the tool list of %s: %s', HIDDEN_TOOLS.join(', '), agent.session?.id, error)+ rt.warn('ds-bot: %s stay in the tool list of %s: %s', HIDDEN_TOOLS.join(', '), id, error)}}// A new Bot, part, or group Session gets its Agent before ds-bot records it, so the@@ -896,5 +941,5 @@ export function install(rt) {}))}- Object.assign(rt, { pickRoom, hideBotUnusedTools })+ Object.assign(rt, { pickRoom, hideBotUnusedTools, syncScopedTools })}diff --git a/ds-bot/test/agent-preset.test.js b/ds-bot/test/agent-preset.test.jsindex fba2f628..b354ab7f 100644--- a/ds-bot/test/agent-preset.test.js+++ b/ds-bot/test/agent-preset.test.js@@ -22,7 +22,6 @@ const TEAM = {questions: {},}-let rtfunction start({ presets } = {}) {const home = mkdtempSync(join(tmpdir(), 'ds-bot-agent-preset-'))writeFileSync(join(home, 'state.json'), JSON.stringify(TEAM))@@ -45,7 +44,7 @@ function start({ presets } = {}) {},sessionTitle: { rename: async () => {} },})- rt = createRuntime(ctx, { home, github: false, diagnosticsFile: false })+ const rt = createRuntime(ctx, { home, github: false, diagnosticsFile: false })const relay = ctx.listeners.get('llm/stream').find(listener => /relayOrPass/.test(String(listener)))const stop = async () => {await ctx.dispose()diff --git a/ds-bot/test/blocking-question.test.js b/ds-bot/test/blocking-question.test.jsindex d32b4e4c..ab789d8e 100644--- a/ds-bot/test/blocking-question.test.js+++ b/ds-bot/test/blocking-question.test.js@@ -70,7 +70,7 @@ async function setup({ served = ['read', 'ask_user', QUESTION], goals } = {}) {sessionTitle: { rename: async () => {} },sessions: { flush: async () => {} },})- rt = createRuntime(ctx, { home, github: false, diagnosticsFile: false })+ const rt = createRuntime(ctx, { home, github: false, diagnosticsFile: false })await rt.load()const guard = (name, sessionId) => ctx.guards.map(check => check({ name, agent: sessionId === undefined ? undefined : { id: sessionId } })).find(reason => reason !== undefined)const hiddenIn = id => hidden.filter(([sessionId, deny]) => sessionId === id && deny.includes(QUESTION))@@ -99,7 +99,6 @@ function goalService() {}// What DSH logs when /goal starts a goal: a snapshot change of the Session's goal log.-let rtconst startGoal = (ctx, goals, sessionId) => {const goal = { id: `goal-${sessionId}`, revision: 1, objective: 'Ship it', phase: 'active', maxGoalRounds: 256 }goals?.current.set(sessionId, goal)diff --git a/ds-bot/test/browser-handoff.test.js b/ds-bot/test/browser-handoff.test.jsindex 46a644b0..499c9796 100644--- a/ds-bot/test/browser-handoff.test.js+++ b/ds-bot/test/browser-handoff.test.js@@ -28,7 +28,6 @@ const TEAM = {questions: {},}-let rtfunction start() {const home = mkdtempSync(join(tmpdir(), 'ds-bot-handoff-'))writeFileSync(join(home, 'state.json'), JSON.stringify(TEAM))@@ -43,7 +42,7 @@ function start() {route = optionsreturn register(options)}- rt = createRuntime(ctx, { home, browser: true })+ const rt = createRuntime(ctx, { home, browser: true })rt.ensureCapabilities = async sessionId => { ensured.push(sessionId) }rt.resolveAgent = async sessionId => ({session: { id: sessionId },diff --git a/ds-bot/test/btw.test.js b/ds-bot/test/btw.test.jsindex 27f0ac61..6a7f0b78 100644--- a/ds-bot/test/btw.test.js+++ b/ds-bot/test/btw.test.js@@ -7,8 +7,6 @@ import { createRuntime } from '../index.js'import { trimToolBoundary } from '../src/host/btw.js'import { fakeContext } from './fake-ctx.js'-let rt-const user = text => ({ role: 'user', content: [{ type: 'text', text }] })const assistant = (...blocks) => ({ role: 'assistant', content: blocks })const call = (id, name = 'read') => ({ type: 'tool-call', id, name, arguments: '{}' })@@ -144,7 +142,7 @@ async function setup({ state = team(), messages, seq = 4, events, foreignLock =let routeconst register = ctx.connection.fetch.registerctx.connection.fetch.register = (options) => { route = options; return register(options) }- rt = createRuntime(ctx, { home, github: false, diagnosticsFile: false })+ const rt = createRuntime(ctx, { home, github: false, diagnosticsFile: false })let notices = 0const saved = rt.stateSavedrt.stateSaved = () => { notices += 1; saved?.() }diff --git a/ds-bot/test/capability-stdio-secrets.test.js b/ds-bot/test/capability-stdio-secrets.test.jsindex fe13de40..e93c9048 100644--- a/ds-bot/test/capability-stdio-secrets.test.js+++ b/ds-bot/test/capability-stdio-secrets.test.js@@ -8,8 +8,6 @@ import { fileURLToPath } from 'node:url'import { createRuntime } from '../index.js'import { fakeContext } from './fake-ctx.js'-let rt-// The fixture stands in for an MCP server: it reports back the argv and env it was// started with, so the test sees exactly what the plugin passed to the child process.const FIXTURE = fileURLToPath(new URL('./fixtures/echo-argv-env.mjs', import.meta.url))@@ -71,7 +69,7 @@ async function setup({ state = team(), secrets = [] } = {}) {const mcpPlugin = { name: 'mcp-client', apply() {} }const loader = { import: async () => ({ default: mcpPlugin }), unwrapExports: exports => exports.default }const ctx = fakeContext({ services: { loader } })- rt = createRuntime(ctx, { home, github: false, diagnosticsFile: false })+ const rt = createRuntime(ctx, { home, github: false, diagnosticsFile: false })ctx.sessionTitle = { rename: async () => {} }rt.resolveAgent = async sessionId => ({ session: { id: sessionId } })await rt.load()diff --git a/ds-bot/test/install.test.js b/ds-bot/test/install.test.jsindex b5e44730..fc51058a 100644--- a/ds-bot/test/install.test.js+++ b/ds-bot/test/install.test.js@@ -47,6 +47,7 @@ test('apply registers listeners and effects in a fixed order', (t) => {'prompt bot-identity 640','on agent/pre-step (prepend)','on tools/execute',+ 'effect ds-bot: scoped team tools','tools guard','on agent/created','on session/event',diff --git a/ds-bot/test/learning.test.js b/ds-bot/test/learning.test.jsindex 99df4c37..91f1e564 100644--- a/ds-bot/test/learning.test.js+++ b/ds-bot/test/learning.test.js@@ -37,7 +37,6 @@ const createReply = JSON.stringify({})const noneReply = JSON.stringify({ action: 'none', reason: 'This work taught nothing reusable.' })-let rtfunction start({ replies = [], config = {}, history = {}, seed = TEAM, learningSeed = {} } = {}) {const home = mkdtempSync(join(tmpdir(), 'ds-bot-learning-'))writeFileSync(join(home, 'state.json'), JSON.stringify(JSON.parse(JSON.stringify(seed))))@@ -93,7 +92,7 @@ function start({ replies = [], config = {}, history = {}, seed = TEAM, learningS...config,learningTimes: { minTurnMs: 0, idleMs: 0, intervalMs: 0, tickMs: 10, ...config.learningTimes },}- rt = createRuntime(ctx, { home, ...settings })+ const rt = createRuntime(ctx, { home, ...settings })const call = async (endpoint, payload = {}) => {const response = await route.fetch(new Request('http://localhost/api/bot', {method: 'POST',diff --git a/ds-bot/test/room-races.test.js b/ds-bot/test/room-races.test.jsindex 8e7743d7..46881a37 100644--- a/ds-bot/test/room-races.test.js+++ b/ds-bot/test/room-races.test.js@@ -6,8 +6,6 @@ import { test } from 'node:test'import { createRuntime } from '../index.js'import { fakeContext } from './fake-ctx.js'-let rt-// #200, #205: a group change read the team, waited (for a Session or a folder check), and// then wrote what it had read. A Bot deleted, or another change made, in that wait was undone.const TEAM = {@@ -62,7 +60,7 @@ async function start(team = TEAM) {sessionTitle: { rename: async () => {} },sessions: { flush: async () => {} },})- rt = createRuntime(ctx, { home })+ const rt = createRuntime(ctx, { home })await rt.load()const held = () => {let releasediff --git a/ds-bot/test/schedules.test.js b/ds-bot/test/schedules.test.jsindex 73d50a07..3491b669 100644--- a/ds-bot/test/schedules.test.js+++ b/ds-bot/test/schedules.test.js@@ -7,8 +7,6 @@ import { createRuntime } from '../index.js'import { fakeContext } from './fake-ctx.js'import { reminderText } from '../src/host/schedules.js'-let rt-const TEAM = {version: 1,mainBotIds: ['m1'],@@ -62,7 +60,7 @@ function setup(services = {}) {const ctx = fakeContext({ services })const register = ctx.connection.fetch.registerctx.connection.fetch.register = (route) => { ctx.route = route; return register(route) }- rt = createRuntime(ctx, { home })+ const rt = createRuntime(ctx, { home })const call = async (endpoint, payload) => (await ctx.route.fetch(new Request('http://localhost/api/bot', { method: 'POST', body: JSON.stringify({ endpoint, payload }) }))).json()return { ctx, home, call, rt }}diff --git a/ds-bot/test/scoped-tools-creation.test.js b/ds-bot/test/scoped-tools-creation.test.jsindex 6843925b..d7b0fc15 100644--- a/ds-bot/test/scoped-tools-creation.test.js+++ b/ds-bot/test/scoped-tools-creation.test.js@@ -57,8 +57,17 @@ const start = (team = TEAM, services = {}, globals = []) => {session: { id: sessionId, header: { cwd: '/team' }, requestHeader: () => undefined, deriveMessages: () => [] },ctx: {tools: {- register: (tool) => { scoped.push({ sessionId, name: tool.name, tool }); return () => {} },- restrict: (filter) => { restricted.push({ sessionId, deny: filter.deny ?? [] }); return () => {} },+ register: (tool) => {+ const entry = { sessionId, name: tool.name, tool }+ scoped.push(entry)+ // DSH's disposer unregisters; the fake drops the record.+ return () => { const at = scoped.indexOf(entry); if (at !== -1) scoped.splice(at, 1) }+ },+ restrict: (filter) => {+ const entry = { sessionId, deny: filter.deny ?? [] }+ restricted.push(entry)+ return () => { const at = restricted.indexOf(entry); if (at !== -1) restricted.splice(at, 1) }+ },},effect: execute => (typeof execute === 'function' ? execute() : undefined),on: () => () => {},@@ -242,3 +251,67 @@ test('a Session ds-bot does not own keeps only the global show_view', async (t)assert.ok(!hasShowView(host, 'plain1'), 'no scoped show_view shadows the global one')assert.ok(!host.restricted.some(entry => entry.sessionId === 'plain1'), 'no restrict applies outside the team')})++test("a deleted Bot's Session keeps none of the scope when it is adopted as a plain one", async (t) => {+ const host = start()+ t.after(host.stop)+ await host.call('state', {})+ await host.agentCreated('b')+ assert.ok(hasShowView(host, 'b'))+ assert.ok(denied(host, 'b'))+ assert.equal((await host.call('delete-bot', { id: 'b' })).ok, true)+ assert.deepEqual(scopedNames(host, 'b'), [])+ assert.equal(host.restricted.some(entry => entry.sessionId === 'b'), false)+ // The Session lives on unowned: adopting it into the DSH Agent must not bring+ // the team tools back.+ const agent = (await host.call('add-agent', {})).value+ assert.equal((await host.call('agent-adopt', { agentId: agent.id, sessionId: 'b' })).ok, true)+ assert.deepEqual(scopedNames(host, 'b'), [])+ assert.equal(host.restricted.some(entry => entry.sessionId === 'b'), false)+})++test('a member who leaves the group loses the scoped tools', async (t) => {+ const team = {+ ...TEAM,+ bots: { ...TEAM.bots, c: { id: 'c', name: 'Scout', role: 'Scout', sessionId: 'c', createdAt: 3 } },+ rooms: { r1: { ...TEAM.rooms.r1, members: ['a', 'b', 'c'], sessions: { b: 'sb' } } },+ }+ const host = start(team)+ t.after(host.stop)+ await host.call('state', {})+ await host.agentCreated('sb')+ assert.ok(hasShowView(host, 'sb'))+ assert.ok(denied(host, 'sb'))+ assert.equal((await host.call('update-room', { id: 'r1', remove: ['b'] })).ok, true)+ assert.deepEqual(scopedNames(host, 'sb'), [])+ assert.equal(host.restricted.some(entry => entry.sessionId === 'sb'), false)+ // Resolving the archived Session again does not bring the tools back.+ await host.agentCreated('sb')+ assert.deepEqual(scopedNames(host, 'sb'), [])+})++test('a deleted group loses its relay scope', async (t) => {+ const host = start()+ t.after(host.stop)+ await host.call('state', {})+ await host.agentCreated('r1')+ assert.deepEqual(scopedNames(host, 'r1'), ['group_relay'])+ assert.equal((await host.call('delete-room', { id: 'r1' })).ok, true)+ assert.deepEqual(scopedNames(host, 'r1'), [])+})++test('a Session that becomes a Bot\'s again gets the scope back', async (t) => {+ const host = start()+ t.after(host.stop)+ await host.call('state', {})+ await host.agentCreated('b')+ assert.ok(hasShowView(host, 'b'))+ assert.equal((await host.call('delete-bot', { id: 'b' })).ok, true)+ assert.deepEqual(scopedNames(host, 'b'), [])+ // A Bot record naming the Session again — here written directly, the way an+ // adopted state could carry it — serves the tools again.+ rt.state.bots.b = { id: 'b', name: 'Writer', role: 'Write', sessionId: 'b', createdAt: 2 }+ await rt.save()+ await host.agentCreated('b')+ assert.ok(hasShowView(host, 'b'))+})diff --git a/ds-bot/test/session-identity.test.js b/ds-bot/test/session-identity.test.jsindex 9fcaa5b0..4ac088fe 100644--- a/ds-bot/test/session-identity.test.js+++ b/ds-bot/test/session-identity.test.js@@ -41,7 +41,6 @@ async function drain(stream) {// `ctx.llm.stream` runs the plugin's `llm/stream` listeners in order, as DSH does,// and then a route that turns down a request without a Session. `respond` gives the// text of each answer the route lets through.-let rtfunction start() {const home = mkdtempSync(join(tmpdir(), 'ds-bot-session-identity-'))writeFileSync(join(home, 'state.json'), JSON.stringify(TEAM))@@ -96,7 +95,7 @@ function start() {sessions: { messageProjections: [] },workspaceRegistry: { archiveSession: async () => {} },})- rt = createRuntime(ctx, { home })+ const rt = createRuntime(ctx, { home })const hook = pattern => ctx.listeners.get('llm/stream').find(listener => pattern.test(String(listener)))const stop = async () => {await ctx.dispose()diff --git a/ds-bot/test/side.test.js b/ds-bot/test/side.test.jsindex dfd21cae..52765e33 100644--- a/ds-bot/test/side.test.js+++ b/ds-bot/test/side.test.js@@ -7,8 +7,6 @@ import { createRuntime } from '../index.js'import { SIDE_MARKER } from '../src/host/side.js'import { fakeContext } from './fake-ctx.js'-let rt-const user = text => ({ role: 'user', content: [{ type: 'text', text }] })const assistant = (...blocks) => ({ role: 'assistant', content: blocks })const call = (id, name = 'read') => ({ type: 'tool-call', id, name, arguments: '{}' })@@ -176,7 +174,7 @@ async function setup({ state = team(), scripts = [], gate = false, messages, seqlet routeconst register = ctx.connection.fetch.registerctx.connection.fetch.register = (options) => { route = options; return register(options) }- rt = createRuntime(ctx, { home, github: false, diagnosticsFile: false })+ const rt = createRuntime(ctx, { home, github: false, diagnosticsFile: false })let notices = 0const saved = rt.stateSavedrt.stateSaved = () => { notices += 1; saved?.() }diff --git a/ds-bot/test/steer.test.js b/ds-bot/test/steer.test.jsindex 0a9fa839..8142a554 100644--- a/ds-bot/test/steer.test.js+++ b/ds-bot/test/steer.test.js@@ -46,7 +46,6 @@ const shellDef = (name = 'pwsh') => {const jobsService = (read) => ({ read })-let rtconst start = async ({ services = {}, def = shellDef() } = {}) => {const home = mkdtempSync(join(tmpdir(), 'ds-bot-steer-'))writeFileSync(join(home, 'state.json'), JSON.stringify(TEAM))@@ -93,7 +92,7 @@ const start = async ({ services = {}, def = shellDef() } = {}) => {})const origGet = ctx.getObject.assign(ctx, { get: name => (name === 'agents' ? agentMap : origGet(name)) })- rt = createRuntime(ctx, { home })+ const rt = createRuntime(ctx, { home })await rt.load?.()const origDeliver = rt.deliverrt.deliver = (...args) => { deliveries.push(args); return origDeliver ? origDeliver(...args) : Promise.resolve() }