Skip to content

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

Roll a failed preset change back only to the last applied one

Fei2411e874d014db5d
diff --git a/ds-bot/src/host/permissions.js b/ds-bot/src/host/permissions.js
index dee171f1..371bd844 100644
--- a/ds-bot/src/host/permissions.js
+++ b/ds-bot/src/host/permissions.js
@@ -26,14 +26,16 @@ export function install(rt) {
const agent = await rt.resolveAgent(sessionId)
const wanted = typeof name === 'function' ? name() : name
if (wanted) presets().set(agent.session, wanted)
+ return 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.
+ // Returns the preset it set, or undefined when it set none.
async function applyPermission(bot, sessionId, room = roomOfSession(sessionId)) {
const name = () => room?.permission ?? bot.permission
if (presets() === undefined || !name()) return
- await setPreset(sessionId, name)
+ return setPreset(sessionId, name)
}
function checkPreset(name, whose) {
@@ -45,18 +47,26 @@ export function install(rt) {
return wanted
}
+ // Per Bot, requests are numbered and the last preset an apply actually set is
+ // tracked: only the newest request may roll the field back, and then to that
+ // applied value, never to a value a request had only asked for.
+ const requests = new Map()
+
async function setPermission(bot, name) {
const wanted = checkPreset(name, 'a Bot\'s')
+ const request = requests.get(bot.id) ?? { seq: 0, applied: bot.permission }
+ const mine = ++request.seq
+ requests.set(bot.id, request)
// The preset is written before its first await: a request still being applied
// then reads the one asked for last.
- const previous = bot.permission
bot.permission = wanted
try {
- await applyPermission(bot, rt.chatOf(bot.id))
+ const applied = await applyPermission(bot, rt.chatOf(bot.id))
+ if (applied !== undefined) request.applied = applied
} catch (error) {
- if (bot.permission === wanted) {
- if (previous === undefined) delete bot.permission
- else bot.permission = previous
+ if (request.seq === mine) {
+ if (request.applied === undefined) delete bot.permission
+ else bot.permission = request.applied
}
throw error
}
diff --git a/ds-bot/test/permissions.test.js b/ds-bot/test/permissions.test.js
index 975f6dd5..8f2f0891 100644
--- a/ds-bot/test/permissions.test.js
+++ b/ds-bot/test/permissions.test.js
@@ -9,10 +9,11 @@ function runtime({ presets = true, auto = false, fail = false, hold = false } =
const writes = []
const releases = []
const bots = {}
+ const control = { fail }
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] },
- set: (session, name) => { if (fail) throw new Error('permission: rejected'); writes.push(`${session}=${name}`) },
+ set: (session, name) => { if (control.fail) throw new Error('permission: rejected'); writes.push(`${session}=${name}`) },
}
const ctx = { get: name => (presets && name === 'permissionPresets' ? service : undefined) }
const rt = {
@@ -33,7 +34,7 @@ function runtime({ presets = true, auto = false, fail = false, hold = false } =
resolve({ session: sessionId })
await new Promise(done => setImmediate(done))
}
- return { rt, writes, releases, bots, settle }
+ return { rt, writes, releases, bots, settle, control }
}
test('a Bot\'s preset is set on its chat and its group Sessions', async () => {
@@ -97,6 +98,37 @@ test('the last preset asked for wins when two Bot changes overlap', async () =>
assert.ok(writes.every(write => write.endsWith('=danger-full-access')))
})
+test('a failed change does not roll back a newer one that already applied', async () => {
+ const { rt, releases, settle, control } = runtime({ hold: true })
+ const bot = { id: 'b1', name: 'Ada', permission: 'workspace-write' }
+ // Two windows ask for the same preset; the newer applies everywhere first.
+ const first = rt.setPermission(bot, 'danger-full-access')
+ const second = rt.setPermission(bot, 'danger-full-access')
+ assert.equal(releases.length, 2)
+ await settle(1) // the newer request applies to the chat, then to the group Session
+ await settle(1)
+ await second
+ control.fail = true
+ const refused = assert.rejects(first, /rejected/)
+ await settle(0) // the older request's chat apply fails now
+ await refused
+ assert.equal(bot.permission, 'danger-full-access')
+})
+
+test('a failed change rolls back to the last applied preset, not to a failed request', async () => {
+ const { rt, settle } = runtime({ hold: true, fail: true })
+ const bot = { id: 'b1', name: 'Ada', permission: 'workspace-write' }
+ const first = rt.setPermission(bot, 'danger-full-access')
+ const second = rt.setPermission(bot, 'workspace-write')
+ const firstFails = assert.rejects(first, /rejected/)
+ await settle(0) // the first request fails and leaves the field to the newer one
+ await firstFails
+ const secondFails = assert.rejects(second, /rejected/)
+ await settle(0) // the second fails too: the field goes back to what applied before
+ await secondFails
+ assert.equal(bot.permission, 'workspace-write')
+})
+
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' }