fix: notify Shangshu when department execution ends
This commit is contained in:
parent
85849f6b58
commit
f569790b76
|
|
@ -336,6 +336,13 @@ function queueDelivery(task, status, now = Date.now()) {
|
||||||
return task.deliveryId
|
return task.deliveryId
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function readyForDepartmentSummary(project, task) {
|
||||||
|
if (task.kind !== 'workflow' || task.ownerSeatId !== 'shangshu' || task.stage !== 'department_exec'
|
||||||
|
|| task.status !== 'running' || task.deliveryState !== 'none') return false
|
||||||
|
const children = project.tasks.filter(child => child.kind === 'execution' && child.parentTaskId === task.id)
|
||||||
|
return children.length > 0 && children.every(child => !OPEN_TASK_STATUS.has(child.status))
|
||||||
|
}
|
||||||
|
|
||||||
// 首发与重投共用同一份正文:任务说明、需求快照、阶段提示和项目记忆。
|
// 首发与重投共用同一份正文:任务说明、需求快照、阶段提示和项目记忆。
|
||||||
function taskDeliveryText(task, memoryText) {
|
function taskDeliveryText(task, memoryText) {
|
||||||
const detail = [
|
const detail = [
|
||||||
|
|
@ -423,7 +430,9 @@ function workflowStageHint(task) {
|
||||||
menxia_review: '用 sansheng_workflow_review 提交审核,批准前确认范围与验收标准齐全。通过后系统交尚书,发现实质问题时说明原因退回中书补齐。',
|
menxia_review: '用 sansheng_workflow_review 提交审核,批准前确认范围与验收标准齐全。通过后系统交尚书,发现实质问题时说明原因退回中书补齐。',
|
||||||
awaiting_execution_approval: '此方案等待用户在「待你处理」中确认。',
|
awaiting_execution_approval: '此方案等待用户在「待你处理」中确认。',
|
||||||
shangshu_dispatch: '门下已通过方案。请使用 sansheng_workflow_dispatch 向六部分派获批任务;不得超出批准范围。',
|
shangshu_dispatch: '门下已通过方案。请使用 sansheng_workflow_dispatch 向六部分派获批任务;不得超出批准范围。',
|
||||||
department_exec: '按获批范围执行。用 sansheng_task_report 提交完成结果或具体失败原因。',
|
department_exec: task.kind === 'workflow'
|
||||||
|
? '六部子任务均已结束。请用 sansheng_task_list 查看本任务的各子任务真实结果和失败原因,再用 sansheng_workflow_summarize 汇总并提交门下验收。'
|
||||||
|
: '按获批范围执行。用 sansheng_task_report 提交完成结果或具体失败原因。',
|
||||||
menxia_acceptance: '请使用 sansheng_workflow_accept 验收;未通过则退回尚书省。',
|
menxia_acceptance: '请使用 sansheng_workflow_accept 验收;未通过则退回尚书省。',
|
||||||
lead_delivery: '请向用户交付真实结果;不得代替部门伪造或改写执行结果。',
|
lead_delivery: '请向用户交付真实结果;不得代替部门伪造或改写执行结果。',
|
||||||
reconfirm: '需求已变更,旧授权失效;请与用户重新商讨。',
|
reconfirm: '需求已变更,旧授权失效;请与用户重新商讨。',
|
||||||
|
|
@ -2256,8 +2265,11 @@ export class SanshengService {
|
||||||
target.sendingOwner = null
|
target.sendingOwner = null
|
||||||
target.deliveryOwner = null
|
target.deliveryOwner = null
|
||||||
if (outcome === 'sent') {
|
if (outcome === 'sent') {
|
||||||
target.status = target.deliveryStatus ?? (target.status === 'draft' ? 'dispatched' : target.status)
|
// 部门可能在发送回写之前就开始执行或提交结果。
|
||||||
target.note = ''
|
if (!['running', 'done', 'failed'].includes(target.reportStatus)) {
|
||||||
|
target.status = target.deliveryStatus ?? (target.status === 'draft' ? 'dispatched' : target.status)
|
||||||
|
target.note = ''
|
||||||
|
}
|
||||||
target.sessionId = seat.sessionId
|
target.sessionId = seat.sessionId
|
||||||
target.dispatchedAt = now
|
target.dispatchedAt = now
|
||||||
target.contextRevision = memory.revision
|
target.contextRevision = memory.revision
|
||||||
|
|
@ -2291,6 +2303,20 @@ export class SanshengService {
|
||||||
// R-002:启动/重连恢复。pending 表示尚未开始发送,可条件领取一次;sending 遗留只标 unknown,绝不自动重发。
|
// R-002:启动/重连恢复。pending 表示尚未开始发送,可条件领取一次;sending 遗留只标 unknown,绝不自动重发。
|
||||||
async recoverPendingDeliveries() {
|
async recoverPendingDeliveries() {
|
||||||
await this.store.ensureLoaded()
|
await this.store.ensureLoaded()
|
||||||
|
// 旧版可能已收齐六部结果,但从未登记尚书通知。
|
||||||
|
if (this.store.list().some(project => project.tasks?.some(task => readyForDepartmentSummary(project, task)))) {
|
||||||
|
await this.store.mutate(s => {
|
||||||
|
for (const project of Object.values(s.projects)) {
|
||||||
|
for (const task of project.tasks ?? []) {
|
||||||
|
if (!readyForDepartmentSummary(project, task)) continue
|
||||||
|
const now = Date.now()
|
||||||
|
queueDelivery(task, task.status, now)
|
||||||
|
task.updatedAt = project.updatedAt = now
|
||||||
|
project.revision = (project.revision ?? 0) + 1
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
const pending = []
|
const pending = []
|
||||||
const sending = []
|
const sending = []
|
||||||
for (const project of this.store.list()) {
|
for (const project of this.store.list()) {
|
||||||
|
|
@ -2368,7 +2394,7 @@ export class SanshengService {
|
||||||
if (!['running', 'done', 'failed'].includes(status)) throw new ServiceError('sansheng/invalid', '报告状态必须是 running、done 或 failed')
|
if (!['running', 'done', 'failed'].includes(status)) throw new ServiceError('sansheng/invalid', '报告状态必须是 running、done 或 failed')
|
||||||
const result = typeof input.result === 'string' ? input.result.trim() : ''
|
const result = typeof input.result === 'string' ? input.result.trim() : ''
|
||||||
if (status === 'done' && !result) throw new ServiceError('sansheng/invalid', '完成报告必须包含真实结果,不能只写“已完成”')
|
if (status === 'done' && !result) throw new ServiceError('sansheng/invalid', '完成报告必须包含真实结果,不能只写“已完成”')
|
||||||
const next = await this.store.mutate(s => {
|
const { task: next, summaryDelivery } = await this.store.mutate(s => {
|
||||||
const project = s.projects[projectId]
|
const project = s.projects[projectId]
|
||||||
const task = project.tasks.find(item => item.id === taskId)
|
const task = project.tasks.find(item => item.id === taskId)
|
||||||
if (!task) throw new ServiceError('sansheng/not-found', `任务 ${taskId} 不存在`)
|
if (!task) throw new ServiceError('sansheng/not-found', `任务 ${taskId} 不存在`)
|
||||||
|
|
@ -2386,7 +2412,10 @@ export class SanshengService {
|
||||||
}
|
}
|
||||||
if (task.status === 'done' && status !== 'done') throw new ServiceError('sansheng/invalid', '已完成的任务不能回退状态')
|
if (task.status === 'done' && status !== 'done') throw new ServiceError('sansheng/invalid', '已完成的任务不能回退状态')
|
||||||
if ((status === 'done' || status === 'failed') && task.status === status && task.reportStatus === status) {
|
if ((status === 'done' || status === 'failed') && task.status === status && task.reportStatus === status) {
|
||||||
return { ...task, late: false, duplicate: true }
|
const parent = project.tasks.find(item => item.id === task.parentTaskId)
|
||||||
|
const summaryDelivery = parent?.stage === 'department_exec' && parent.deliveryState === 'pending'
|
||||||
|
? { taskId: parent.id, deliveryId: parent.deliveryId } : null
|
||||||
|
return { task: { ...task, late: false, duplicate: true }, summaryDelivery }
|
||||||
}
|
}
|
||||||
// 已取消任务的结果迟到:保留取消状态,但把真实结果留档,不假装没发生。
|
// 已取消任务的结果迟到:保留取消状态,但把真实结果留档,不假装没发生。
|
||||||
const late = task.status === 'cancelled'
|
const late = task.status === 'cancelled'
|
||||||
|
|
@ -2405,8 +2434,20 @@ export class SanshengService {
|
||||||
task.updatedAt = Date.now()
|
task.updatedAt = Date.now()
|
||||||
s.projects[projectId].updatedAt = Date.now()
|
s.projects[projectId].updatedAt = Date.now()
|
||||||
s.projects[projectId].revision = (s.projects[projectId].revision ?? 0) + 1
|
s.projects[projectId].revision = (s.projects[projectId].revision ?? 0) + 1
|
||||||
return { ...task, late }
|
const parent = task.kind === 'execution' && !late && status !== 'running'
|
||||||
|
? project.tasks.find(item => item.id === task.parentTaskId) : null
|
||||||
|
const summaryDelivery = parent && readyForDepartmentSummary(project, parent)
|
||||||
|
? { taskId: parent.id, deliveryId: queueDelivery(parent, parent.status, task.updatedAt) } : null
|
||||||
|
if (summaryDelivery) parent.updatedAt = task.updatedAt
|
||||||
|
return { task: { ...task, late }, summaryDelivery }
|
||||||
})
|
})
|
||||||
|
if (summaryDelivery) {
|
||||||
|
try {
|
||||||
|
await this.taskDispatch(projectId, summaryDelivery.taskId, { deliveryId: summaryDelivery.deliveryId })
|
||||||
|
} catch (error) {
|
||||||
|
console.warn('[sansheng] 尚书汇总通知发送失败:', summaryDelivery.taskId, error?.message ?? error)
|
||||||
|
}
|
||||||
|
}
|
||||||
return { task: next, late: Boolean(next.late) }
|
return { task: next, late: Boolean(next.late) }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -74,7 +74,7 @@ async function publishRequirement(env, req) {
|
||||||
}, { seatId: 'lead', sessionId: env.sessions.lead })
|
}, { seatId: 'lead', sessionId: env.sessions.lead })
|
||||||
}
|
}
|
||||||
|
|
||||||
async function drive(env, target) {
|
async function drive(env, target, assignments = [{ seatId: 'gongbu', title: '执行任务', goal: '按范围执行' }]) {
|
||||||
const req = await createRequirement(env)
|
const req = await createRequirement(env)
|
||||||
const published = await publishRequirement(env, req)
|
const published = await publishRequirement(env, req)
|
||||||
await waitTaskDelivery(env.service, env.projectId, published.taskId)
|
await waitTaskDelivery(env.service, env.projectId, published.taskId)
|
||||||
|
|
@ -85,8 +85,9 @@ async function drive(env, target) {
|
||||||
await env.service.workflowReview(env.projectId, taskId, { action: 'approve', note: '方案可执行' }, { seatId: 'menxia', sessionId: env.sessions.menxia })
|
await env.service.workflowReview(env.projectId, taskId, { action: 'approve', note: '方案可执行' }, { seatId: 'menxia', sessionId: env.sessions.menxia })
|
||||||
if (target === 'shangshu_dispatch') return { taskId }
|
if (target === 'shangshu_dispatch') return { taskId }
|
||||||
const dispatched = await env.service.workflowDispatch(env.projectId, taskId, {
|
const dispatched = await env.service.workflowDispatch(env.projectId, taskId, {
|
||||||
assignments: [{ seatId: 'gongbu', title: '执行任务', goal: '按范围执行' }],
|
assignments,
|
||||||
}, { seatId: 'shangshu', sessionId: env.sessions.shangshu })
|
}, { seatId: 'shangshu', sessionId: env.sessions.shangshu })
|
||||||
|
if (target === 'awaiting_reports') return { taskId, children: dispatched.children }
|
||||||
const child = dispatched.children[0]
|
const child = dispatched.children[0]
|
||||||
await env.service.taskReport(env.projectId, child.id, {
|
await env.service.taskReport(env.projectId, child.id, {
|
||||||
status: 'done', result: '真实结果:完成', seatId: 'gongbu', sessionId: env.sessions.gongbu,
|
status: 'done', result: '真实结果:完成', seatId: 'gongbu', sessionId: env.sessions.gongbu,
|
||||||
|
|
@ -98,6 +99,51 @@ async function drive(env, target) {
|
||||||
return { taskId, childId: child.id }
|
return { taskId, childId: child.id }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
test('六部最后一项结束后只通知尚书一次,主任务仍由尚书提交汇总', async () => {
|
||||||
|
const env = await fixture('/summary-notification')
|
||||||
|
const { taskId, children: [first, last] } = await drive(env, 'awaiting_reports', [
|
||||||
|
{ seatId: 'gongbu', title: '实现' },
|
||||||
|
{ seatId: 'gongbu', title: '检查' },
|
||||||
|
])
|
||||||
|
env.fake.calls.followup.length = 0
|
||||||
|
await env.service.taskReport(env.projectId, first.id, { status: 'done', result: '实现完成', seatId: 'gongbu' })
|
||||||
|
assert.equal(env.fake.calls.followup.length, 0)
|
||||||
|
const report = { status: 'failed', note: '检查失败的原因', seatId: 'gongbu' }
|
||||||
|
await Promise.all([
|
||||||
|
env.service.taskReport(env.projectId, last.id, report),
|
||||||
|
env.service.taskReport(env.projectId, last.id, report),
|
||||||
|
])
|
||||||
|
assert.equal(env.fake.calls.followup.length, 1)
|
||||||
|
const [notification] = env.fake.calls.followup
|
||||||
|
assert.equal(notification.sessionId, env.sessions.shangshu)
|
||||||
|
assert.equal(notification.message.source.taskId, taskId)
|
||||||
|
assert.match(notification.message.content[0].text, /sansheng_workflow_summarize/)
|
||||||
|
const task = (await env.service.taskGet(env.projectId, taskId)).task
|
||||||
|
assert.equal(task.stage, 'department_exec')
|
||||||
|
assert.equal(task.status, 'running')
|
||||||
|
assert.equal(task.deliveryState, 'accepted')
|
||||||
|
assert.equal(task.sessionId, env.sessions.shangshu)
|
||||||
|
assert.equal((await env.service.taskGet(env.projectId, last.id)).task.status, 'failed')
|
||||||
|
})
|
||||||
|
|
||||||
|
test('启动时补发旧版漏掉的尚书通知,并在再次恢复时去重', async () => {
|
||||||
|
const env = await fixture('/summary-notification-recovery')
|
||||||
|
const { taskId } = await drive(env, 'department_exec')
|
||||||
|
await env.store.mutate(state => {
|
||||||
|
const task = state.projects[env.projectId].tasks.find(item => item.id === taskId)
|
||||||
|
task.deliveryState = 'none'
|
||||||
|
task.deliveryId = null
|
||||||
|
})
|
||||||
|
env.fake.calls.followup.length = 0
|
||||||
|
const service = new SanshengService({
|
||||||
|
store: new SanshengStore(env.file), host: env.fake.host, provisioner: env.service.provisioner,
|
||||||
|
seatDelivery: createSeatDelivery({ host: env.fake.host }),
|
||||||
|
})
|
||||||
|
assert.equal((await service.recoverPendingDeliveries()).recovered[0]?.delivered, true)
|
||||||
|
assert.equal((await service.recoverPendingDeliveries()).recovered.length, 0)
|
||||||
|
assert.equal(env.fake.calls.followup.length, 1)
|
||||||
|
})
|
||||||
|
|
||||||
test('场景1:领取后 followup 前取消(领取失效),followup 0 次,状态保持 cancelled', async () => {
|
test('场景1:领取后 followup 前取消(领取失效),followup 0 次,状态保持 cancelled', async () => {
|
||||||
const env = await fixture('/fix-scenario1')
|
const env = await fixture('/fix-scenario1')
|
||||||
const task = await createTask(env)
|
const task = await createTask(env)
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue