A team of long-lived bots for DeepSeek Harness, with main bots, bot-to-bot messages and group chats.
Roll back duplicated moved schedules
klarkxy0c12dc6e1237
diff --git a/ds-bot/src/host/schedules.js b/ds-bot/src/host/schedules.jsindex 469d8134..74b21318 100644--- a/ds-bot/src/host/schedules.js+++ b/ds-bot/src/host/schedules.js@@ -124,6 +124,18 @@ export function install(rt) {},}+ // Removes one task and says whether it is gone. The service refuses a task it+ // does not own instead of failing, so a refusal is a removal that did not happen.+ async function remove(sessionId, id) {+ try {+ const result = await service.delete({ sessionId, id })+ if (result?.deleted === true) return { removed: true }+ return { removed: false, reason: result?.code ?? 'the service refused the removal' }+ } catch (error) {+ return { removed: false, reason: error?.message ?? error }+ }+ }+// A fresh part takes over the earlier part's active tasks: each is created again on// the fresh Session before the old one goes, so a failure leaves it where it was.async function moveSchedules(fromId, toId) {@@ -138,12 +150,20 @@ export function install(rt) {for (const record of records) {const request = requestOf(record)if (request === null || (request.at !== undefined && Date.parse(request.at) <= Date.now())) continue+ let createdtry {- await service.create(toId, request)- await service.delete({ sessionId: fromId, id: record.id })+ created = await service.create(toId, request)} catch (error) {rt.warn('ds-bot: task %s stayed with the earlier part: %s', record.title, error?.message ?? error)+ continue}+ const old = await remove(fromId, record.id)+ if (old.removed) continue+ // The earlier part still holds the task, so the fresh copy has to go: two armed+ // copies of one task would fire twice.+ const fresh = await remove(toId, created.id)+ if (fresh.removed) rt.warn('ds-bot: task %s stayed with the earlier part: %s', record.title, old.reason)+ else rt.warn('ds-bot: task %s left in two parts: %s', record.title, fresh.reason)}}diff --git a/ds-bot/test/schedules.test.js b/ds-bot/test/schedules.test.jsindex f019f7a1..d2efe3e3 100644--- a/ds-bot/test/schedules.test.js+++ b/ds-bot/test/schedules.test.js@@ -3,7 +3,7 @@ import { mkdtempSync, rmSync, writeFileSync } from 'node:fs'import { tmpdir } from 'node:os'import { join } from 'node:path'import { test } from 'node:test'-import { apply } from '../index.js'+import { createRuntime } from '../index.js'import { fakeContext } from './fake-ctx.js'import { reminderText } from '../src/host/schedules.js'@@ -21,22 +21,35 @@ const TEAM = {questions: {},}-// An in-memory stand-in for DSH's schedule service.-function scheduleService() {+// The record DSH stores, so a task that is moved can be read back.+function stored(id, request) {+ const base = { id, title: request.title, prompt: request.prompt, scheduledAt: '2030-01-01T01:00:00.000Z' }+ if (request.every_seconds !== undefined) return { ...base, kind: 'every', everySeconds: request.every_seconds, timing: request.every_seconds }+ if (request.daily !== undefined) return { ...base, kind: 'daily', time: request.daily.time, timeZone: request.daily.time_zone, timing: request.daily }+ if (request.weekly !== undefined) return { ...base, kind: 'weekly', time: request.weekly.time, timeZone: request.weekly.time_zone, weekdays: request.weekly.weekdays, timing: request.weekly }+ return { ...base, kind: 'at', timing: request.at }+}++// An in-memory stand-in for DSH's schedule service. `broken` tells it how a removal+// fails on a Session: 'throw' for a failed write, 'refuse' for the not-found result+// the service returns instead of failing.+function scheduleService({ broken = {} } = {}) {const tasks = new Map()let next = 1return {tasks,async create(sessionId, request) {const id = `schedule-${next++}`- const kind = Object.keys(request).find(key => !['title', 'prompt'].includes(key))- const record = { id, kind, title: request.title, prompt: request.prompt, timing: request[kind], scheduledAt: '2030-01-01T01:00:00.000Z' }+ const record = stored(id, request)tasks.set(id, { sessionId, record })return record},async list({ sessionId }) { return [...tasks.values()].filter(task => task.sessionId === sessionId).map(task => task.record) },async catalog() { return [...tasks.values()].map(task => ({ ...task.record, sessionId: task.sessionId, status: 'active' })) },async delete({ sessionId, id }) {+ const failure = broken[sessionId]+ if (failure === 'throw') throw new Error('the schedule store refused the removal')+ if (failure === 'refuse') return { id, deleted: false, code: 'schedule_not_found' }if (tasks.get(id)?.sessionId !== sessionId) return { id, deleted: false, code: 'schedule_not_found' }tasks.delete(id)return { id, deleted: true }@@ -50,9 +63,9 @@ function setup(services = {}) {const ctx = fakeContext({ services })const register = ctx.connection.fetch.registerctx.connection.fetch.register = (route) => { ctx.route = route; return register(route) }- apply(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 }+ return { ctx, home, rt, call }}test('without the schedule service the page says so and creating is refused', async (t) => {@@ -121,3 +134,60 @@ test('a due reminder reaches a group as its prompts', () => {assert.equal(reminderText(batch), '[Scheduled task] One\n[Scheduled task] Two')assert.equal(reminderText('plain text'), 'plain text')})++// #511: a Bot's chat moves on to a fresh part, and its task moves with it. The+// earlier part of a Bot's chat is 'w1' and the fresh one is 'w2'.+async function moveSetup(broken = {}) {+ const schedule = scheduleService({ broken })+ const { ctx, home, rt } = setup({ schedule })+ await schedule.create('w1', { title: 'Standup', prompt: 'Where are we?', every_seconds: 3600 })+ const warnings = []+ rt.warn = (...args) => warnings.push(args)+ return { schedule, ctx, home, rt, warnings }+}++test('a task that moves goes to the fresh part alone', async (t) => {+ const { schedule, ctx, home, rt, warnings } = await moveSetup()+ t.after(async () => { await ctx.dispose(); rmSync(home, { recursive: true, force: true, maxRetries: 5 }) })+ await rt.moveSchedules('w1', 'w2')+ const armed = [...schedule.tasks.values()]+ assert.equal(armed.length, 1, JSON.stringify(armed))+ assert.equal(armed[0].sessionId, 'w2')+ assert.equal(armed[0].record.title, 'Standup')+ assert.deepEqual(warnings, [])+})++test('a task whose removal fails stays with the earlier part alone', async (t) => {+ const { schedule, ctx, home, rt, warnings } = await moveSetup({ w1: 'throw' })+ t.after(async () => { await ctx.dispose(); rmSync(home, { recursive: true, force: true, maxRetries: 5 }) })+ await rt.moveSchedules('w1', 'w2')+ const armed = [...schedule.tasks.values()]+ assert.equal(armed.length, 1, JSON.stringify(armed))+ assert.equal(armed[0].sessionId, 'w1')+ assert.equal(armed[0].record.title, 'Standup')+ assert.equal(warnings.length, 1)+ assert.match(warnings[0][0], /stayed with the earlier part/)+})++test('a task the service refuses to remove stays with the earlier part alone', async (t) => {+ const { schedule, ctx, home, rt, warnings } = await moveSetup({ w1: 'refuse' })+ t.after(async () => { await ctx.dispose(); rmSync(home, { recursive: true, force: true, maxRetries: 5 }) })+ await rt.moveSchedules('w1', 'w2')+ const armed = [...schedule.tasks.values()]+ assert.equal(armed.length, 1, JSON.stringify(armed))+ assert.equal(armed[0].sessionId, 'w1')+ assert.equal(armed[0].record.title, 'Standup')+ assert.equal(warnings.length, 1)+ assert.match(warnings[0][0], /stayed with the earlier part/)+})++test('a task that cannot be rolled back is reported as left in two parts', async (t) => {+ const { schedule, ctx, home, rt, warnings } = await moveSetup({ w1: 'throw', w2: 'throw' })+ t.after(async () => { await ctx.dispose(); rmSync(home, { recursive: true, force: true, maxRetries: 5 }) })+ await rt.moveSchedules('w1', 'w2')+ const armed = [...schedule.tasks.values()]+ assert.equal(armed.length, 2, JSON.stringify(armed))+ assert.deepEqual(armed.map(task => task.sessionId).sort(), ['w1', 'w2'])+ assert.equal(warnings.length, 1)+ assert.match(warnings[0][0], /left in two parts/)+})