A team of long-lived bots for DeepSeek Harness, with main bots, bot-to-bot messages and group chats.
Serve DSH 0.2.2's Host-wide scheduled tasks
Fei2411d92229f16586
diff --git a/ds-bot/client.js b/ds-bot/client.jsindex 06bc894b..d16d6b93 100644--- a/ds-bot/client.js+++ b/ds-bot/client.js@@ -1143,6 +1143,7 @@ var ZH = {"Every weekday at {time}": "每个工作日 {time}","Every weekend at {time}": "每个周末 {time}","On the schedule {expression}": "按计划 {expression}",+ "Custom schedule": "自定义计划","Next: {time}": "下次 {time}","Overdue: {time}": "已逾期 {time}",Finished: "已结束",@@ -13623,12 +13624,15 @@ function timingText(task) {}case "cron":return `${t("On the schedule {expression}", { expression: task.expression })}${zone}`;+ case "rrule":+ return t("Custom schedule");default:return "";}}var nextText = (task) => {if (task.state === "overdue" || task.state === void 0 && task.status === "active" && Date.parse(task.scheduledAt) <= Date.now()) return t("Overdue: {time}", { time: dayAndTime(Date.parse(task.scheduledAt)) });+ if (task.status === "paused") return t("Paused");return task.status === "active" ? t("Next: {time}", { time: dayAndTime(Date.parse(task.scheduledAt)) }) : t("Finished");};function TaskRow2({ task, actions, onView, showBot, roster }) {diff --git a/ds-bot/src/client/i18n.js b/ds-bot/src/client/i18n.jsindex 1abd42ab..97e89ed6 100644--- a/ds-bot/src/client/i18n.js+++ b/ds-bot/src/client/i18n.js@@ -1096,6 +1096,7 @@ const ZH = {'Every weekday at {time}': '每个工作日 {time}','Every weekend at {time}': '每个周末 {time}','On the schedule {expression}': '按计划 {expression}',+ 'Custom schedule': '自定义计划','Next: {time}': '下次 {time}','Overdue: {time}': '已逾期 {time}',Finished: '已结束',diff --git a/ds-bot/src/client/schedules.js b/ds-bot/src/client/schedules.jsindex 1e5dcc7e..f0850f3b 100644--- a/ds-bot/src/client/schedules.js+++ b/ds-bot/src/client/schedules.js@@ -66,11 +66,13 @@ export function timingText(task) {return `${text}${zone}`}case 'cron': return `${t('On the schedule {expression}', { expression: task.expression })}${zone}`+ case 'rrule': return t('Custom schedule')default: return ''}}const nextText = (task) => {if (task.state === 'overdue' || (task.state === undefined && task.status === 'active' && Date.parse(task.scheduledAt) <= Date.now())) return t('Overdue: {time}', { time: dayAndTime(Date.parse(task.scheduledAt)) })+ if (task.status === 'paused') return t('Paused')return task.status === 'active' ? t('Next: {time}', { time: dayAndTime(Date.parse(task.scheduledAt)) }) : t('Finished')}diff --git a/ds-bot/src/host/schedules.js b/ds-bot/src/host/schedules.jsindex d42dd38f..533b1924 100644--- a/ds-bot/src/host/schedules.js+++ b/ds-bot/src/host/schedules.js@@ -39,6 +39,17 @@ function requestOf(record) {return null}+// DSH 0.2.2 keeps tasks Host-wide, each with a delivery target, and changes them through+// edit(); DSH 0.2.1 keeps them by Session. Both are served.+const hostWide = service => typeof service.edit === 'function'+// The existing Session a task delivers to; a task that opens new Sessions has none.+const sessionOf = entry => entry.target === undefined ? entry.sessionId : entry.target.kind === 'session' ? entry.target.sessionId : undefined+const TIMING_KINDS = { at: 'at', every_seconds: 'every', daily: 'daily', weekly: 'weekly' }+const timingOf = (selector) => {+ const [key] = Object.keys(selector)+ return { kind: TIMING_KINDS[key], [key]: selector[key] }+}+// The settings form's timing as a create request selector.function selectorOf(when) {const zone = typeof when?.timeZone === 'string' && when.timeZone !== '' ? when.timeZone : 'UTC'@@ -96,13 +107,15 @@ export function install(rt) {const now = Date.now()const tasks = []for (const entry of await service.catalog()) {- const owner = ownerOf(entry.sessionId)+ const sessionId = sessionOf(entry)+ const owner = sessionId === undefined ? undefined : ownerOf(sessionId)if (!known(owner)) continue// The state DSH derives for its own view: a task past its next firing reads- // overdue; an inactive one is finished. DSH 0.2.1's catalog() carries the- // status but not this state, so it is derived the same way here.- const state = entry.status === 'active' ? (now >= Date.parse(entry.scheduledAt) ? 'overdue' : 'scheduled') : 'finished'- tasks.push({ ...entry, ...owner, state })+ // overdue; a paused one stays paused; any other is finished. catalog() carries+ // the status but not this state, so it is derived the same way here.+ const state = entry.status === 'active' ? (now >= Date.parse(entry.scheduledAt) ? 'overdue' : 'scheduled')+ : entry.status === 'paused' ? 'paused' : 'finished'+ tasks.push({ ...entry, sessionId, ...owner, state })}return { available: true, tasks }}@@ -119,13 +132,23 @@ export function install(rt) {if (title === '') throw invalid('Give the task a name')if (title.length > MAX_TITLE) throw invalid(`Keep the name under ${MAX_TITLE} characters`)if (prompt === '') throw invalid('Say what the Bot should do')- await service.create(bot ? rt.chatOf(bot.id) : rt.sessionOfRoom(room), { title, prompt, ...selectorOf(body.when) })+ const sessionId = bot ? rt.chatOf(bot.id) : rt.sessionOfRoom(room)+ const selector = selectorOf(body.when)+ if (hostWide(service)) await service.create({ title, prompt, timing: timingOf(selector), target: { kind: 'session', sessionId } })+ else await service.create(sessionId, { title, prompt, ...selector })return schedulesView()},async 'schedule-delete'(body) {if (service === null) throw invalid('Automation tasks are off in DSH')if (ownerOf(body.sessionId) === undefined) throw invalid('No such task')- await service.delete({ sessionId: body.sessionId, id: body.id })+ if (hostWide(service)) {+ // Ids are Host-wide here, so the task must still deliver to the team Session named.+ const entry = (await service.catalog()).find(task => task.id === body.id)+ if (entry === undefined || sessionOf(entry) !== body.sessionId) throw invalid('No such task')+ await service.edit({ id: body.id, action: 'delete' })+ } else {+ await service.delete({ sessionId: body.sessionId, id: body.id })+ }return schedulesView()},}@@ -134,6 +157,15 @@ 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+ // A Host-wide task changes its target in place and keeps its next firing; a+ // paused one moves too, so it resumes in the Bot's current part.+ if (hostWide(service)) {+ for (const entry of await service.catalog()) {+ if (entry.status === 'ended' || sessionOf(entry) !== fromId) continue+ await service.edit({ id: entry.id, action: 'update', patch: { target: { kind: 'session', sessionId: toId } } })+ }+ return+ }const records = await service.list({ sessionId: fromId })for (const record of records) {const request = requestOf(record)diff --git a/ds-bot/src/host/tools.js b/ds-bot/src/host/tools.jsindex 5f1804a4..0ef28280 100644--- a/ds-bot/src/host/tools.js+++ b/ds-bot/src/host/tools.js@@ -26,6 +26,10 @@ const HIDDEN_TOOLS = HIDDEN_TOOL_GROUPS.flat()// The guidance DSH writes into the prompt for the hidden tools.const HIDDEN_SECTIONS = new Set([...HIDDEN_TOOLS.map(name => `tool:${name}`), 'tool:goal'])const GOAL_REFUSAL = 'Bot chats do not run /goal. To give a group a goal, use Set a goal in its + menu.'+// DSH 0.2.2's schedule tools can deliver to a new Session, which is outside the team and+// has no Bot; a Bot's tasks deliver to its own chat, which DS Bot moves on with each part.+const SCHEDULE_TARGET_TOOLS = ['schedule_create', 'schedule_edit']+const SCHEDULE_TARGET_REFUSAL = 'A Bot’s scheduled tasks run in its own chat. Use target "this_session".'const DELEGATION_REFUSAL = 'Bots do not start subagents or workflows. Do the work yourself, or hand it to another Bot with message_bot.'const MODE_PARAM = {type: 'string',@@ -913,6 +917,8 @@ export function install(rt) {ctx.tools.guard((exec) => {if (!rt.isBotSession(exec.agent?.id)) return undefinedif (exec.name === BLOCKING_QUESTION_TOOL) return `Bots cannot use ${BLOCKING_QUESTION_TOOL}. Ask with ask_user in your own chat, or ask in your reply.`+ const target = exec.arguments?.target+ if (SCHEDULE_TARGET_TOOLS.includes(exec.name) && target !== undefined && target !== 'this_session') return SCHEDULE_TARGET_REFUSALreturn DELEGATION_TOOLS.includes(exec.name) ? DELEGATION_REFUSAL : undefined})// Creation waits for this listener, so the Agent's first step already lacks the tool.diff --git a/ds-bot/test/blocking-question.test.js b/ds-bot/test/blocking-question.test.jsindex 7c84f2c2..fdcd0403 100644--- a/ds-bot/test/blocking-question.test.js+++ b/ds-bot/test/blocking-question.test.js@@ -288,6 +288,28 @@ test('Bot Sessions filter the hidden tools and their guidance out of the promptassert.deepEqual(filtered.tools.map(tool => tool.name), ['read', 'message_bot'])assert.deepEqual(filtered.contexts, [{ name: 'x' }])assert.deepEqual(filtered.variables, { a: 1 })+ // Fei2411/DS-Bot#814: DSH 0.2.2 adds the tool list after this waterfall, so the+ // assembly it passes has none; the guidance is still filtered and no tools appear.+ const current = await filters[0].listener({}, {}, async () => ({+ sections: [{ name: 'bot-identity' }, { name: 'tool:subagent' }],+ contexts: [],+ variables: {},+ }))+ assert.deepEqual(current, { sections: [{ name: 'bot-identity' }], contexts: [], variables: {} })await stop()for (const record of scopedListeners) assert.equal(record.disposed, true)})++// DSH 0.2.2's schedule tools can deliver to a new Session, outside the team.+test('a Bot schedules only into its own chat', async (t) => {+ const { ctx, stop } = await setup()+ t.after(stop)+ const guard = (name, sessionId, args) => ctx.guards.map(check => check({ name, arguments: args, agent: { id: sessionId } })).find(reason => reason !== undefined)+ for (const target of ['new_once', 'new']) {+ assert.match(guard('schedule_create', 'writer', { target }), /this_session/)+ assert.match(guard('schedule_edit', 'room-writer', { id: 's1', action: 'update', target }), /this_session/)+ assert.equal(guard('schedule_create', 'plain', { target }), undefined)+ }+ assert.equal(guard('schedule_create', 'writer', { target: 'this_session' }), undefined)+ assert.equal(guard('schedule_edit', 'writer', { id: 's1', action: 'delete' }), undefined)+})diff --git a/ds-bot/test/schedules.test.js b/ds-bot/test/schedules.test.jsindex 488297fc..23d46405 100644--- a/ds-bot/test/schedules.test.js+++ b/ds-bot/test/schedules.test.js@@ -57,6 +57,32 @@ function scheduleService() {}}+// The Host-wide service of DSH 0.2.2: each task has a delivery target and changes through edit().+function hostSchedule() {+ const tasks = new Map()+ let next = 1+ const edits = []+ return {+ tasks,+ edits,+ async create({ title, prompt, timing, target }) {+ const id = `schedule-${next++}`+ const [selector] = Object.keys(timing).filter(key => key !== 'kind')+ tasks.set(id, { record: { id, kind: timing.kind, title, prompt, scheduledAt: timing.at ?? '2030-01-01T01:00:00.000Z' }, timing: timing[selector], target, status: 'active' })+ return { id }+ },+ async catalog() {+ return [...tasks.values()].map(task => ({ ...task.record, target: task.target, status: task.status }))+ },+ async edit(request) {+ edits.push(request)+ if (request.action === 'delete') tasks.delete(request.id)+ else Object.assign(tasks.get(request.id), request.patch)+ return { id: request.id }+ },+ }+}+function setup(services = {}) {const home = mkdtempSync(join(tmpdir(), 'dsb-schedules-'))writeFileSync(join(home, 'state.json'), JSON.stringify(TEAM))@@ -139,6 +165,40 @@ test('a due reminder reaches a group as its prompts', () => {assert.equal(reminderText('plain text'), 'plain text')})+// Fei2411/DS-Bot#814: DSH 0.2.2 keeps tasks Host-wide with a delivery target.+test('on DSH 0.2.2 tasks target the Bot’s current part, list by target, delete by id and move in place', async (t) => {+ const schedule = hostSchedule()+ const { ctx, home, call, rt } = setup({ schedule })+ t.after(async () => { await ctx.dispose(); rmSync(home, { recursive: true, force: true, maxRetries: 5 }) })+ const made = await call('schedule-create', { target: 'w1', title: 'Briefing', prompt: 'Sum up the day', when: { kind: 'daily', time: '09:00', timeZone: 'Asia/Shanghai' } })+ assert.equal(made.ok, true, JSON.stringify(made))+ const [created] = [...schedule.tasks.values()]+ assert.deepEqual(created.target, { kind: 'session', sessionId: 'w2' })+ assert.equal(created.record.kind, 'daily')+ assert.deepEqual(created.timing, { time: '09:00:00', time_zone: 'Asia/Shanghai' })+ await call('schedule-create', { target: 'm1', title: 'Ping', prompt: 'Check', when: { kind: 'every', minutes: 30 } })+ assert.deepEqual([...schedule.tasks.values()].at(-1).timing, 1800)++ schedule.tasks.set('schedule-paused', { record: { id: 'schedule-paused', kind: 'rrule', title: 'Held', prompt: 'x', scheduledAt: '2030-01-01T00:00:00.000Z' }, target: { kind: 'session', sessionId: 'w2' }, status: 'paused' })+ schedule.tasks.set('schedule-ended', { record: { id: 'schedule-ended', kind: 'at', title: 'Done', prompt: 'x', scheduledAt: '2020-01-01T00:00:00.000Z' }, target: { kind: 'session', sessionId: 'w2' }, status: 'ended' })+ schedule.tasks.set('schedule-new', { record: { id: 'schedule-new', kind: 'at', title: 'Elsewhere', prompt: 'x', scheduledAt: '2030-01-01T00:00:00.000Z' }, target: { kind: 'new_once' }, status: 'active' })+ const listed = (await call('schedules', {})).value+ assert.deepEqual(listed.tasks.map(task => [task.title, task.botId, task.sessionId, task.state]), [+ ['Briefing', 'w1', 'w2', 'scheduled'], ['Ping', 'm1', 'm1', 'scheduled'], ['Held', 'w1', 'w2', 'paused'], ['Done', 'w1', 'w2', 'finished'],+ ])++ await rt.moveSchedules('w2', 's9')+ assert.deepEqual(schedule.edits.map(edit => [edit.id, edit.patch?.target?.sessionId]), [['schedule-1', 's9'], ['schedule-paused', 's9']])+ assert.deepEqual(schedule.tasks.get('schedule-ended').target, { kind: 'session', sessionId: 'w2' })++ assert.match((await call('schedule-delete', { sessionId: 'w2', id: 'schedule-1' })).error.message, /No such task/)+ assert.match((await call('schedule-delete', { sessionId: 'm1', id: 'schedule-new' })).error.message, /No such task/)+ const removed = await call('schedule-delete', { sessionId: 'm1', id: 'schedule-2' })+ assert.equal(removed.ok, true, JSON.stringify(removed))+ assert.equal(schedule.tasks.has('schedule-2'), false)+ assert.deepEqual(schedule.edits.at(-1), { id: 'schedule-2', action: 'delete' })+})+test('a failed task move can resume without losing identical remaining tasks', async (t) => {const schedule = scheduleService()const { ctx, home, rt } = setup({ schedule })