Skip to content

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

Make the task move idempotent

Fei24119985e4a218cc
diff --git a/ds-bot/src/host/schedules.js b/ds-bot/src/host/schedules.js
index 469d8134..df0e94ae 100644
--- a/ds-bot/src/host/schedules.js
+++ b/ds-bot/src/host/schedules.js
@@ -39,6 +39,11 @@ function requestOf(record) {
return null
}
+// Two stored records describe the same task when their create requests match.
+function keyOfRequest(request) {
+ return request === null ? '' : JSON.stringify(request)
+}
+
// The settings form's timing as a create request selector.
function selectorOf(when) {
const zone = typeof when?.timeZone === 'string' && when.timeZone !== '' ? when.timeZone : 'UTC'
@@ -135,11 +140,22 @@ export function install(rt) {
rt.warn('ds-bot: tasks of %s not moved: %s', fromId, error?.message ?? error)
return
}
+ // A retry after a crash between create and delete must not copy a task twice.
+ let moved
+ try {
+ moved = new Set((await service.list({ sessionId: toId })).map(record => keyOfRequest(requestOf(record))))
+ } catch {
+ moved = new Set()
+ }
for (const record of records) {
const request = requestOf(record)
if (request === null || (request.at !== undefined && Date.parse(request.at) <= Date.now())) continue
try {
- await service.create(toId, request)
+ const key = keyOfRequest(request)
+ if (!moved.has(key)) {
+ await service.create(toId, request)
+ moved.add(key)
+ }
await service.delete({ sessionId: fromId, id: record.id })
} catch (error) {
rt.warn('ds-bot: task %s stayed with the earlier part: %s', record.title, error?.message ?? error)
diff --git a/ds-bot/test/schedules.test.js b/ds-bot/test/schedules.test.js
index f019f7a1..30c632bd 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'
@@ -29,8 +29,18 @@ function scheduleService() {
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 kind = { every_seconds: 'every' }[Object.keys(request).find(key => !['title', 'prompt'].includes(key))]
+ ?? Object.keys(request).find(key => !['title', 'prompt'].includes(key))
+ const record = {
+ id, kind, title: request.title, prompt: request.prompt,
+ timing: request[Object.keys(request).find(key => !['title', 'prompt'].includes(key))],
+ scheduledAt: request.at ?? '2030-01-01T01:00:00.000Z',
+ everySeconds: request.every_seconds,
+ time: request.daily?.time ?? request.weekly?.time,
+ timeZone: request.daily?.time_zone ?? request.weekly?.time_zone ?? request.cron?.time_zone,
+ weekdays: request.weekly?.weekdays,
+ expression: request.cron?.expression,
+ }
tasks.set(id, { sessionId, record })
return record
},
@@ -50,9 +60,9 @@ function setup(services = {}) {
const ctx = fakeContext({ services })
const register = ctx.connection.fetch.register
ctx.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, call, rt }
}
test('without the schedule service the page says so and creating is refused', async (t) => {
@@ -121,3 +131,24 @@ 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')
})
+
+// #571: a restart pass re-runs the task move; a crash between create and delete must
+// not leave a second copy of a recurring task on the fresh part.
+test('a repeated task move copies each task only once', async (t) => {
+ const schedule = scheduleService()
+ const { ctx, home, rt } = setup({ schedule })
+ t.after(async () => { await ctx.dispose(); rmSync(home, { recursive: true, force: true, maxRetries: 5 }) })
+ await schedule.create('w2', { title: 'Ping', prompt: 'Check', every_seconds: 3600 })
+
+ const remove = schedule.delete
+ schedule.delete = async () => { throw new Error('down') }
+ await rt.moveSchedules('w2', 's9')
+ const on = id => [...schedule.tasks.values()].filter(task => task.sessionId === id)
+ assert.equal(on('s9').length, 1)
+ assert.equal(on('w2').length, 1)
+
+ schedule.delete = remove
+ await rt.moveSchedules('w2', 's9')
+ assert.equal(on('s9').length, 1)
+ assert.equal(on('w2').length, 0)
+})