A team of long-lived bots for DeepSeek Harness, with main bots, bot-to-bot messages and group chats.
Make the last requested permission preset win when changes overlap
Fei24111a697629d998
diff --git a/ds-bot/src/host/permissions.js b/ds-bot/src/host/permissions.jsindex c59a584b..dee171f1 100644--- a/ds-bot/src/host/permissions.js+++ b/ds-bot/src/host/permissions.js@@ -20,16 +20,19 @@ export function install(rt) {const roomOfSession = sessionId => rt.roomOf(rt.groupOwners.get(sessionId)?.roomId)+ // `name` may be a function: it is read once the agent resolves, so a change asked+ // for while the resolve ran wins over the one still being applied.async function setPreset(sessionId, name) {const agent = await rt.resolveAgent(sessionId)- presets().set(agent.session, name)+ const wanted = typeof name === 'function' ? name() : name+ if (wanted) presets().set(agent.session, wanted)}// Sets the preset on one of the Bot's Sessions: its group's there, else its own; a Bot// without one keeps DSH's default. `room` is for a group Session not indexed yet.async function applyPermission(bot, sessionId, room = roomOfSession(sessionId)) {- const name = room?.permission ?? bot.permission- if (presets() === undefined || !name) return+ const name = () => room?.permission ?? bot.permission+ if (presets() === undefined || !name()) returnawait setPreset(sessionId, name)}@@ -44,8 +47,19 @@ export function install(rt) {async function setPermission(bot, name) {const wanted = checkPreset(name, 'a Bot\'s')- await applyPermission({ ...bot, permission: wanted }, rt.chatOf(bot.id))+ // The preset is written before its first await: a request still being applied+ // then reads the one asked for last.+ const previous = bot.permissionbot.permission = wanted+ try {+ await applyPermission(bot, rt.chatOf(bot.id))+ } catch (error) {+ if (bot.permission === wanted) {+ if (previous === undefined) delete bot.permission+ else bot.permission = previous+ }+ throw error+ }for (const sessionId of rt.groupSessionsOf(bot.id)) {if (roomOfSession(sessionId)?.permission) continueawait applyPermission(bot, sessionId)@@ -69,8 +83,7 @@ export function install(rt) {const bot = rt.botOf(botId)if (bot === undefined) continue// A member without a preset of its own goes back to the default its Session started on.- const next = wanted ?? bot.permission ?? service.catalog().defaultPreset- await setPreset(sessionId, next)+ await setPreset(sessionId, () => room.permission ?? bot.permission ?? service.catalog().defaultPreset).catch(error => rt.warn('ds-bot: permission for %s in %s not applied: %s', bot.name, room.name, error))}return truediff --git a/ds-bot/test/permissions.test.js b/ds-bot/test/permissions.test.jsindex a48431e8..975f6dd5 100644--- a/ds-bot/test/permissions.test.js+++ b/ds-bot/test/permissions.test.js@@ -5,8 +5,10 @@ import { install } from '../src/host/permissions.js'const PRESETS = { 'workspace-write': {}, 'danger-full-access': {} }const SESSION_ONLY = ['auto']-function runtime({ presets = true, auto = false, fail = false } = {}) {+function runtime({ presets = true, auto = false, fail = false, hold = false } = {}) {const writes = []+ const releases = []+ const bots = {}const service = {catalog: () => ({ options: [...Object.keys(PRESETS), ...(auto ? SESSION_ONLY : [])].map(value => ({ value, name: value })), defaultOptions: [], defaultPreset: 'workspace-write' }),resolve: (name) => { if (!(name in PRESETS) && !(auto && SESSION_ONLY.includes(name))) throw new Error(`permission: unknown preset "${name}"`); return PRESETS[name] },@@ -19,11 +21,19 @@ function runtime({ presets = true, auto = false, fail = false } = {}) {groupSessionsOf: id => [`${id}-group`],groupOwners: new Map(),roomOf: () => undefined,- resolveAgent: async sessionId => ({ session: sessionId }),+ botOf: id => bots[id],+ resolveAgent: sessionId => (hold ? new Promise(resolve => releases.push({ sessionId, resolve })) : Promise.resolve({ session: sessionId })),warn() {},}install(rt)- return { rt, writes }+ // With `hold`, every Session's agent resolves only when its index here is settled,+ // so an earlier request can be made to apply after a later one.+ const settle = async (index) => {+ const [{ sessionId, resolve }] = releases.splice(index, 1)+ resolve({ session: sessionId })+ await new Promise(done => setImmediate(done))+ }+ return { rt, writes, releases, bots, settle }}test('a Bot\'s preset is set on its chat and its group Sessions', async () => {@@ -69,3 +79,39 @@ test('a preset the Bot\'s own chat refuses is not kept', async () => {await assert.rejects(rt.setPermission(bot, 'danger-full-access'), /rejected/)assert.equal(bot.permission, 'workspace-write')})++test('the last preset asked for wins when two Bot changes overlap', async () => {+ const { rt, writes, releases, settle } = runtime({ hold: true })+ const bot = { id: 'b1', name: 'Ada' }+ const first = rt.setPermission(bot, 'workspace-write')+ const second = rt.setPermission(bot, 'danger-full-access')+ assert.equal(releases.length, 2)+ await settle(1) // the later request applies to the chat, then to the group Session+ await settle(1)+ await settle(0) // the earlier request applies last and must not win+ await settle(0)+ await first+ await second+ assert.equal(bot.permission, 'danger-full-access')+ assert.equal(writes.length, 4)+ assert.ok(writes.every(write => write.endsWith('=danger-full-access')))+})++test('the last preset asked for wins when two group changes overlap', async () => {+ const { rt, writes, releases, bots, settle } = runtime({ hold: true })+ bots.b1 = { id: 'b1', name: 'Ada' }+ bots.b2 = { id: 'b2', name: 'Ben' }+ const room = { name: 'Ops', sessions: { b1: 's1', b2: 's2' } }+ const first = rt.setRoomPermission(room, 'workspace-write')+ const second = rt.setRoomPermission(room, 'danger-full-access')+ assert.equal(releases.length, 2)+ await settle(1) // the later request reaches s1, then s2, before the earlier one does+ await settle(1)+ await settle(0) // the earlier request applies last and must not win+ await settle(0)+ await first+ await second+ assert.equal(room.permission, 'danger-full-access')+ assert.equal(writes.length, 4)+ assert.ok(writes.every(write => write.endsWith('=danger-full-access')))+})