From 73bc2cd0e5aab2f51a8b4ae8d839cebe66b12e18 Mon Sep 17 00:00:00 2001 From: Day Dreamer <87640074+HugoXK@users.noreply.github.com> Date: Thu, 19 Nov 2026 00:55:15 +0800 Subject: [PATCH] Sort tasks with no next run after every live one `bot_schedules` sorted its rows on the instant alone, so a paused task (no instant at all) and a finished one-shot (a past instant, #2767) sat above the tasks still due. A live row now comes first, and each group keeps instant order inside itself. --- changelog.d/3174.fixed.md | 1 + core/src/host/schedules.js | 6 ++++- core/test/schedules.test.js | 24 ++++++++++++++++++++ docs/subsystems/core-host.md | 4 ++-- patches/ds-bot/0001-core-fixes.patch | 34 +++++++++++++++++++++++++--- 5 files changed, 63 insertions(+), 6 deletions(-) create mode 100644 changelog.d/3174.fixed.md diff --git a/changelog.d/3174.fixed.md b/changelog.d/3174.fixed.md new file mode 100644 index 000000000..83077ba8a --- /dev/null +++ b/changelog.d/3174.fixed.md @@ -0,0 +1 @@ +`bot_schedules` now lists a Bot's live scheduled tasks before the ones with no next run, so a paused task or a finished one-shot no longer sits at the top of the list ([#3174](https://github.com/ThinkFlowLab/FlyBot/issues/3174)). diff --git a/core/src/host/schedules.js b/core/src/host/schedules.js index 17bb65069..f47db74c9 100644 --- a/core/src/host/schedules.js +++ b/core/src/host/schedules.js @@ -696,13 +696,17 @@ export function install(rt) { if (at?.botId !== botId) return rows.push({ at: when ?? '', + live: status === 'active' && when !== undefined, line: `- "${entry.title ?? 'untitled'}" (${entry.id}) — ${status === 'active' && when !== undefined ? `next ${when}` : 'no next run'}, ${status}, ${at.place}, session ${entry.sessionId}`, }) } for (const entry of await service.catalog()) add(entry, entry.status ?? 'active', entry.scheduledAt) for (const entry of Object.values(paused())) add({ ...entry.record, id: entry.id, sessionId: entry.sessionId }, 'paused', undefined) if (rows.length === 0) return `${head}\nnone — no scheduled task in any of its Sessions, so this is the whole list for the Bot.` - rows.sort((a, b) => a.at.localeCompare(b.at) || a.line.localeCompare(b.line)) + // A live row comes first, then the rows with no next run (#3174): sorting on the instant + // alone put every dead row above the tasks still due, because a paused row has none and a + // finished one-shot keeps a past one (#2767). Each group keeps instant order inside itself. + rows.sort((a, b) => (a.live === b.live ? 0 : a.live ? -1 : 1) || a.at.localeCompare(b.at) || a.line.localeCompare(b.line)) return [head, ...rows.map(row => row.line)].join('\n') }, })) diff --git a/core/test/schedules.test.js b/core/test/schedules.test.js index eba6a0bdb..cf2986c26 100644 --- a/core/test/schedules.test.js +++ b/core/test/schedules.test.js @@ -816,6 +816,30 @@ test('#2767: an inactive task prints no next run, and an active one keeps its li assert.ok(answer.includes('- "Live brief" (s-live) — next 2030-01-01T01:00:00.000Z, active, own chat, session w2'), answer) }) +// #3174: a row with no next run is not what the reader opens this list for. The sort key was the +// instant, and a dead row has none (paused) or keeps a past one (a finished one-shot, #2767), so +// both landed above every live task. A live row comes first; the dead ones keep their own order +// after it, because they are still the Bot's tasks and still have to be listed. +test('#3174: a row with no next run sorts after every live row', async (t) => { + const schedule = scheduleService() + const team = JSON.parse(JSON.stringify(TEAM)) + team.pausedSchedules = { 's-paused': { id: 's-paused', sessionId: 'w2', record: { title: 'Paused brief', prompt: 'Sum up' }, pausedAt: 6 } } + const { ctx, home } = setup({ schedule }, team) + t.after(async () => { await ctx.dispose(); rmSync(home, { recursive: true, force: true, maxRetries: 5 }) }) + const task = (id, title, status, scheduledAt) => schedule.tasks.set(id, { sessionId: 'w2', status, record: { id, title, prompt: `${title} prompt`, scheduledAt } }) + task('s-done', 'Finished one-shot', 'inactive', '2026-10-29T07:20:46.458Z') + task('s-live', 'Live brief', 'active', '2030-01-01T01:00:00.000Z') + const answer = await ctx.registeredTools.get('bot_schedules').execute({}, { agent: { id: 'w2' } }) + const at = name => answer.indexOf(`- "${name}"`) + assert.ok(at('Live brief') >= 0 && at('Finished one-shot') >= 0 && at('Paused brief') >= 0, answer) + assert.ok(at('Live brief') < at('Finished one-shot'), `a live row comes before a finished one:\n${answer}`) + assert.ok(at('Live brief') < at('Paused brief'), `a live row comes before a paused one:\n${answer}`) + // The field each line carries is unchanged: only the order moves. + assert.ok(answer.includes('- "Live brief" (s-live) — next 2030-01-01T01:00:00.000Z, active, own chat, session w2'), answer) + assert.ok(answer.includes('- "Finished one-shot" (s-done) — no next run, inactive, own chat, session w2'), answer) + assert.ok(answer.includes('- "Paused brief" (s-paused) — no next run, paused, own chat, session w2'), answer) +}) + // #1321: two owners on one host. `copyFrom` and `schedule-update` resolve a source Session the // way the page does, and the host knowing that Session is not the caller being allowed to read // its task: a copy or an edit may only touch a task of the caller's own user. diff --git a/docs/subsystems/core-host.md b/docs/subsystems/core-host.md index dc5502175..ff8a74884 100644 --- a/docs/subsystems/core-host.md +++ b/docs/subsystems/core-host.md @@ -582,9 +582,9 @@ DSH 0.2 offers the schedule service only through its optional bundle `@deepseek- DSH's own `schedule_list` answers for the Session the call runs in, and its `[]` is indistinguishable from "this Bot has nothing scheduled": a task the owner sees in the UI is invisible from another Session of the same Bot. On 2026-10-21 that produced a written claim that a live schedule had never existed, and it came close to a Bot "cleaning up" a timer it could not see. -- **`bot_schedules`** (no arguments) answers for the calling Bot. It reads the host-wide `catalog()` and keeps the Sessions that speak for it: its own chat (`chatOwners`), the archived parts of it (`pastOwners` with no `roomId`), its group Sessions, live and archived (`groupOwners` / `pastOwners` with a `roomId`, named by the group chat), and a **group chat's own Session** — the group's tasks live on the room's Session id, kept when the calling Bot is in `room.members`, so every member (the admin too) sees them and a Bot outside the group does not. Its paused tasks come from `state.pausedSchedules`, including those on the group's own Session. Each line carries the id, the title, the next run, the state and the owning Session; the answer says in so many words that it is Bot-wide and that `schedule_list` is per Session, so an empty answer cannot be read as a claim about another Session. Only an `active` row has a next run: a row with any other status prints `no next run` even though DSH keeps the instant it was once due there, because a finished one-shot otherwise advertised a moment that had already passed while the settings page on the same data said `Finished` (#2767); the kept instant still sorts the row, so the order does not move. +- **`bot_schedules`** (no arguments) answers for the calling Bot. It reads the host-wide `catalog()` and keeps the Sessions that speak for it: its own chat (`chatOwners`), the archived parts of it (`pastOwners` with no `roomId`), its group Sessions, live and archived (`groupOwners` / `pastOwners` with a `roomId`, named by the group chat), and a **group chat's own Session** — the group's tasks live on the room's Session id, kept when the calling Bot is in `room.members`, so every member (the admin too) sees them and a Bot outside the group does not. Its paused tasks come from `state.pausedSchedules`, including those on the group's own Session. Each line carries the id, the title, the next run, the state and the owning Session; the answer says in so many words that it is Bot-wide and that `schedule_list` is per Session, so an empty answer cannot be read as a claim about another Session. Only an `active` row has a next run: a row with any other status prints `no next run` even though DSH keeps the instant it was once due there, because a finished one-shot otherwise advertised a moment that had already passed while the settings page on the same data said `Finished` (#2767). A live row sorts first and a row with no next run comes after every live one, so a paused task or a finished one-shot cannot sit above the tasks still due; inside each group the stored instants order the rows. - **Read-only, and it changes nothing about the scoping of the rest.** `schedule_create` / `schedule_update` / `schedule_delete` and DSH's own tools keep their session rule; the tool adds a way to *see* the Bot's tasks, not a way to edit them without knowing the Session. -- Tests: `core/test/schedules.test.js` (`#628: bot_schedules answers for the Bot…`, the group-chat-own-Session cases, the service-off and empty-list cases, and the inactive-versus-active next-run case of #2767). +- Tests: `core/test/schedules.test.js` (`#628: bot_schedules answers for the Bot…`, the group-chat-own-Session cases, the service-off and empty-list cases, the inactive-versus-active next-run case of #2767, and the live-rows-first ordering case). ### Scheduled tasks: edit, pause, missed runs (#354) diff --git a/patches/ds-bot/0001-core-fixes.patch b/patches/ds-bot/0001-core-fixes.patch index 8d23a27b2..a7ca95bde 100644 --- a/patches/ds-bot/0001-core-fixes.patch +++ b/patches/ds-bot/0001-core-fixes.patch @@ -8309,7 +8309,7 @@ diff --git a/src/host/schedules.js b/src/host/schedules.js } if (typeof ctx.inject === 'function') ctx.inject(['schedule'], attach) else if (ctx.get?.('schedule') !== undefined) attach({ schedule: ctx.get('schedule'), effect: (execute, label) => ctx.effect(execute, label) }) -@@ -89,63 +89,630 @@ export function install(rt) { +@@ -89,63 +89,634 @@ export function install(rt) { return rt.groupOwners.get(sessionId) ?? rt.pastOwners.get(sessionId) } const known = owner => owner !== undefined && (owner.botId === undefined ? state.rooms[owner.roomId] !== undefined : state.bots[owner.botId] !== undefined) @@ -8835,13 +8835,17 @@ diff --git a/src/host/schedules.js b/src/host/schedules.js + if (at?.botId !== botId) return + rows.push({ + at: when ?? '', ++ live: status === 'active' && when !== undefined, + line: `- "${entry.title ?? 'untitled'}" (${entry.id}) — ${status === 'active' && when !== undefined ? `next ${when}` : 'no next run'}, ${status}, ${at.place}, session ${entry.sessionId}`, + }) + } + for (const entry of await service.catalog()) add(entry, entry.status ?? 'active', entry.scheduledAt) + for (const entry of Object.values(paused())) add({ ...entry.record, id: entry.id, sessionId: entry.sessionId }, 'paused', undefined) + if (rows.length === 0) return `${head}\nnone — no scheduled task in any of its Sessions, so this is the whole list for the Bot.` -+ rows.sort((a, b) => a.at.localeCompare(b.at) || a.line.localeCompare(b.line)) ++ // A live row comes first, then the rows with no next run (#3174): sorting on the instant ++ // alone put every dead row above the tasks still due, because a paused row has none and a ++ // finished one-shot keeps a past one (#2767). Each group keeps instant order inside itself. ++ rows.sort((a, b) => (a.live === b.live ? 0 : a.live ? -1 : 1) || a.at.localeCompare(b.at) || a.line.localeCompare(b.line)) + return [head, ...rows.map(row => row.line)].join('\n') + }, + })) @@ -15841,7 +15845,7 @@ diff --git a/test/schedules.test.js b/test/schedules.test.js test('a group chat task lives on the group Session and lists under the group', async (t) => { const schedule = scheduleService() const { ctx, home, call } = setup({ schedule }) -@@ -121,3 +121,961 @@ test('a due reminder reaches a group as its prompts', () => { +@@ -121,3 +121,985 @@ 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') }) @@ -16449,6 +16453,30 @@ diff --git a/test/schedules.test.js b/test/schedules.test.js + assert.ok(answer.includes('- "Live brief" (s-live) — next 2030-01-01T01:00:00.000Z, active, own chat, session w2'), answer) +}) + ++// #3174: a row with no next run is not what the reader opens this list for. The sort key was the ++// instant, and a dead row has none (paused) or keeps a past one (a finished one-shot, #2767), so ++// both landed above every live task. A live row comes first; the dead ones keep their own order ++// after it, because they are still the Bot's tasks and still have to be listed. ++test('#3174: a row with no next run sorts after every live row', async (t) => { ++ const schedule = scheduleService() ++ const team = JSON.parse(JSON.stringify(TEAM)) ++ team.pausedSchedules = { 's-paused': { id: 's-paused', sessionId: 'w2', record: { title: 'Paused brief', prompt: 'Sum up' }, pausedAt: 6 } } ++ const { ctx, home } = setup({ schedule }, team) ++ t.after(async () => { await ctx.dispose(); rmSync(home, { recursive: true, force: true, maxRetries: 5 }) }) ++ const task = (id, title, status, scheduledAt) => schedule.tasks.set(id, { sessionId: 'w2', status, record: { id, title, prompt: `${title} prompt`, scheduledAt } }) ++ task('s-done', 'Finished one-shot', 'inactive', '2026-10-29T07:20:46.458Z') ++ task('s-live', 'Live brief', 'active', '2030-01-01T01:00:00.000Z') ++ const answer = await ctx.registeredTools.get('bot_schedules').execute({}, { agent: { id: 'w2' } }) ++ const at = name => answer.indexOf(`- "${name}"`) ++ assert.ok(at('Live brief') >= 0 && at('Finished one-shot') >= 0 && at('Paused brief') >= 0, answer) ++ assert.ok(at('Live brief') < at('Finished one-shot'), `a live row comes before a finished one:\n${answer}`) ++ assert.ok(at('Live brief') < at('Paused brief'), `a live row comes before a paused one:\n${answer}`) ++ // The field each line carries is unchanged: only the order moves. ++ assert.ok(answer.includes('- "Live brief" (s-live) — next 2030-01-01T01:00:00.000Z, active, own chat, session w2'), answer) ++ assert.ok(answer.includes('- "Finished one-shot" (s-done) — no next run, inactive, own chat, session w2'), answer) ++ assert.ok(answer.includes('- "Paused brief" (s-paused) — no next run, paused, own chat, session w2'), answer) ++}) ++ +// #1321: two owners on one host. `copyFrom` and `schedule-update` resolve a source Session the +// way the page does, and the host knowing that Session is not the caller being allowed to read +// its task: a copy or an edit may only touch a task of the caller's own user.