A team of long-lived bots for DeepSeek Harness, with main bots, bot-to-bot messages and group chats.
Stop a superseded preset change writing Sessions; restore all on rollback
Fei2411353e10aaeab4
diff --git a/ds-bot/src/host/permissions.js b/ds-bot/src/host/permissions.jsindex 371bd844..f8a5f235 100644--- a/ds-bot/src/host/permissions.js+++ b/ds-bot/src/host/permissions.js@@ -60,21 +60,35 @@ export function install(rt) {// The preset is written before its first await: a request still being applied// then reads the one asked for last.bot.permission = wanted+ // The preset for one Session is read once its agent resolves: undefined once a+ // newer request exists, so a superseded request writes no more Sessions — every+ // Session the Bot has ends at the value the newest request actually applied.+ const applyTo = (sessionId, room = roomOfSession(sessionId)) =>+ setPreset(sessionId, () => (request.seq === mine ? room?.permission ?? bot.permission : undefined))+ const groups = async (warnFail) => {+ for (const sessionId of rt.groupSessionsOf(bot.id)) {+ if (request.seq !== mine) break+ if (roomOfSession(sessionId)?.permission) continue+ await applyTo(sessionId).catch(warnFail)+ }+ }try {- const applied = await applyPermission(bot, rt.chatOf(bot.id))+ const applied = await applyTo(rt.chatOf(bot.id))if (applied !== undefined) request.applied = applied} catch (error) {if (request.seq === mine) {if (request.applied === undefined) delete bot.permission- else bot.permission = request.applied+ else {+ bot.permission = request.applied+ // The restored preset goes back on every Session the change left behind.+ const warnFail = error => rt.warn('ds-bot: permission for %s not restored: %s', bot.name, error)+ await applyTo(rt.chatOf(bot.id)).catch(warnFail)+ await groups(warnFail)+ }}throw error}- for (const sessionId of rt.groupSessionsOf(bot.id)) {- if (roomOfSession(sessionId)?.permission) continue- await applyPermission(bot, sessionId)- .catch(error => rt.warn('ds-bot: permission for %s in a group not applied: %s', bot.name, error))- }+ await groups(error => rt.warn('ds-bot: permission for %s in a group not applied: %s', bot.name, error))}// A group's preset, or null when each member uses its own.diff --git a/ds-bot/test/permissions.test.js b/ds-bot/test/permissions.test.jsindex 8f2f0891..c928aad3 100644--- a/ds-bot/test/permissions.test.js+++ b/ds-bot/test/permissions.test.js@@ -23,7 +23,7 @@ function runtime({ presets = true, auto = false, fail = false, hold = false } =groupOwners: new Map(),roomOf: () => undefined,botOf: id => bots[id],- resolveAgent: sessionId => (hold ? new Promise(resolve => releases.push({ sessionId, resolve })) : Promise.resolve({ session: sessionId })),+ resolveAgent: sessionId => (hold ? new Promise((resolve, reject) => releases.push({ sessionId, resolve, reject })) : Promise.resolve({ session: sessionId })),warn() {},}install(rt)@@ -34,7 +34,12 @@ 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, control }+ const drop = async (index) => {+ const [{ reject }] = releases.splice(index, 1)+ reject(new Error('permission: session went away'))+ await new Promise(done => setImmediate(done))+ }+ return { rt, writes, releases, bots, settle, drop, control }}test('a Bot\'s preset is set on its chat and its group Sessions', async () => {@@ -89,17 +94,15 @@ test('the last preset asked for wins when two Bot changes overlap', async () =>assert.equal(releases.length, 2)await settle(1) // the later request applies to the chat, then to the group Sessionawait settle(1)- await settle(0) // the earlier request applies last and must not win- await settle(0)+ while (releases.length) await settle(0) // the earlier request writes nothing moreawait firstawait secondassert.equal(bot.permission, 'danger-full-access')- assert.equal(writes.length, 4)- assert.ok(writes.every(write => write.endsWith('=danger-full-access')))+ assert.deepEqual(writes, ['b1-chat=danger-full-access', 'b1-group=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 { rt, releases, settle, drop } = 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')@@ -108,27 +111,46 @@ test('a failed change does not roll back a newer one that already applied', asynawait settle(1) // the newer request applies to the chat, then to the group Sessionawait settle(1)await second- control.fail = true- const refused = assert.rejects(first, /rejected/)- await settle(0) // the older request's chat apply fails now+ const refused = assert.rejects(first, /went away/)+ await drop(0) // the older request's apply resolves only now, and failsawait refusedassert.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 { rt, releases, settle, drop, control } = runtime({ hold: 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+ const second = rt.setPermission(bot, 'danger-full-access')+ const firstFails = assert.rejects(first, /went away/)+ await drop(0) // the superseded request fails and leaves the field to the newer oneawait firstFails+ control.fail = trueconst secondFails = assert.rejects(second, /rejected/)- await settle(0) // the second fails too: the field goes back to what applied before+ while (releases.length) await settle(0) // the newest one fails too and rolls backawait secondFailsassert.equal(bot.permission, 'workspace-write')})+test('a superseded change leaves no Session on a preset that lost', async () => {+ const { rt, writes, releases, settle, control } = runtime({ hold: true })+ const bot = { id: 'b1', name: 'Ada' }+ const first = rt.setPermission(bot, 'workspace-write')+ await settle(0) // the older request sets the chat, then waits on the group Session+ const second = rt.setPermission(bot, 'danger-full-access')+ assert.equal(releases.length, 2)+ await settle(0) // the older request's group apply resolves: a newer one owns it now+ control.fail = true+ const refused = assert.rejects(second, /rejected/)+ await settle(0) // the newer request's chat apply fails and rolls the field back+ control.fail = false+ while (releases.length) await settle(0) // the rollback puts the restored preset back+ await refused+ await first+ assert.equal(bot.permission, 'workspace-write')+ assert.deepEqual(writes, ['b1-chat=workspace-write', 'b1-chat=workspace-write', 'b1-group=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' }