|
| 1 | +// #2988: a card reaches its finished state by more than one door, and two of them disagreed about |
| 2 | +// the number it draws. A board move to Done sets `progress` to 100 when the request names none |
| 3 | +// (`tasks.js` `task-update` / `task-create` / `track_tasks`), and the overall card is set to 100 by |
| 4 | +// `settleOverall`, but accepting a result in the Inbox only transitioned the status: the same |
| 5 | +// `result-ready` card stored at 60 read 60% after Accept and 100% after a board drag, and an |
| 6 | +// accepted card cannot be corrected afterwards. These cases pin the number each door leaves. |
| 7 | +import assert from 'node:assert/strict' |
| 8 | +import { mkdtempSync, rmSync, writeFileSync } from 'node:fs' |
| 9 | +import { tmpdir } from 'node:os' |
| 10 | +import { join } from 'node:path' |
| 11 | +import { test } from 'node:test' |
| 12 | +import { createRuntime } from '../index.js' |
| 13 | +import { progressOf } from '../src/shared/task-board.js' |
| 14 | +import { fakeContext } from './fake-ctx.js' |
| 15 | + |
| 16 | +const team = () => ({ |
| 17 | + version: 2, |
| 18 | + revision: 1, |
| 19 | + mainBotIds: ['a'], |
| 20 | + members: { |
| 21 | + 'owner-1': { id: 'owner-1', createdAt: 1, designatedChiefId: 'a' }, |
| 22 | + }, |
| 23 | + bots: { |
| 24 | + a: { id: 'a', name: 'Chief', role: 'Lead', sessionId: 'a', createdAt: 1, ownerId: 'owner-1' }, |
| 25 | + d1: { id: 'd1', name: 'Dev1', role: 'Dev', sessionId: 'd1', createdAt: 2, ownerId: 'owner-1', createdBy: 'a' }, |
| 26 | + }, |
| 27 | + rooms: { |
| 28 | + war: { |
| 29 | + id: 'war', name: 'War', members: ['a', 'd1'], admin: 'a', mode: 'everyone', named: true, sessions: {}, log: [], seq: 0, seen: {}, |
| 30 | + participants: [ |
| 31 | + { kind: 'human', id: 'owner-1', role: 'admin', displayName: 'Alice', joinedSeq: 0, joinedAt: 1 }, |
| 32 | + { kind: 'bot', id: 'a', role: 'admin', displayName: 'Chief', joinedSeq: 0, joinedAt: 1 }, |
| 33 | + { kind: 'bot', id: 'd1', role: 'member', displayName: 'Dev1', joinedSeq: 0, joinedAt: 1 }, |
| 34 | + ], |
| 35 | + }, |
| 36 | + }, |
| 37 | + grants: {}, |
| 38 | + agents: {}, exchanges: [], exchangeTurns: {}, questions: {}, prefs: {}, |
| 39 | +}) |
| 40 | + |
| 41 | +async function start(t) { |
| 42 | + const home = mkdtempSync(join(tmpdir(), 'ds-bot-accept-progress-')) |
| 43 | + writeFileSync(join(home, 'state.json'), JSON.stringify(team())) |
| 44 | + const ctx = fakeContext() |
| 45 | + const agents = new Map() |
| 46 | + const agentOf = (sessionId) => { |
| 47 | + if (!agents.has(sessionId)) { |
| 48 | + agents.set(sessionId, { |
| 49 | + session: { id: sessionId }, id: sessionId, calls: [], status: 'idle', |
| 50 | + followup() {}, steer() {}, inject() {}, cancel() {}, async whenIdle() {}, inbox: { nextStep: [], nextTurn: [], remove() {} }, |
| 51 | + }) |
| 52 | + } |
| 53 | + return agents.get(sessionId) |
| 54 | + } |
| 55 | + Object.assign(ctx, { |
| 56 | + llm: { listProviders: () => [], listModels: async () => [], resolveModelInfo: async () => ({}) }, |
| 57 | + workspaceRegistry: { get: () => undefined, resolveByPath: async () => undefined, create: async path => ({ id: 'w1', path }), archiveSession: async () => {} }, |
| 58 | + sessionController: { create: async () => ({ sessionId: 'made-1' }), resolveAgent: async id => ({ agent: agentOf(id) }), selectModel: async () => {}, page: async () => ({ records: [], hasMore: false }) }, |
| 59 | + sessionTitle: { rename: async () => {} }, |
| 60 | + sessions: { flush: async () => {} }, |
| 61 | + }) |
| 62 | + const rt = createRuntime(ctx, { home }) |
| 63 | + await rt.load() |
| 64 | + t.after(async () => { |
| 65 | + try { await ctx.dispose() } catch {} |
| 66 | + rmSync(home, { recursive: true, force: true, maxRetries: 5 }) |
| 67 | + }) |
| 68 | + return { rt } |
| 69 | +} |
| 70 | + |
| 71 | +// A card stored at a partial number, ready for the requester to accept the Bot's result. |
| 72 | +async function resultReady(rt, progress = 60) { |
| 73 | + const card = await rt.taskEndpoint('task-create', { botId: 'd1', title: 'Ship the fix', criteria: 'the number matches' }, null) |
| 74 | + await rt.taskEndpoint('task-update', { id: card.id, progress }, null) |
| 75 | + await rt.taskEndpoint('task-accept', { id: card.id }, null) |
| 76 | + await rt.taskEndpoint('task-start', { id: card.id }, null) |
| 77 | + await rt.taskEndpoint('task-return', { id: card.id, summary: 'done' }, null) |
| 78 | + assert.equal(rt.state.tasks[card.id].status, 'result-ready') |
| 79 | + assert.equal(rt.state.tasks[card.id].progress, progress) |
| 80 | + return card.id |
| 81 | +} |
| 82 | + |
| 83 | +test('the same finished card draws 100% from the board and 60% from the Inbox (#2988)', async (t) => { |
| 84 | + const { rt } = await start(t) |
| 85 | + const board = await resultReady(rt) |
| 86 | + const inbox = await resultReady(rt) |
| 87 | + await rt.taskEndpoint('task-update', { id: board, column: 'done' }, null) |
| 88 | + await rt.taskEndpoint('task-accept-result', { id: inbox }, null) |
| 89 | + assert.equal(rt.state.tasks[board].status, 'accepted') |
| 90 | + assert.equal(rt.state.tasks[inbox].status, 'accepted') |
| 91 | + assert.deepEqual( |
| 92 | + { board: rt.state.tasks[board].progress, inbox: rt.state.tasks[inbox].progress }, |
| 93 | + { board: 100, inbox: 100 }, |
| 94 | + 'the same finished card draws 100% from the board and 60% from the Inbox', |
| 95 | + ) |
| 96 | + assert.equal(progressOf(rt.state.tasks[inbox]), 100) |
| 97 | +}) |
| 98 | + |
| 99 | +test('a board move to done on an accepted card keeps reading 100 (#2988)', async (t) => { |
| 100 | + const { rt } = await start(t) |
| 101 | + const id = await resultReady(rt) |
| 102 | + await rt.taskEndpoint('task-accept-result', { id }, null) |
| 103 | + await rt.taskEndpoint('task-update', { id, column: 'done' }, null) |
| 104 | + assert.equal(rt.state.tasks[id].status, 'accepted') |
| 105 | + assert.equal(rt.state.tasks[id].progress, 100) |
| 106 | + assert.equal(progressOf(rt.state.tasks[id]), 100) |
| 107 | +}) |
| 108 | + |
| 109 | +test('a cancelled card keeps the number it last stored (#2988)', async (t) => { |
| 110 | + const { rt } = await start(t) |
| 111 | + const id = await resultReady(rt, 60) |
| 112 | + await rt.taskEndpoint('task-cancel', { id }, null) |
| 113 | + assert.equal(rt.state.tasks[id].status, 'cancelled') |
| 114 | + assert.equal(rt.state.tasks[id].progress, 60) |
| 115 | + // Cancelled is not finished: the drawn number stays the stored one, never the done⇒100 default. |
| 116 | + assert.equal(progressOf(rt.state.tasks[id]), 60) |
| 117 | +}) |
0 commit comments