A team of long-lived bots for DeepSeek Harness, with main bots, bot-to-bot messages and group chats.
Prototype private Bots: closed to other Bots, outgoing sends need approval
Fei24113917acc40500
diff --git a/ds-bot/index.js b/ds-bot/index.jsindex af47e7c7..bd4b76bb 100644--- a/ds-bot/index.js+++ b/ds-bot/index.js@@ -13,6 +13,7 @@ import * as requests from './src/host/requests.js'import * as delivery from './src/host/delivery.js'import * as roster from './src/host/roster.js'import * as folders from './src/host/folders.js'+import * as privacy from './src/host/privacy.js'import * as agents from './src/host/agents.js'import * as prompt from './src/host/prompt.js'import * as timeContext from './src/host/time-context.js'@@ -45,8 +46,9 @@ export const inject = ['llm', 'tools', 'systemPrompt', 'sessionController', 'ses// Each module's install(rt) registers its listeners, tools, and effects, and puts on `rt`// the functions other modules call; they call them through `rt` at run time, never during// install. Listeners run and tools are listed in registration order, so this order holds:-// usage meters model requests after every listener that rewrites them.-const MODULES = [diagnostics, store, models, providers, permissions, turns, handoffs, requests, delivery, roster, folders, agents, prompt, timeContext, tools, chatIndex, groups, approvals, resume, memory, parts, images, secrets, connectors, capabilities, schedules, browser, browserPlugins, usage, update, dshUpdate, thinking, feed, channel]+// usage meters model requests after every listener that rewrites them. privacy checks a+// call before the hooks of tools do, so a closed call never reaches a card they show.+const MODULES = [diagnostics, store, models, providers, permissions, turns, handoffs, requests, delivery, roster, folders, privacy, agents, prompt, timeContext, tools, chatIndex, groups, approvals, resume, memory, parts, images, secrets, connectors, capabilities, schedules, browser, browserPlugins, usage, update, dshUpdate, thinking, feed, channel]export function apply(ctx, config = {}) {createRuntime(ctx, config)diff --git a/ds-bot/src/host/folders.js b/ds-bot/src/host/folders.jsindex f3691f9f..2cee3bb3 100644--- a/ds-bot/src/host/folders.js+++ b/ds-bot/src/host/folders.js@@ -123,6 +123,8 @@ export function install(rt) {if (!(await isFolder(path))) throw new Error(`${text} is a file, not a folder`)if (await holdsData(path)) throw new Error(`${text} holds DeepSeek Harness data, so it cannot be the team workspace`)if (path === await realTeam()) return path+ const clash = await rt.privateClash(path)+ if (clash) throw new Error(`${text} is in or around the folder of ${clash.name}, a private Bot, so it cannot be the team workspace`)const workspace = await ctx.workspaceRegistry.resolveByPath(path) ?? await ctx.workspaceRegistry.create(path, 'DS Bot')state.workspaceId = workspace.idawait rt.save()diff --git a/ds-bot/src/host/privacy.js b/ds-bot/src/host/privacy.jsnew file mode 100644index 00000000..e9dd059a--- /dev/null+++ b/ds-bot/src/host/privacy.js@@ -0,0 +1,257 @@+// Private Bots. A private Bot reads the team like any Bot, but no other Bot reads it, and+// what it sends out waits for the user. DSH does not fence reads, so this closes the+// tools DS Bot owns and checks the paths that the file tools and the shell are given;+// a shell can still reach any file the account can (see docs/plans).+import { join, resolve } from 'node:path'+import { within } from './folders.js'+import { unquote } from './text.js'++const FILE_TOOLS = { read: 'file_path', write: 'file_path', edit: 'file_path', read_image: 'file_path', str_replace_editor: 'path' }+const WRITE_TOOLS = ['write', 'edit', 'str_replace_editor']+const SEARCH_TOOLS = ['glob', 'grep']+const SHELL_TOOLS = ['bash', 'pwsh', 'terminal_open', 'terminal_send']+const SESSION_TOOLS = ['session_search', 'session_event_search', 'session_trace', 'session_event_trace', 'session_event_read']++// Windows and macOS file systems ignore case.+const fold = text => (process.platform === 'linux' ? text : text.toLowerCase())+const overlap = (a, b) => within(a, b) || within(b, a)+const strings = value => (typeof value === 'string' ? [value] : Array.isArray(value) ? value.flatMap(strings) : value && typeof value === 'object' ? Object.values(value).flatMap(strings) : [])+// The same path as a shell writes it: forward slashes, and on Windows both drive forms.+const asShell = (path) => {+ const slashed = path.replace(/\\/g, '/')+ const drive = /^([a-z]):\//i.exec(slashed)+ return drive ? [slashed, `/${drive[1]}${slashed.slice(2)}`] : [slashed]+}++export function install(rt) {+ const { ctx, state } = rt++ const isPrivate = id => rt.botOf(id)?.private === true+ const privateBots = () => Object.values(state.bots).filter(bot => bot.private === true)+ const sessionsOf = bot => [...new Set([bot.id, rt.chatOf(bot.id), ...(bot.segments ?? []).map(segment => segment.sessionId), ...rt.groupSessionsOf(bot.id)])]+ const dataRoots = () => [rt.dshHome, rt.home, process.env.DS_BOT_USER_DSH_HOME?.trim()].filter(Boolean)++ // A path as it is and as the file system resolves it, folded for comparing.+ const spellings = async (path) => [...new Set([fold(resolve(path)), fold(await rt.real(path))])]++ // The Bot a call speaks for. A subagent speaks for the Bot that started it.+ const callerOf = (exec) => {+ const own = rt.selfOf(exec.agent?.id)+ if (own !== undefined) return own+ const parent = exec.agent?.session?.header?.parentSession+ return parent === undefined ? undefined : rt.selfOf(parent)+ }++ // Where a private Bot keeps what must stay closed: its folder and its memory. Its+ // Sessions and its id are in file names under DSH's and the team's data.+ const regionsOf = async bot => (await Promise.all([bot.workDir, join(rt.home, 'memory', 'bots', bot.id)].filter(Boolean).map(spellings))).flat()+ const sharedFiles = async () => (await Promise.all([rt.statePath, join(rt.home, 'memory', 'journal.jsonl')].filter(Boolean).map(spellings))).flat()++ const teamFolder = () => rt.teamFolder() ?? resolve(rt.defaultTeamFolder())++ // The private Bot that `path` belongs to, other than `self`; undefined when it is open.+ // `search` is for tools that walk a folder: a folder that holds a closed one is closed+ // too, and so are the data folders, where the walk cannot be told which files to skip.+ async function ownerOfPath(path, { self, search = false, base } = {}) {+ const bots = privateBots()+ if (bots.length === 0) return undefined+ const keys = await spellings(base === undefined ? path : resolve(base, path))+ for (const bot of bots) {+ if (bot.id === self) continue+ const needles = sessionsOf(bot).map(fold)+ const regions = await regionsOf(bot)+ for (const key of keys) {+ if (regions.some(region => within(key, region) || (search && within(region, key)))) return bot+ if (needles.some(needle => key.includes(needle))) return bot+ }+ }+ for (const file of await sharedFiles()) if (keys.some(key => key.startsWith(file))) return bots[0]+ const other = bots.find(bot => bot.id !== self)+ if (search && other !== undefined) {+ const open = [rt.workDirOf({ botId: self }), teamFolder()].filter(Boolean)+ const folders = (await Promise.all(open.map(spellings))).flat()+ for (const root of (await Promise.all(dataRoots().map(spellings))).flat()) {+ if (keys.some(key => within(root, key) || (within(key, root) && !folders.some(folder => within(key, folder))))) return other+ }+ }+ return undefined+ }++ // Text a shell runs: a closed path anywhere in it closes the command. A path that the+ // command builds, or reaches by walking up from a folder, is not seen.+ async function ownerInText(text, self) {+ const flat = fold(text.replace(/\\/g, '/'))+ for (const bot of privateBots()) {+ if (bot.id === self) continue+ const spelled = (await regionsOf(bot)).flatMap(asShell)+ if (spelled.some(region => flat.includes(region)) || sessionsOf(bot).some(id => flat.includes(fold(id)))) return bot+ }+ for (const file of await sharedFiles()) if (asShell(file).some(spelled => flat.includes(spelled))) return privateBots()[0]+ return undefined+ }++ const closed = (bot, what) => new Error(`${bot.name} is a private Bot, and other Bots cannot reach its ${what}. Ask the user if ${bot.name} should share something.`)++ // What a private Bot sends out is shown to the user first, as the user sees it.+ async function approve(exec, { en, zh }) {+ const approval = ctx.get?.('approval')+ if (approval === undefined || exec.agent?.session === undefined) throw new Error('A private Bot needs the user\'s approval for this, and there is no way to ask for it here, so it was not done.')+ const outcome = await approval.request({+ agent: exec.agent,+ toolName: exec.name,+ ...(exec.callId !== undefined ? { callId: exec.callId } : {}),+ reason: en,+ displayReason: { en, zh },+ ...(exec.signal ? { signal: exec.signal } : {}),+ })+ if (outcome === 'allowed-once') return+ throw new Error(outcome === 'rejected' ? 'The user did not allow it, so it was not done.'+ : outcome === 'cancelled' ? 'The approval was cancelled, so it was not done.'+ : 'The approval could not be answered, so it was not done.')+ }++ // The Bot a tool argument names, as the tools and the memory scopes read it.+ const botNamed = ref => (typeof ref === 'string' && ref !== '' ? rt.findBot(ref) ?? rt.findBot(unquote(ref)) : undefined)++ // Tools between Bots. Other Bots may not reach a private Bot; what a private Bot sends+ // to them is the user's to allow. A private Bot is no member of a group, which the+ // roster checks as the members are written.+ async function guardTeam(exec, self) {+ const args = exec.arguments ?? {}+ const target = (ref) => {+ const bot = botNamed(ref)+ return bot !== undefined && isPrivate(bot.id) && bot.id !== self ? bot : undefined+ }+ switch (exec.name) {+ case 'message_bot': {+ const bot = target(args.to)+ if (bot) throw new Error(`Not sent: ${bot.name} is a private Bot, and other Bots cannot message it. Ask the user to pass it on.`)+ const to = botNamed(args.to)+ if (isPrivate(self) && to !== undefined) await approve(exec, { en: `Let ${rt.botOf(self).name} send this to ${to.name}?\n\n${String(args.message ?? '')}`, zh: `允许 ${rt.botOf(self).name} 把下面的内容发给 ${to.name} 吗?\n\n${String(args.message ?? '')}` })+ return+ }+ // Every Bot reads the shared capabilities, so saving one shares what is in it.+ case 'save_capability': {+ if (!isPrivate(self)) return+ const shown = [args.name, args.description, typeof args.content === 'string' ? args.content.slice(0, 500) : ''].filter(Boolean).join('\n\n')+ await approve(exec, { en: `Let ${rt.botOf(self).name} share this with the team?\n\n${shown}`, zh: `允许 ${rt.botOf(self).name} 把下面的内容共享给团队吗?\n\n${shown}` })+ return+ }+ case 'stop_bot': {+ const bot = target(args.bot)+ if (bot) throw closed(bot, 'turns')+ return+ }+ case 'update_bot': {+ const bot = target(args.bot)+ if (bot) throw closed(bot, 'settings')+ return+ }+ case 'recall':+ case 'remember':+ case 'forget': {+ const bot = target(args.scope)+ if (bot) throw closed(bot, 'memory')+ return+ }+ default:+ }+ }++ // The file tools, the shell and the session search: where the path or text names a+ // closed place, the call fails before it runs.+ async function guardFiles(exec, self) {+ const args = exec.arguments ?? {}+ const folder = () => rt.sessionFolder(exec.agent?.session ?? exec.agent)+ const refuse = (bot) => { throw closed(bot, 'files') }+ const name = exec.name+ if (name in FILE_TOOLS) {+ const path = args[FILE_TOOLS[name]]+ if (typeof path !== 'string' || path === '') return+ const bot = await ownerOfPath(path, { self, base: folder() })+ if (bot) refuse(bot)+ // A private Bot's file changes stay in its own folder unless the user allows them.+ if (isPrivate(self) && WRITE_TOOLS.includes(name)) {+ const own = await spellings(rt.botOf(self).workDir)+ const target = await spellings(resolve(folder(), path))+ if (!target.every(key => own.some(root => within(key, root)))) await approve(exec, { en: `Let ${rt.botOf(self).name} change ${path}? It is outside its own folder.`, zh: `允许 ${rt.botOf(self).name} 修改 ${path} 吗?它在自己的文件夹之外。` })+ }+ return+ }+ if (SEARCH_TOOLS.includes(name)) {+ const where = typeof args.path === 'string' && args.path !== '' ? args.path : '.'+ const bot = await ownerOfPath(where, { self, search: true, base: folder() })+ ?? (typeof args.pattern === 'string' && /^([a-z]:|[\\/]|~)/i.test(args.pattern) ? await ownerOfPath(args.pattern.replace(/[*?[{].*$/, ''), { self, search: true, base: folder() }) : undefined)+ if (bot) refuse(bot)+ return+ }+ if (SHELL_TOOLS.includes(name)) {+ const bot = await ownerInText(strings(args).join('\n'), self)+ if (bot) refuse(bot)+ return+ }+ if (name === 'working_directory' && typeof args.cd === 'string') {+ const bot = await ownerOfPath(args.cd, { self, search: true, base: folder() })+ if (bot) refuse(bot)+ return+ }+ if (SESSION_TOOLS.includes(name)) {+ const asked = strings([args.session_id, args.session_ids, args.parent_session_ids])+ const bot = privateBots().find(found => found.id !== self && sessionsOf(found).some(id => asked.includes(id)))+ if (bot) refuse(bot)+ }+ }++ // Before the tools' own hooks, so that a closed call never reaches a card they show.+ ctx.on('tools/execute', async (exec, next) => {+ if (privateBots().length === 0) return next()+ const self = callerOf(exec)+ await guardTeam(exec, self)+ await guardFiles(exec, self)+ return next()+ })++ // A folder that a private Bot's folder would overlap, or that overlaps one.+ async function privateClash(path, { except } = {}) {+ const keys = await spellings(path)+ for (const bot of privateBots()) {+ if (bot.id === except || bot.workDir === undefined) continue+ const dirs = await spellings(bot.workDir)+ if (keys.some(key => dirs.some(dir => overlap(key, dir)))) return bot+ }+ return undefined+ }++ // Checks a change of `private` or of the folder of a Bot before it is written. A+ // private Bot's folder is no other Bot's, no group's and not the team workspace,+ // because DSH cannot be told which files of a shared folder to hide.+ async function checkChange(bot, { flag, moving, workDir }) {+ const makes = flag === undefined ? bot.private === true : flag+ const dir = moving ? workDir : bot.workDir+ if (!makes) {+ const clash = moving && dir !== undefined ? await privateClash(dir, { except: bot.id }) : undefined+ if (clash) throw new Error(`${dir} is in or around the folder of ${clash.name}, a private Bot, so ${bot.name} cannot work there`)+ return+ }+ if (rt.isMain(bot.id)) throw new Error(`${bot.name} is a Main Bot, so it cannot be private`)+ if (dir === undefined) throw new Error(`${bot.name} needs a folder of its own to be private`)+ if (Object.values(state.rooms).some(room => room.members.includes(bot.id))) throw new Error(`${bot.name} is in a group chat, where every member reads what is said. Remove it from its groups first`)+ const keys = await spellings(dir)+ const others = [+ ['the team workspace', teamFolder()],+ ...Object.values(state.bots).filter(other => other.id !== bot.id && other.workDir).map(other => [`the folder of ${other.name}`, other.workDir]),+ ...Object.values(state.rooms).filter(room => room.workDir).map(room => [`the folder of "${room.name}"`, room.workDir]),+ ]+ for (const [what, path] of others) {+ const spelled = await spellings(path)+ if (keys.some(key => spelled.some(other => overlap(key, other)))) throw new Error(`${dir} is in or around ${what}, so ${bot.name} cannot be private there. Give it a folder of its own`)+ }+ }++ const checkNoPrivate = (ids) => {+ const found = ids.map(id => rt.botOf(id)).find(bot => bot?.private === true)+ if (found) throw new Error(`${found.name} is a private Bot and cannot be in a group chat: what is said in a group is read by every member`)+ }++ Object.assign(rt, { isPrivate, privateClash, checkPrivacyChange: checkChange, checkNoPrivate })+}diff --git a/ds-bot/src/host/roster.js b/ds-bot/src/host/roster.jsindex b4b79a9d..4c418978 100644--- a/ds-bot/src/host/roster.js+++ b/ds-bot/src/host/roster.js@@ -146,6 +146,8 @@ export function install(rt) {if (effort !== undefined && effort !== '' && modelRef === undefined) throw new Error('Pick a model before choosing a thinking level')const effortId = modelRef === undefined ? undefined : await rt.resolveEffort(modelRef, effort)const dir = workDir === undefined || workDir === null ? undefined : await rt.cleanWorkDir(workDir)+ const clash = dir === undefined ? undefined : await rt.privateClash(dir)+ if (clash) throw new Error(`${dir} is in or around the folder of ${clash.name}, a private Bot, so ${cleanName} cannot work there`)const id = await createSession(cleanName, { dir })try {state.bots[id] = {@@ -222,6 +224,7 @@ export function install(rt) {async function duplicateBot(id) {const bot = rt.botOf(id)if (bot === undefined) throw new Error('Unknown Bot')+ if (bot.private === true) throw new Error(`${bot.name} is private, so it cannot be copied`)const served = (await rt.catalog()).some(entry => entry.ref === bot.model)// A folder that went away since leaves the copy in the team workspace.const workDir = bot.workDir !== undefined && await rt.isFolder(bot.workDir) ? bot.workDir : undefined@@ -245,11 +248,16 @@ export function install(rt) {if (patch.browserGrants !== undefined && actorId !== undefined) {throw new Error(`Only the user can set what ${bot.name} may do in the browser`)}+ if (patch.private !== undefined && actorId !== undefined) {+ throw new Error(`Only the user can make ${bot.name} private or open`)+ }const browserGrants = patch.browserGrants === undefined ? undefined : cleanBrowserGrants(patch.browserGrants)// Checked first, so a folder, model or level that does not pass leaves the rest// unchanged too.const moving = patch.workDir !== undefinedconst workDir = moving ? await rt.cleanWorkDir(patch.workDir ?? '') : undefined+ const flag = patch.private === undefined ? undefined : patch.private === true+ await rt.checkPrivacyChange(bot, { flag, moving, workDir })const modelRef = patch.model === undefined ? undefined: patch.model === null || patch.model === '' ? null: await rt.resolveModel(patch.model)@@ -304,6 +312,8 @@ export function install(rt) {if (browserGrants === null) delete bot.browserGrantselse bot.browserGrants = browserGrants}+ if (flag === true) bot.private = true+ else if (flag === false) delete bot.privateconst moved = moving && workDir !== bot.workDirif (moving) {if (workDir === undefined) delete bot.workDir@@ -367,6 +377,7 @@ export function install(rt) {function checkMembers(members) {if (members.length < 2) throw new Error('A group chat needs at least two Bots')if (members.length > ROOM_MAX_MEMBERS) throw new Error(`A group chat holds at most ${ROOM_MAX_MEMBERS} Bots`)+ rt.checkNoPrivate(members)}// A Main Bot leads the groups it makes; a group the user makes is led by a Main Bot@@ -472,6 +483,8 @@ export function install(rt) {planRoomChange(asked, patch, actorId)const moving = patch.workDir !== undefinedconst workDir = moving ? await rt.cleanWorkDir(patch.workDir ?? '') : undefined+ const clash = workDir === undefined ? undefined : await rt.privateClash(workDir)+ if (clash) throw new Error(`${workDir} is in or around the folder of ${clash.name}, a private Bot, so a group cannot work there`)// No await from here until every change is written.const room = rt.roomOf(id)if (room === undefined) throw new Error('Unknown group chat')@@ -557,6 +570,7 @@ export function install(rt) {async function setMain(id, main, { primary = false, replace } = {}) {const bot = rt.botOf(id)if (bot === undefined) throw new Error('Unknown Bot')+ if (main && bot.private === true) throw new Error(`${bot.name} is private, so it cannot be a Main Bot`)const others = state.mainBotIds.filter(other => other !== id)let nextif (!main) {diff --git a/ds-bot/src/host/tools.js b/ds-bot/src/host/tools.jsindex 7a7942f8..2ecfcad6 100644--- a/ds-bot/src/host/tools.js+++ b/ds-bot/src/host/tools.js@@ -63,8 +63,10 @@ export function install(rt) {await rt.load()const self = rt.selfOf(exec.agent?.id)const lines = Object.values(state.bots).map(bot => {+ // A private Bot is only named to the others: not what it does, where, or what now.+ if (bot.private === true && bot.id !== self) return `- ${bot.name} (${[bot.role, 'private Bot, closed to other Bots'].filter(Boolean).join(', ')})`const model = bot.appliedModel ?? bot.model- const tags = [bot.role, model ? `model ${model}` : '', bot.workDir ? `works in ${bot.workDir}` : '', rt.isMain(bot.id) ? 'Main Bot' : '', bot.id === self ? 'you' : ''].filter(Boolean).join(', ')+ const tags = [bot.role, model ? `model ${model}` : '', bot.workDir ? `works in ${bot.workDir}` : '', rt.isMain(bot.id) ? 'Main Bot' : '', bot.private === true ? 'private' : '', bot.id === self ? 'you' : ''].filter(Boolean).join(', ')const job = bot.instructions ? ` — ${bot.instructions.split('\n')[0].slice(0, 120)}` : ''const now = bot.id === self ? '' : `\n now: ${rt.statusOf(bot.id)}`return `- ${bot.name}${tags ? ` (${tags})` : ''}${job}${now}`diff --git a/ds-bot/test/install.test.js b/ds-bot/test/install.test.jsindex c437e04c..7cd31790 100644--- a/ds-bot/test/install.test.js+++ b/ds-bot/test/install.test.js@@ -40,6 +40,7 @@ test('apply registers listeners, tools, and effects in a fixed order', (t) => {'on credentials/reference-updated','on settings/document-updated','on session/event',+ 'on tools/execute','effect ds-bot: agent tool restrict retry','on agent/created','effect ds-bot: identity section',diff --git a/ds-bot/test/privacy.test.js b/ds-bot/test/privacy.test.jsnew file mode 100644index 00000000..f87f33ab--- /dev/null+++ b/ds-bot/test/privacy.test.js@@ -0,0 +1,368 @@+import assert from 'node:assert/strict'+import { mkdirSync, mkdtempSync, readFileSync, realpathSync, rmSync, symlinkSync, writeFileSync } from 'node:fs'+import { tmpdir } from 'node:os'+import { join, relative } from 'node:path'+import { test } from 'node:test'+import { createRuntime } from '../index.js'+import { fakeApproval, fakeContext } from './fake-ctx.js'++// A Bot's id is a Session id, which a path may hold.+const P = 'd1a40000-1111-4222-8333-444444444444'++// Chief (Main), Writer in the group Ops, and Diary, a Bot with a folder of its own.+function seed({ diary, privateBot }) {+ return {+ version: 1,+ mainBotIds: ['a'],+ workspaceId: 'w1',+ bots: {+ a: { id: 'a', name: 'Chief', role: 'Boss', sessionId: 'a', createdAt: 1 },+ b: { id: 'b', name: 'Writer', role: 'Write', sessionId: 'b', createdAt: 2 },+ [P]: { id: P, name: 'Diary', role: 'Notes', sessionId: P, createdAt: 3, instructions: 'Keep the diary of the user.', workDir: diary, ...(privateBot ? { private: true } : {}) },+ },+ rooms: { r1: { id: 'r1', name: 'Ops', members: ['a', 'b'], mode: 'admin', admin: 'a', named: true, sessions: { b: 'gb' } } },+ agents: {},+ exchanges: [],+ exchangeTurns: {},+ questions: {},+ prefs: {},+ revision: 0,+ }+}++async function start({ services = {}, privateBot = true } = {}) {+ // The native realpath, as fs/promises uses: on Windows the JS one keeps 8.3 short names.+ const root = realpathSync.native(mkdtempSync(join(tmpdir(), 'ds-bot-privacy-')))+ const dsh = join(root, 'dsh')+ const home = join(dsh, 'bot')+ const team = join(home, 'workspace')+ const project = join(root, 'project')+ const diary = join(root, 'diary')+ for (const dir of [team, project, diary]) mkdirSync(dir, { recursive: true })+ writeFileSync(join(diary, 'notes.txt'), 'secret')+ writeFileSync(join(home, 'state.json'), JSON.stringify(seed({ diary, privateBot })))+ const previousHome = process.env.DSH_HOME+ process.env.DSH_HOME = dsh++ const ctx = fakeContext({ services })+ const agents = new Map()+ const agentOf = (sessionId) => {+ if (!agents.has(sessionId)) {+ agents.set(sessionId, {+ id: sessionId,+ session: { id: sessionId, header: { cwd: sessionId === P ? diary : team }, requestHeader: () => undefined, deriveMessages: () => [] },+ status: 'idle',+ followup() {},+ steer() {},+ inject() {},+ cancel() {},+ async whenIdle() {},+ async runMaintenance(fn) { return fn(AbortSignal.timeout(60_000)) },+ inbox: { nextStep: [], nextTurn: [], remove() {} },+ })+ }+ return agents.get(sessionId)+ }+ const registry = new Map([['w1', { id: 'w1', path: team, title: 'DS Bot' }]])+ Object.assign(ctx, {+ llm: { listProviders: () => [], listModels: async () => [], resolveModelInfo: async () => ({}) },+ workspaceRegistry: {+ get: id => registry.get(id),+ list: () => [...registry.values()],+ resolveByPath: async path => [...registry.values()].find(workspace => workspace.path === path),+ create: async (path) => {+ const workspace = { id: 'w-new', path, title: 'DS Bot' }+ registry.set(workspace.id, workspace)+ return workspace+ },+ archiveSession: async () => {},+ },+ sessionController: {+ create: async () => ({ sessionId: 's1' }),+ resolveAgent: async sessionId => ({ agent: agentOf(sessionId) }),+ selectModel: async () => {},+ page: async () => ({ records: [], hasMore: false }),+ },+ sessionTitle: { rename: async () => {} },+ sessions: { flush: async () => {} },+ })+ let route+ const register = ctx.connection.fetch.register+ ctx.connection.fetch.register = (options) => {+ route = options+ return register(options)+ }+ const rt = createRuntime(ctx, { home, github: false, diagnosticsFile: false })+ const call = async (endpoint, payload) => (await route.fetch(new Request('http://localhost/api/bot', {+ method: 'POST', body: JSON.stringify({ endpoint, payload }),+ }))).json()+ const state = () => JSON.parse(readFileSync(join(home, 'state.json'), 'utf8'))+ const tool = (name, args, sessionId) => ctx.registeredTools.get(name).execute(args, { agent: agentOf(sessionId), name, callId: 'c1' })+ // The privacy listener alone, which runs first; the last step stands for the tool.+ const guard = ctx.listeners.get('tools/execute')[0]+ const run = async (name, args, sessionId, agent = agentOf(sessionId)) => {+ let ran = false+ const exec = { name, arguments: args, agent, callId: 'c1', signal: AbortSignal.timeout(60_000) }+ try {+ await guard(exec, async () => { ran = true })+ return { ran }+ } catch (error) {+ return { ran, error: error.message }+ }+ }+ const stop = async () => {+ await ctx.dispose()+ if (previousHome === undefined) delete process.env.DSH_HOME+ else process.env.DSH_HOME = previousHome+ rmSync(root, { recursive: true, force: true, maxRetries: 5 })+ }+ // Loading once fills the Session-owner indexes.+ await tool('list_bots', {}, 'a')+ return { rt, ctx, root, dsh, home, team, project, diary, agentOf, call, tool, run, state, stop }+}++const CLOSED = /private Bot/++test('only the user makes a Bot private, and the Bot needs a folder of its own', async (t) => {+ const { rt, project, team, state, stop } = await start({ privateBot: false })+ t.after(stop)+ await assert.rejects(rt.updateBot(P, { private: true }, 'a'), /Only the user can make Diary private/)+ await assert.rejects(rt.updateBot('b', { private: true }), /needs a folder of its own/)+ await assert.rejects(rt.updateBot('b', { private: true, workDir: team }), /needs a folder of its own/)+ await assert.rejects(rt.updateBot('a', { private: true, workDir: project }), /Main Bot/)+ // Writer is in the group Ops, where every member reads what is said.+ await assert.rejects(rt.updateBot('b', { private: true, workDir: project }), /group chat/)+ assert.equal(state().bots.b.private, undefined)+ await rt.updateBot(P, { private: true })+ assert.equal(state().bots.p, undefined)+ assert.equal(state().bots[P].private, true)+ await rt.updateBot(P, { private: false })+ assert.equal(state().bots[P].private, undefined)+})++test('the user makes a Bot private through the client channel', async (t) => {+ const { call, state, stop } = await start({ privateBot: false })+ t.after(stop)+ await call('update-bot', { id: P, private: true })+ assert.equal(state().bots[P].private, true)+ await call('update-bot', { id: P, private: false })+ assert.equal(state().bots[P].private, undefined)+})++test('a private Bot has a folder that no other folder is in or around', async (t) => {+ const { rt, team, project, diary, state, stop } = await start({ privateBot: false })+ t.after(stop)+ mkdirSync(join(team, 'inside'))+ mkdirSync(join(diary, 'inside'))+ await rt.updateBot(P, { workDir: join(team, 'inside') })+ await assert.rejects(rt.updateBot(P, { private: true }), /team workspace/)+ await rt.updateBot(P, { workDir: diary })+ // Writer's folder lies in Diary's, so Diary cannot be private there.+ await rt.updateBot('b', { workDir: join(diary, 'inside') })+ await assert.rejects(rt.updateBot(P, { private: true }), /folder of Writer/)+ await rt.updateBot('b', { workDir: project })+ await rt.updateBot(P, { private: true })+ assert.equal(state().bots[P].private, true)+ // Once Diary is private, no other Bot, group or team workspace goes in or around its folder.+ await assert.rejects(rt.updateBot('b', { workDir: diary }), CLOSED)+ await assert.rejects(rt.updateBot('b', { workDir: join(diary, 'inside') }), CLOSED)+ await assert.rejects(rt.updateRoom('r1', { workDir: join(diary, 'inside') }), CLOSED)+ await assert.rejects(rt.createBot({ name: 'Third', workDir: diary }), CLOSED)+ await assert.rejects(rt.setTeamWorkspace(diary), CLOSED)+ await assert.rejects(rt.updateBot(P, { workDir: project }), /folder of Writer/)+ assert.equal(state().bots.b.workDir, project)+})++test('a private Bot is no Main Bot, no member of a group and no copy', async (t) => {+ const { rt, stop } = await start()+ t.after(stop)+ await assert.rejects(rt.setMain(P, true), /private/)+ await assert.rejects(rt.createRoom({ members: ['a', P], createdBy: 'a' }), /cannot be in a group chat/)+ await assert.rejects(rt.updateRoom('r1', { add: [P] }, 'a'), /cannot be in a group chat/)+ await assert.rejects(rt.duplicateBot(P), /cannot be copied/)+})++test('the others only see the name and role of a private Bot', async (t) => {+ const { tool, stop } = await start()+ t.after(stop)+ const others = await tool('list_bots', {}, 'b')+ assert.match(others, /- Diary \(Notes, private Bot, closed to other Bots\)/)+ assert.doesNotMatch(others, /Keep the diary|works in/)+ assert.doesNotMatch(others, /Diary[^\n]*\n\s+now:/)+ const own = await tool('list_bots', {}, P)+ assert.match(own, /Diary \(Notes, .*private, you\) — Keep the diary of the user\./)+})++test('other Bots cannot reach a private Bot through the tools between Bots', async (t) => {+ const { run, stop } = await start()+ t.after(stop)+ for (const [name, args, from] of [+ ['message_bot', { to: 'Diary', message: 'hi' }, 'a'],+ ['message_bot', { to: '@diary', message: 'hi' }, 'b'],+ ['message_bot', { to: P, message: 'hi' }, 'b'],+ ['stop_bot', { bot: 'Diary' }, 'a'],+ ['update_bot', { bot: 'Diary', instructions: 'x' }, 'a'],+ ['recall', { scope: 'Diary', query: 'x' }, 'a'],+ ['recall', { scope: '"Diary"' }, 'b'],+ ['remember', { scope: 'Diary', text: 'x' }, 'a'],+ ['forget', { scope: 'Diary', entry: 'x' }, 'a'],+ ]) {+ const result = await run(name, args, from)+ assert.equal(result.ran, false, `${name} from ${from}`)+ assert.match(result.error, CLOSED)+ }+ // The others still reach each other and the shared scopes.+ assert.deepEqual(await run('message_bot', { to: 'Writer', message: 'hi' }, 'a'), { ran: true })+ assert.deepEqual(await run('recall', { scope: 'team', query: 'x' }, 'b'), { ran: true })+ assert.deepEqual(await run('recall', { scope: 'own' }, P), { ran: true })+ assert.deepEqual(await run('update_bot', { bot: 'Diary', role: 'x' }, P), { ran: true })+})++test('other Bots cannot read, write or search what a private Bot keeps', async (t) => {+ const { run, root, home, diary, team, project, stop } = await start()+ t.after(stop)+ const notes = join(diary, 'notes.txt')+ const posix = path => path.replace(/\\/g, '/')+ const gitBash = /^[a-z]:/i.test(diary) ? `/${diary[0].toLowerCase()}${posix(diary).slice(2)}` : posix(diary)+ const refused = async (name, args, from = 'b') => {+ const result = await run(name, args, from)+ assert.equal(result.ran, false, `${name} ${JSON.stringify(args)}`)+ assert.match(result.error, CLOSED)+ }+ await refused('read', { file_path: notes })+ await refused('read', { file_path: relative(team, notes) })+ if (process.platform !== 'linux') await refused('read', { file_path: notes.toUpperCase() })+ await refused('write', { file_path: notes, content: 'x' })+ await refused('edit', { file_path: join(diary, 'new.txt'), old_string: 'a', new_string: 'b' })+ await refused('str_replace_editor', { command: 'view', path: notes })+ await refused('read_image', { file_path: join(diary, 'a.png') })+ await refused('read', { file_path: join(home, 'memory', 'bots', P, 'MEMORY.md') })+ await refused('read', { file_path: join(home, 'memory', 'journal.jsonl') })+ await refused('read', { file_path: join(home, 'state.json') })+ await refused('glob', { pattern: '**/*', path: diary })+ await refused('glob', { pattern: '**/*.txt', path: root })+ await refused('grep', { pattern: 'secret', path: root })+ await refused('glob', { pattern: `${posix(diary)}/**` })+ await refused('glob', { pattern: '**/*', path: home })+ await refused('bash', { command: `cat ${posix(notes)}` })+ await refused('bash', { command: `cat ${gitBash}/notes.txt` })+ await refused('pwsh', { command: `Get-Content '${notes}'`, workdir: project })+ await refused('bash', { command: 'ls', workdir: diary })+ await refused('bash', { command: `cat ${posix(join(home, 'state.json'))}` })+ await refused('working_directory', { cd: diary })+ await refused('working_directory', { cd: root })+ await refused('session_search', { query: 'x', session_id: P })+ await refused('session_trace', { session_ids: ['b', P] })+ await refused('session_event_read', { parent_session_ids: [P] })+ // What is open stays open.+ for (const [name, args] of [+ ['read', { file_path: join(project, 'a.txt') }],+ ['read', { file_path: 'a.txt' }],+ ['write', { file_path: join(team, 'a.txt'), content: 'x' }],+ ['glob', { pattern: '**/*', path: project }],+ ['glob', { pattern: '**/*', path: team }],+ ['grep', { pattern: 'x' }],+ ['bash', { command: 'ls', workdir: project }],+ ['working_directory', { cd: join(team, 'sub') }],+ ['session_search', { query: 'x', session_id: 'b' }],+ ]) {+ assert.deepEqual(await run(name, args, 'b'), { ran: true }, `${name} ${JSON.stringify(args)}`)+ }+})++test('a link into the folder of a private Bot is closed too', async (t) => {+ const { run, project, diary, stop } = await start()+ t.after(stop)+ try {+ symlinkSync(diary, join(project, 'link'), 'junction')+ } catch {+ t.skip('no links here')+ return+ }+ const result = await run('read', { file_path: join(project, 'link', 'notes.txt') }, 'b')+ assert.equal(result.ran, false)+ assert.match(result.error, CLOSED)+})++test('the private Bot reads its own and the team\'s files; a Session it starts speaks for it', async (t) => {+ const { run, diary, project, team, home, stop } = await start()+ t.after(stop)+ assert.deepEqual(await run('read', { file_path: join(diary, 'notes.txt') }, P), { ran: true })+ assert.deepEqual(await run('glob', { pattern: '**/*', path: diary }, P), { ran: true })+ // Its own data is no secret from it: with no other private Bot, the data folder is open to a search.+ assert.deepEqual(await run('glob', { pattern: '**/*', path: home }, P), { ran: true })+ assert.deepEqual(await run('read', { file_path: join(project, 'a.txt') }, P), { ran: true })+ assert.deepEqual(await run('glob', { pattern: '**/*', path: team }, P), { ran: true })+ const sub = parent => ({ id: 'sub1', session: { id: 'sub1', header: { cwd: team, parentSession: parent } } })+ const fromWriter = await run('read', { file_path: join(diary, 'notes.txt') }, 'b', sub('b'))+ assert.equal(fromWriter.ran, false)+ assert.match(fromWriter.error, CLOSED)+ assert.deepEqual(await run('read', { file_path: join(diary, 'notes.txt') }, P, sub(P)), { ran: true })+})++test('what a private Bot sends to the others waits for the user', async (t) => {+ const approval = fakeApproval('allowed-once')+ const { run, stop } = await start({ services: { approval } })+ t.after(stop)+ assert.deepEqual(await run('message_bot', { to: 'Writer', message: 'My secret plan' }, P), { ran: true })+ assert.equal(approval.requests.length, 1)+ assert.equal(approval.requests[0].toolName, 'message_bot')+ assert.match(approval.requests[0].reason, /Diary[\s\S]*Writer[\s\S]*My secret plan/)+ assert.match(approval.requests[0].displayReason.zh, /My secret plan/)+ // The Bots that are not private send as before.+ await run('message_bot', { to: 'Chief', message: 'hi' }, 'b')+ assert.equal(approval.requests.length, 1)+ assert.deepEqual(await run('save_capability', { name: 'plan', kind: 'skill', content: 'steps' }, P), { ran: true })+ assert.equal(approval.requests.length, 2)+ assert.match(approval.requests[1].reason, /share this with the team[\s\S]*steps/)+})++test('a message of a private Bot that the user does not allow is not sent', async (t) => {+ for (const outcome of ['rejected', 'cancelled']) {+ const { run, stop } = await start({ services: { approval: fakeApproval(outcome) } })+ t.after(stop)+ const result = await run('message_bot', { to: 'Writer', message: 'My secret plan' }, P)+ assert.equal(result.ran, false, outcome)+ assert.match(result.error, /not done/)+ }+ // With no way to ask, nothing goes either.+ const { run, stop } = await start()+ t.after(stop)+ const result = await run('message_bot', { to: 'Writer', message: 'x' }, P)+ assert.equal(result.ran, false)+ assert.match(result.error, /no way to ask/)+})++test('a private Bot changes files outside its folder only with the user\'s approval', async (t) => {+ const approval = fakeApproval('allowed-once')+ const { run, diary, project, stop } = await start({ services: { approval } })+ t.after(stop)+ assert.deepEqual(await run('write', { file_path: join(diary, 'today.txt'), content: 'x' }, P), { ran: true })+ assert.deepEqual(await run('write', { file_path: 'today.txt', content: 'x' }, P), { ran: true })+ assert.equal(approval.requests.length, 0)+ assert.deepEqual(await run('edit', { file_path: join(project, 'a.txt'), old_string: 'a', new_string: 'b' }, P), { ran: true })+ assert.equal(approval.requests.length, 1)+ assert.match(approval.requests[0].reason, /outside its own folder/)+ // A path that leaves the folder by `..` is outside it.+ assert.deepEqual(await run('write', { file_path: join(diary, '..', 'project', 'b.txt'), content: 'x' }, P), { ran: true })+ assert.equal(approval.requests.length, 2)+ // Reading is free.+ assert.deepEqual(await run('read', { file_path: join(project, 'a.txt') }, P), { ran: true })+ assert.equal(approval.requests.length, 2)+})++test('a write of a private Bot outside its folder that the user does not allow is not made', async (t) => {+ const { run, project, stop } = await start({ services: { approval: fakeApproval('rejected') } })+ t.after(stop)+ const result = await run('write', { file_path: join(project, 'a.txt'), content: 'x' }, P)+ assert.equal(result.ran, false)+ assert.match(result.error, /not done/)+})++test('with no private Bot the tools run as they did', async (t) => {+ const { run, diary, stop } = await start({ privateBot: false })+ t.after(stop)+ assert.deepEqual(await run('read', { file_path: join(diary, 'notes.txt') }, 'b'), { ran: true })+ assert.deepEqual(await run('message_bot', { to: 'Diary', message: 'hi' }, 'a'), { ran: true })+ assert.deepEqual(await run('glob', { pattern: '*', path: join(diary, '..') }, 'b'), { ran: true })+})