Skip to content

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

Preserve scheduled tasks before archiving old conversation parts

Fei2411980e6f4f7afc
diff --cc ds-bot/src/host/parts.js
index 9905360d,5ec7222c..712a0a57
--- a/ds-bot/src/host/parts.js
+++ b/ds-bot/src/host/parts.js
@@@ -879,10 -876,7 +879,14 @@@ export function install(rt)
}
if (job.moved) {
rt.renewMemory(agent.id)
- // The task move started inside the switch; stopping the earlier part while it
- // still runs would drop the tasks it has not copied yet.
- await job.movedSchedules
- await ctx.workspaceRegistry.archiveSession(agent.id)
++ try {
++ await rt.moveSchedules(agent.id, currentOf(job))
++ } catch (error) {
++ // Keep the restart marker: stopping this part would delete unmoved tasks.
++ job.kept = true
++ throw error
++ }
+ await ctx.workspaceRegistry.archiveSession(agent.id, { stopActivity: true })
.catch(error => rt.warn('ds-bot: earlier part %s not archived: %s', agent.id, error))
}
}
@@@ -983,7 -977,7 +987,6 @@@
const now = await freshRoute(bot, session)
if (now !== undefined && (now.ref !== picked?.ref || now.effort !== picked?.effort)) await selectRoute(bot, freshId, now)
if (job.failed !== undefined) rescued.set(freshId, job.bare ? 'bare' : 'carried')
- job.movedSchedules = rt.moveSchedules(agent.id, freshId)
- void rt.moveSchedules(agent.id, freshId)
job.moved = true
const resume = job.resume || job.overflow
// Then what the old part held: the stopped turn's input, then whatever waited. It
diff --cc ds-bot/src/host/schedules.js
index d8ee9613,469d8134..1d14a37f
--- a/ds-bot/src/host/schedules.js
+++ b/ds-bot/src/host/schedules.js
@@@ -133,41 -128,21 +128,18 @@@ export function install(rt)
// the fresh Session before the old one goes, so a failure leaves it where it was.
async function moveSchedules(fromId, toId) {
if (service === null) return
-- let records
-- try {
-- records = await service.list({ sessionId: fromId })
-- } catch (error) {
-- 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.
- // Copies already on the fresh Session satisfy earlier records of the same
- // request in order, so two genuinely identical tasks still both move.
- const have = new Map()
- try {
- for (const record of await service.list({ sessionId: toId })) {
- const key = keyOfRequest(requestOf(record))
- have.set(key, (have.get(key) ?? 0) + 1)
- }
- } catch {
- // Without a read-back every source task is copied; a retry then adds one copy
- // per source record, which is the correct multiplicity anyway.
-- }
- const seen = new Map()
++ const records = await service.list({ sessionId: fromId })
for (const record of records) {
const request = requestOf(record)
if (request === null || (request.at !== undefined && Date.parse(request.at) <= Date.now())) continue
++ // A failed read or copy must prevent stopActivity from deleting an unmoved
++ // task. Equal requests can be separate tasks; copy each source record.
++ await service.create(toId, request)
try {
- const key = keyOfRequest(request)
- const nth = (seen.get(key) ?? 0) + 1
- seen.set(key, nth)
- if (nth > (have.get(key) ?? 0)) {
- await service.create(toId, request)
- have.set(key, (have.get(key) ?? 0) + 1)
- }
- await service.create(toId, request)
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)
++ // The copy is safe; archiving the earlier part will remove its old task.
++ rt.warn('ds-bot: earlier copy of task %s not removed: %s', record.title, error?.message ?? error)
}
}
}
diff --cc ds-bot/test/schedules.test.js
index a6eaf6aa,f019f7a1..3491b669
--- a/ds-bot/test/schedules.test.js
+++ b/ds-bot/test/schedules.test.js
@@@ -131,25 -121,3 +131,27 @@@ test('a due reminder reaches a group a
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) => {
++test('a failed task move can resume without losing identical remaining tasks', 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 })
+ 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 create = schedule.create
++ let attempts = 0
++ schedule.create = async (...args) => {
++ if (++attempts === 2) throw new Error('down')
++ return create(...args)
++ }
++ await assert.rejects(rt.moveSchedules('w2', 's9'), /down/)
+ const on = id => [...schedule.tasks.values()].filter(task => task.sessionId === id)
- assert.equal(on('s9').length, 2)
- assert.equal(on('w2').length, 2)
++ assert.equal(on('s9').length, 1)
++ assert.equal(on('w2').length, 1)
+
- schedule.delete = remove
++ schedule.create = create
+ await rt.moveSchedules('w2', 's9')
+ assert.equal(on('s9').length, 2)
+ assert.equal(on('w2').length, 0)
+})
diff --cc ds-bot/test/switch-keeps-input.test.js
index 6ac7b639,1dff409d..ae0afa84
--- a/ds-bot/test/switch-keeps-input.test.js
+++ b/ds-bot/test/switch-keeps-input.test.js
@@@ -329,26 -329,6 +329,42 @@@ test('a marked part that stayed curren
assert.deepEqual(second.state().switching, {})
})
+// #571: archiving with stopActivity drops the tasks the earlier part still owns, so
+// the archive waits for the task move the switch started.
+test('an earlier part is stopped only after its tasks moved to the fresh part', async (t) => {
+ const host = await start({ services })
+ t.after(host.stop)
+ const order = []
+ let release
+ const moved = new Promise(resolve => { release = resolve })
+ host.rt.moveSchedules = async (from, to) => { order.push(['move', from, to]); await moved }
+ const archive = host.ctx.workspaceRegistry.archiveSession
+ host.ctx.workspaceRegistry.archiveSession = async (id, options) => { order.push(['archive', id]); return archive(id, options) }
+ await moveWriter(host, [])
+ await until(() => order.length >= 1)
+ await settle()
+ assert.deepEqual(order, [['move', 'b', 's1']])
+ release()
+ await until(() => order.some(([kind]) => kind === 'archive'))
+ assert.deepEqual(order, [['move', 'b', 's1'], ['archive', 'b']])
+})
+
++test('a failed task move leaves the earlier part unarchived and marked for retry', async (t) => {
++ const host = await start({ services })
++ t.after(host.stop)
++ host.rt.moveSchedules = async () => { throw new Error('task storage unavailable') }
++ await moveWriter(host, [])
++ await until(() => host.warnings.some(line => line.includes('task storage unavailable')))
++ await settle()
++ assert.deepEqual(host.archived, [])
++ assert.deepEqual(host.state().switching, { b: { botId: 'b' } })
++
++ host.rt.moveSchedules = async () => {}
++ await host.rt.heldBack()
++ assert.deepEqual(host.archived, [['b', { stopActivity: true }]])
++ assert.deepEqual(host.state().switching, {})
++})
++
test('unmarked earlier parts are never resolved by the restart pass', async (t) => {
const host = await start({ services })
await moveWriter(host, [])