diff --git a/changelog.d/3114-digest-adopt.fixed.md b/changelog.d/3114-digest-adopt.fixed.md new file mode 100644 index 000000000..5ac1f7c69 --- /dev/null +++ b/changelog.d/3114-digest-adopt.fixed.md @@ -0,0 +1 @@ +A Project whose assistant Session already holds its 09:00 digest now adopts that task instead of writing a second one, and a digest its owner paused stays paused rather than being replaced by an active copy. (#1751) diff --git a/docs/subsystems/project.md b/docs/subsystems/project.md index e2e40c84e..dedf3cea5 100644 --- a/docs/subsystems/project.md +++ b/docs/subsystems/project.md @@ -52,7 +52,7 @@ package owns everything that reads or acts on that record. | `plugins/project/src/project-chief.js` | The Project Chief (#1859): one hidden Bot per Project, named `Project Chief ()`, bound with `bindProject` and given instructions that name that one Project and forbid answering for any other; retired through the host's own `deleteBot` with the Project. `rt.projectChief` is `{ create, chiefOf, remove }`. | | `plugins/project/src/project-team.js` | The team dossier (#1864), read-only: `rt.projectTeam(projectId)` answers `{ rows }` in the board's order — the Chief, the assistant, then `roles.maintainers` / `roles.members` — with persona (the Bot's instructions), the head and topic names of its own memory, its skills and only this Project's rooms. Every string and list is capped and a deleted Bot is a `null` row, never an error for the whole answer. | | `plugins/project/src/project-metrics.js` | The board's seven numbers (#1829) read from GitHub REST and the local task store, with a five-minute cache and one honest failure answer: a missing credential or an unreadable source is `unavailable` and every value reads `null`, never `0`. The lists are followed page by page under a cap (#2650), a foreign host is never followed with the token, and the day boundary is the caller's zone (`resolveTimeZone`) rather than the host's UTC clock. Its day key is `core/src/host/day-key.js` since #2830. | -| `plugins/project/src/project-assistant.js` | The Project Assistant (#1712): one hidden Bot and Session per Project with exactly one 09:00 Asia/Shanghai `project_digest` task on that Session through the host's schedule path (never a cron in the plugin), per-owner memory in `state.projectMemory` behind the store's write door (600 code points), and a digest built only from board rows and Project State (what merged, what closed, what is in progress and on whom, the next slice; a day with no change is one line, a second build answers `skipped`). Its writes carry `Written-by: Project Assistant ()` / `Posted-for: `. | +| `plugins/project/src/project-assistant.js` | The Project Assistant (#1712): one hidden Bot and Session per Project with exactly one 09:00 Asia/Shanghai `project_digest` task on that Session through the host's schedule path (never a cron in the plugin; a task already on that Session with that title and a daily rule — including one its owner paused — is adopted instead of written again, and only an ended row (`inactive`) is replaced, so the stored `scheduleId` always names a task that can still fire, [#1751](https://github.com/ThinkFlowLab/FlyBot/issues/1751)), per-owner memory in `state.projectMemory` behind the store's write door (600 code points), and a digest built only from board rows and Project State (what merged, what closed, what is in progress and on whom, the next slice; a day with no change is one line, a second build answers `skipped`). Its writes carry `Written-by: Project Assistant ()` / `Posted-for: `. | | `plugins/project/client.js` | Empty in this slice: the browser half is still in the core, see **Does not own**. | ## Entry points diff --git a/plugins/project/src/project-assistant.js b/plugins/project/src/project-assistant.js index 628ab1558..6a0b39660 100644 --- a/plugins/project/src/project-assistant.js +++ b/plugins/project/src/project-assistant.js @@ -233,13 +233,24 @@ export function install(rt) { const owner = rt.botOf?.(botId)?.ownerId const actor = owner === undefined ? undefined : { memberId: owner } try { + // #1751: one digest per Session. A task already there is adopted — the title on this Session + // and its daily rule are who it is — so a repeated reconcile, or a Project that lost the id, + // names the task that exists instead of writing a second. A task its owner paused is the + // digest too, and stays paused; only an ended row (`inactive` is all DSH has besides + // `active`) is replaced, or the stored id would name a task that can never fire. + const listed = await rt.scheduleEndpoint('schedules', {}, actor) + const existing = (listed?.tasks ?? []).find(task => task.sessionId === sessionId && task.title === DIGEST_TITLE + && task.kind === 'daily' && task.status !== 'inactive') + if (typeof existing?.id === 'string') return { scheduleId: existing.id } + const view = await rt.scheduleEndpoint('schedule-create', { target: botId, title: DIGEST_TITLE, prompt: DIGEST_PROMPT, when: { kind: 'daily', time: DIGEST_TIME, timeZone: DIGEST_ZONE }, }, actor) - const row = (view?.tasks ?? []).find(task => task.sessionId === sessionId && task.title === DIGEST_TITLE) + const row = (view?.tasks ?? []).find(task => task.sessionId === sessionId && task.title === DIGEST_TITLE + && task.kind === 'daily' && task.status !== 'inactive') return { scheduleId: row?.id ?? null } } catch (error) { return { error: error?.message ?? String(error) } diff --git a/plugins/project/test/project-assistant.test.js b/plugins/project/test/project-assistant.test.js index 364e92754..da9642a04 100644 --- a/plugins/project/test/project-assistant.test.js +++ b/plugins/project/test/project-assistant.test.js @@ -350,6 +350,98 @@ test('project-create retries the digest for an assistant it did not create (#175 } }) +// #1751: the retry must not be a second write. A Project Assistant whose Session already holds +// its 09:00 digest adopts that task; its identity is the Session, the title and a daily rule, so a +// task its owner paused is the digest too, and an ended row is the only one that is replaced. +test('reconcile adopts a 09:00 digest already on the Session and does not write a second (#1751)', async () => { + const { rt, service, attachSchedule, stop } = await start({ schedule: false }) + try { + const project = await created(rt, { name: 'Adopt', ownerUserId: 'owner-1' }) + const sessionId = rt.readProject(project.id).assistant.sessionId + assert.equal(service.tasks.size, 0, 'nothing is written while the service is off') + await service.create(sessionId, { title: '09:00 Project digest', prompt: 'the digest', daily: { time: '09:00', time_zone: 'Asia/Shanghai' } }) + assert.equal(service.created.length, 1, 'the Session already holds one digest') + + attachSchedule() + await rt.projectAssistant.reconcile() + + assert.equal(service.created.length, 1, 'the digest already on the Session is adopted, not written again') + assert.equal(service.tasks.size, 1, 'one 09:00 task on the Session, not two') + const saved = rt.readProject(project.id).assistant + assert.equal(saved.scheduleId, 'schedule-1', 'the Project names the task that was already there') + assert.equal(saved.error, undefined, 'an adopted digest leaves no retry behind') + } finally { + await stop() + } +}) + +test('reconcile adopts a paused digest rather than writing an active second one (#1751)', async () => { + const { rt, service, attachSchedule, stop } = await start({ schedule: false }) + try { + const project = await created(rt, { name: 'Paused', ownerUserId: 'owner-1' }) + const sessionId = rt.readProject(project.id).assistant.sessionId + // DSH's own storage keeps `active | inactive` only. A paused task lives in the host's + // `pausedSchedules` and the view merges it back with `status: 'paused'`, which is the state + // the digest above is in: its owner paused it, so no active copy may take its place. + rt.state.pausedSchedules ??= {} + rt.state.pausedSchedules['schedule-9'] = { + id: 'schedule-9', + sessionId, + record: { title: '09:00 Project digest', prompt: 'the digest', kind: 'daily', time: '09:00', timeZone: 'Asia/Shanghai' }, + pausedAt: Date.now(), + } + + attachSchedule() + await rt.projectAssistant.reconcile() + + assert.equal(service.created.length, 0, 'the owner paused this digest, so no active one replaces it') + assert.equal(service.tasks.size, 0, 'and nothing new is written to DSH') + const saved = rt.readProject(project.id).assistant + assert.equal(saved.scheduleId, 'schedule-9', 'the Project names the paused task its owner can resume') + } finally { + await stop() + } +}) + +test('an ended digest is replaced, and the Project stores the live task rather than the dead one (#1751)', async () => { + const { rt, service, attachSchedule, stop } = await start({ schedule: false }) + try { + const project = await created(rt, { name: 'Ended', ownerUserId: 'owner-1' }) + const sessionId = rt.readProject(project.id).assistant.sessionId + await service.create(sessionId, { title: '09:00 Project digest', prompt: 'the digest', daily: { time: '09:00', time_zone: 'Asia/Shanghai' } }) + // The row DSH leaves behind when a task ends: still in the catalog, with `status: 'inactive'`. + service.tasks.get('schedule-1').status = 'inactive' + + attachSchedule() + await rt.projectAssistant.reconcile() + + assert.equal(service.created.length, 2, 'an ended digest is replaced by a live one') + assert.equal(service.tasks.size, 2, 'the dead row stays where DSH keeps it') + const saved = rt.readProject(project.id).assistant + assert.equal(saved.scheduleId, 'schedule-2', 'the stored id names a task that can still fire') + } finally { + await stop() + } +}) + +test('a weekly task reusing the digest title is not the digest (#1751)', async () => { + const { rt, service, attachSchedule, stop } = await start({ schedule: false }) + try { + const project = await created(rt, { name: 'Weekly', ownerUserId: 'owner-1' }) + const sessionId = rt.readProject(project.id).assistant.sessionId + await service.create(sessionId, { title: '09:00 Project digest', prompt: 'something else', weekly: { time: '09:00', time_zone: 'Asia/Shanghai', weekdays: [1] } }) + + attachSchedule() + await rt.projectAssistant.reconcile() + + assert.equal(service.created.length, 2, 'the daily digest is written beside the weekly task') + const saved = rt.readProject(project.id).assistant + assert.equal(saved.scheduleId, 'schedule-2', 'the adopted task is the daily one, not the weekly') + } finally { + await stop() + } +}) + test('buildDigest is pure: the same rows answer the same four sections', () => { const project = { id: 'project-1', name: 'Pure' } const rows = [