dsh-team-bots 0.2.347 → 0.2.364
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +2 -1
- package/src/services/notify.js +54 -0
- package/src/services/proposals.js +36 -1
- package/src/services/runtime/prompt.js +36 -6
- package/src/services/scheduler.js +130 -0
- package/src/services/task-dispatch.js +73 -5
- package/src/services/task-review.js +9 -1
- package/src/services/task-runs.js +7 -1
- package/src/services/tasks.js +81 -0
- package/src/tools.js +5 -5
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "dsh-team-bots",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.364",
|
|
4
4
|
"description": "Project spaces, group chats, task boards, memory and usage for teams of DSH bots",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -41,6 +41,7 @@
|
|
|
41
41
|
"pretest": "npm run build",
|
|
42
42
|
"test": "node --test \"tests/*.test.js\" \"scripts/*.test.js\"",
|
|
43
43
|
"release": "node scripts/release.mjs",
|
|
44
|
+
"finalize": "node scripts/finalize.mjs",
|
|
44
45
|
"predev": "npm run build",
|
|
45
46
|
"dev": "node scripts/dev-server.mjs",
|
|
46
47
|
"prestart": "npm run build",
|
package/src/services/notify.js
CHANGED
|
@@ -18,6 +18,33 @@ import { text } from '../util.js'
|
|
|
18
18
|
export const RUN_PARTNER_NOTICE_CAP = 3
|
|
19
19
|
export const PARTNER_NOTICE_DAILY_CAP = 5
|
|
20
20
|
|
|
21
|
+
// ── 滞留阈值兜底(DSHBOTS-280):in_review/blocked 非施工状态的超时提醒 ─────────
|
|
22
|
+
// 只提醒不自动处置:不自动关单、不自动转状态、不重派,验收与解阻由项目总监闭环(DSHBOTS-281 口径)。
|
|
23
|
+
export const STALE_REVIEW_HOURS = 24
|
|
24
|
+
export const STALE_BLOCKED_HOURS = 48
|
|
25
|
+
export const STALE_CARD_NOTICE_PREFIX = '【滞留提醒】'
|
|
26
|
+
|
|
27
|
+
// 滞留判定(纯函数,供 scheduler 扫描与守门测试共用;阈值内/外两向可测)。
|
|
28
|
+
// 基准时间 = tasks.updated_at:blocked 无专门「进入 blocked」时间戳列,用 updated_at 近似
|
|
29
|
+
// ——转 blocked 走 updateTask 必刷 updated_at,其后任何卡面更新同样只推迟提醒,不会漏报。
|
|
30
|
+
export function staleCardHit({ status, updated_at, now = Date.now() } = {} ) {
|
|
31
|
+
if (status !== 'in_review' && status !== 'blocked') return null
|
|
32
|
+
const baseMs = updated_at ? new Date(updated_at).getTime() : NaN
|
|
33
|
+
if (!Number.isFinite(baseMs)) return null
|
|
34
|
+
const hours = (now - baseMs) / 3_600_000
|
|
35
|
+
const threshold = status === 'in_review' ? STALE_REVIEW_HOURS : STALE_BLOCKED_HOURS
|
|
36
|
+
return hours > threshold ? { status, hours, threshold } : null
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
// D-269 口径文案 = 原因(滞留时长+阈值)+ 点名总监的处置方向(DSHBOTS-281:处置归项目
|
|
40
|
+
// 总监自行闭环,不写「owner 可改/可用」转接话术;blocked 侧重派即自动重置死线窗口)。
|
|
41
|
+
export function staleCardNoticeBody({ identifier, status, hours } = {} ) {
|
|
42
|
+
const idle = Math.floor(hours)
|
|
43
|
+
return status === 'in_review'
|
|
44
|
+
? `${STALE_CARD_NOTICE_PREFIX}${identifier} 待验收已滞留 ${idle} 小时(阈值 ${STALE_REVIEW_HOURS}h):待总监验收收口(accept/return)。`
|
|
45
|
+
: `${STALE_CARD_NOTICE_PREFIX}${identifier} 受阻已滞留 ${idle} 小时(阈值 ${STALE_BLOCKED_HOURS}h):待总监解阻/收口(重派即自动重置死线窗口)。`
|
|
46
|
+
}
|
|
47
|
+
|
|
21
48
|
export const notifyMethods = {
|
|
22
49
|
// 伙伴提醒配额闸(DSHBOTS-267):放行返回 true;超限静默丢弃(warn 留痕)返回 false。
|
|
23
50
|
// run 归属不到(发起方无活跃运行)时不设限——空闲/人工会话的零星通知无循环风险。
|
|
@@ -48,6 +75,33 @@ export const notifyMethods = {
|
|
|
48
75
|
this.insertRunEvent(runId, 'partner_notice', `partner_notice:${targetId}`)
|
|
49
76
|
},
|
|
50
77
|
|
|
78
|
+
// 滞留提醒投递(DSHBOTS-280):等价 owner 通知通道——与 D-274 死线升级同款
|
|
79
|
+
// (insertMessage 系统留痕进任务线程 + mentions 点名项目 owner)。
|
|
80
|
+
// 防重=同卡每日 ≤1 条,以当日已有同前缀系统留痕为准,超限静默(只 warn 不投递)。
|
|
81
|
+
// 口径说明:D-267 配额闸(allowPartnerNotice)按 run_events type='partner_notice'
|
|
82
|
+
// 的 run 维度计数,run_id 非空挂不上无 run 的 scheduler 扫描;带 run 上下文的
|
|
83
|
+
// notifyMemberFromBot 路径照旧走同一配额流,本扫描路径的防重落点即消息留痕流。
|
|
84
|
+
notifyStaleCardOwner({ project, task, hit }) {
|
|
85
|
+
const identifier = `${project.key}-${task.number}`
|
|
86
|
+
const already = this.store.get(
|
|
87
|
+
"SELECT id FROM messages WHERE task_id = ? AND sender_type = 'system' AND body LIKE ? AND date(created_at) = date('now') LIMIT 1",
|
|
88
|
+
task.id, `${STALE_CARD_NOTICE_PREFIX}%`,
|
|
89
|
+
)
|
|
90
|
+
if (already) {
|
|
91
|
+
this.logger.warn?.('dsh-team: 滞留提醒同卡每日上限(1 条),静默跳过:%s', identifier)
|
|
92
|
+
return false
|
|
93
|
+
}
|
|
94
|
+
this.insertMessage({
|
|
95
|
+
projectId: project.id,
|
|
96
|
+
senderType: 'system',
|
|
97
|
+
senderId: 'system',
|
|
98
|
+
taskId: task.id,
|
|
99
|
+
body: staleCardNoticeBody({ identifier, status: hit.status, hours: hit.hours }),
|
|
100
|
+
mentions: project.owner_bot_id ? [project.owner_bot_id] : [],
|
|
101
|
+
})
|
|
102
|
+
return true
|
|
103
|
+
},
|
|
104
|
+
|
|
51
105
|
// 工具入口:target=member 通知本项目另一成员;target=task 提醒本项目执行中任务。
|
|
52
106
|
// 鉴权:调用者必须是本项目成员(memberContextFor),目标成员/任务都锁在本项目内,
|
|
53
107
|
// 跨项目 ref 一律解析不到(notFound),不泄露他项目是否存在同名目标。
|
|
@@ -10,6 +10,29 @@ import { publishSkill, rollbackSkill, skillSnapshot, stageSkill, syncSkills } fr
|
|
|
10
10
|
import { scanProjectSkills } from '../project-skills.js'
|
|
11
11
|
import { id, jsonParse, nowIso, parseDisabledSkills, sha256, slugify, text } from '../util.js'
|
|
12
12
|
|
|
13
|
+
// DSHBOTS-271 融合稿闸门:进化 bot_update 的 instructions 必须是「原指令+新增内容」的融合稿,
|
|
14
|
+
// 纯替换稿会把原提示词整体顶掉。保留率 = 原 instructions 的非空行(trim 后)在提案稿
|
|
15
|
+
// 行集合中的保留占比;低于阈值即拒绝(原因+替代指引,D-269 口径)。
|
|
16
|
+
// 原 instructions 为空(首次立指令)没有可保留内容,保留率恒为 1,直接放行并在 diff 留痕。
|
|
17
|
+
export const MERGE_KEEP_RATIO = 0.6
|
|
18
|
+
export const MERGE_DRAFT_GUIDANCE = '进化指令必须是原指令+新增内容的融合稿:保留原有指令全文再写改动,不是全新替换稿也不是增量补丁'
|
|
19
|
+
|
|
20
|
+
export function mergeKeepRatio(original , proposed ) {
|
|
21
|
+
const originLines = String(original ?? '').split('\n').map(line => line.trim()).filter(Boolean)
|
|
22
|
+
if (!originLines.length) return 1
|
|
23
|
+
const proposedLines = new Set(String(proposed ?? '').split('\n').map(line => line.trim()).filter(Boolean))
|
|
24
|
+
const kept = originLines.filter(line => proposedLines.has(line)).length
|
|
25
|
+
return kept / originLines.length
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
// 创建侧与应用侧(applyProposal 审批落地前)共用同一闸门函数,防创建后 bots.instructions
|
|
29
|
+
// 已被 updateBot 直改通道改写、或绕过创建路径提交的纯替换稿落地。
|
|
30
|
+
export function assertMergeDraft(original , proposed ) {
|
|
31
|
+
const ratio = mergeKeepRatio(original, proposed)
|
|
32
|
+
if (ratio >= MERGE_KEEP_RATIO) return ratio
|
|
33
|
+
throw badRequest(`原指令保留率 ${(ratio * 100).toFixed(0)}%,低于 ${MERGE_KEEP_RATIO * 100}%:${MERGE_DRAFT_GUIDANCE}`)
|
|
34
|
+
}
|
|
35
|
+
|
|
13
36
|
|
|
14
37
|
|
|
15
38
|
export const proposalsMethods = {
|
|
@@ -24,6 +47,10 @@ export const proposalsMethods = {
|
|
|
24
47
|
}
|
|
25
48
|
if (kind === 'skill' && !input.payload?.content) throw badRequest('技能提案必须包含内容')
|
|
26
49
|
const payload = input.payload ?? {}
|
|
50
|
+
// 融合稿闸门(DSHBOTS-271):bot_update 带 instructions 时按当前原稿校验保留率;skill 提案不经过此闸门。
|
|
51
|
+
if (kind === 'bot_update' && payload.instructions !== undefined) {
|
|
52
|
+
assertMergeDraft(this.botRow(input.botId).instructions, payload.instructions)
|
|
53
|
+
}
|
|
27
54
|
const canary = this.proposalCanary(input, kind)
|
|
28
55
|
const diff = kind === 'bot_update' ? this.botProposalDiff(input.botId, payload) : this.skillProposalDiff(payload)
|
|
29
56
|
const proposalId = id('prop')
|
|
@@ -51,7 +78,10 @@ export const proposalsMethods = {
|
|
|
51
78
|
if (payload[field] === undefined) continue
|
|
52
79
|
const before = bot[field] ?? null
|
|
53
80
|
if (String(before ?? '') === String(payload[field] ?? '')) continue
|
|
54
|
-
|
|
81
|
+
const entry = { from: before, to: payload[field] }
|
|
82
|
+
// 首次立指令留痕(DSHBOTS-271):原 instructions 为空时融合闸门放行,diff 标记首立。
|
|
83
|
+
if (field === 'instructions' && !String(before ?? '').trim()) entry.firstInstructions = true
|
|
84
|
+
diff[field] = entry
|
|
55
85
|
}
|
|
56
86
|
return diff
|
|
57
87
|
},
|
|
@@ -135,6 +165,11 @@ export const proposalsMethods = {
|
|
|
135
165
|
},
|
|
136
166
|
async applyProposal(proposal, payload, canary, actor) {
|
|
137
167
|
if (proposal.kind === 'bot_update') {
|
|
168
|
+
// 应用侧复核(DSHBOTS-271):审批落地前按 bots.instructions 当前值重跑同一闸门——
|
|
169
|
+
// 创建提案后原指令可能已被 updateBot 直改通道改写,此处防过期的融合稿或绕过创建路径的纯替换稿落地。
|
|
170
|
+
if (payload.instructions !== undefined) {
|
|
171
|
+
assertMergeDraft(this.botRow(proposal.bot_id).instructions, payload.instructions)
|
|
172
|
+
}
|
|
138
173
|
await this.updateBot(proposal.bot_id, {
|
|
139
174
|
...(payload.role !== undefined ? { role: payload.role } : {}),
|
|
140
175
|
...(payload.instructions !== undefined ? { instructions: payload.instructions } : {}),
|
|
@@ -12,6 +12,7 @@ import { scanProjectSkills } from '../../project-skills.js'
|
|
|
12
12
|
import { id, nowIso, parseDisabledSkills } from '../../util.js'
|
|
13
13
|
import { dmPrompt, replyDeliveryPrompt } from '../../botmsg.js'
|
|
14
14
|
import { rolloverPendingKey, rolloverSummaryKey } from '../../config.js'
|
|
15
|
+
import { taskDeadlineInfo } from '../tasks.js'
|
|
15
16
|
import { readWriteScopes } from '../write-scopes.js'
|
|
16
17
|
import { sanitizeTmpBotDirName } from '../../paths.js'
|
|
17
18
|
|
|
@@ -450,14 +451,25 @@ ${(chatMessage?.body ?? '(no text)').slice(0, 2_000)}`
|
|
|
450
451
|
// 时效要求(DSHBOTS-125/131):time_budget_minutes 未传/NULL 则完全不注入。
|
|
451
452
|
// 任务派生运行(review/handle/memory_review)与该任务共用同一时效上限,提示词必须同步告知,
|
|
452
453
|
// 否则运行会在代理不知情的情况下被超时中止。
|
|
453
|
-
|
|
454
|
+
// 任务级死线(DSHBOTS-274):deadline 只由施工分支(下方 if (task))传入,追加剩余时间一行;
|
|
455
|
+
// 预算耗尽改收尾轮口径。review/handle/memory_review 分支不传 deadline(死线豁免,不注入该行)。
|
|
456
|
+
const timeBudgetBlockFor = (row, deadline = null) => {
|
|
454
457
|
const timeBudget = Number(row?.timeBudgetMinutes ?? row?.time_budget_minutes)
|
|
455
|
-
|
|
458
|
+
if (!(Number.isFinite(timeBudget) && timeBudget > 0)) return ''
|
|
459
|
+
let deadlineLine = ''
|
|
460
|
+
if (deadline?.deadlineMs !== null && deadline?.deadlineMs !== undefined) {
|
|
461
|
+
const remainingMs = deadline.deadlineMs - Date.now()
|
|
462
|
+
deadlineLine = remainingMs > 0
|
|
463
|
+
? `Deadline: ${new Date(deadline.deadlineMs).toISOString()} / ${Math.ceil(remainingMs / 60_000)} min remaining.`
|
|
464
|
+
: `Deadline: ${new Date(deadline.deadlineMs).toISOString()} passed — 超线,本轮是收尾轮,优先可交付小步。`
|
|
465
|
+
}
|
|
466
|
+
return `
|
|
456
467
|
|
|
457
468
|
# Time budget
|
|
458
469
|
This task must be delivered within ${timeBudget} minutes; the run is aborted when the budget expires.
|
|
459
470
|
Plan the depth of your work accordingly. If you judge the task cannot be finished within that time, deliver a usable partial result on schedule and state clearly in your delivery report the remaining work and why the budget could not be met.
|
|
460
|
-
The effective timeout is the smaller of this budget and the global run timeout (runTimeoutMs)
|
|
471
|
+
The effective timeout is the smaller of this budget and the global run timeout (runTimeoutMs).
|
|
472
|
+
${deadlineLine}`.trimEnd()
|
|
461
473
|
}
|
|
462
474
|
if ((run.kind === 'review' || run.kind === 'handle') && task) {
|
|
463
475
|
// 超长防护(DSHBOTS-113,事故 m_33bddb;DSHBOTS-140 扩到 owner review / handle):
|
|
@@ -586,7 +598,8 @@ ${(sourceRun?.output ?? '').slice(0, 4000)}`
|
|
|
586
598
|
// (8k 截断 + 盲区标记),建卡上限 40k 的长卡不再整段进提示词。
|
|
587
599
|
const description = truncateTaskDescription(task.description)
|
|
588
600
|
// 时效要求(DSHBOTS-125/131):time_budget_minutes 未传/NULL 则完全不注入,提示词与无时效任务一致。
|
|
589
|
-
|
|
601
|
+
// 任务级死线(DSHBOTS-274):施工分支传入整卡死线,时效段追加剩余时间/收尾轮口径。
|
|
602
|
+
const timeBudgetBlock = timeBudgetBlockFor(task, taskDeadlineInfo(this.store, task))
|
|
590
603
|
// 紧急豁免(DSHBOTS-126):urgent 任务卡追加豁免指令(用户口径 m08196-2)——
|
|
591
604
|
// 无视车道排队立即开工;非 urgent 完全不注入,保持既有车道纪律。
|
|
592
605
|
const urgentExemptionBlock = task.priority === 'urgent' ? `
|
|
@@ -611,6 +624,16 @@ Before closing: if this task cannot be finished in one run or new closing condit
|
|
|
611
624
|
# Splitting for speed
|
|
612
625
|
Split only when all hold: the task has 2+ independent parts, parts have disjoint write scopes, and the joined cost of split cards stays below one-run delivery. One card per part: parent_task_id to this task, blocked_by where order matters. Not the project owner? Propose the split in your delivery report. Example: API card (src/api/) + docs card, run concurrently.`
|
|
613
626
|
// splitting-guidance:end
|
|
627
|
+
// delivery-selfcheck:start —— 交付前自校段(DSHBOTS-276,P1 派单质量前置):治 GS 实测
|
|
628
|
+
// 34% 补丁/返工卡——交付报告缺自跑验证证据。所有任务卡无条件注入,置于卡头
|
|
629
|
+
// Status 与 Description 之间(D-269 只追加手法):不动既有锚点前缀与 D-274 时效段
|
|
630
|
+
// (tests/prompt-budget ≤1900 静态前缀不打红、8k 截断「描述后即结尾」断言不打红、
|
|
631
|
+
// tests/prompt-prefix-order 段序不打红——三守门实测后选定此位)。
|
|
632
|
+
const deliverySelfcheckBlock = `
|
|
633
|
+
|
|
634
|
+
# Delivery self-check
|
|
635
|
+
Before reporting delivery, self-run the reproducible verification and put the evidence in the delivery report: the exact test/build commands you ran with their results, the list of files you changed, and the counts or links the acceptance criteria name (grep hits, test pass counts, commit hashes). Verify your own claims first — re-run the target tests and re-check the anchors yourself; do not report delivery on unverified work. Self-checking is always cheaper than rework: a delivery report without evidence will be returned.`
|
|
636
|
+
// delivery-selfcheck:end
|
|
614
637
|
// recovery note(DSHBOTS-135):宿主重启重排队的运行(core.js 置 recovery=1)追加恢复提醒,
|
|
615
638
|
// 对齐 superagent 原型语义——核实现状再续做、不重复外部动作。startRun 注入后即刻清零,
|
|
616
639
|
// 本段只在重排队后的第一次执行出现。与时效段(D-125)、豁免段(D-126)、纪律段(D-132)互不干扰。
|
|
@@ -634,7 +657,7 @@ When the deliverable includes images, put the files in the workspace and referen
|
|
|
634
657
|
# Assigned task ${project.key}-${task.number}
|
|
635
658
|
Title: ${task.title}
|
|
636
659
|
Priority: ${task.priority}
|
|
637
|
-
Status: ${task.status}
|
|
660
|
+
Status: ${task.status}${deliverySelfcheckBlock}
|
|
638
661
|
Description:
|
|
639
662
|
${description}${threads}${timeBudgetBlock}${urgentExemptionBlock}${recoveryNoteBlock}`
|
|
640
663
|
}
|
|
@@ -674,9 +697,16 @@ ${description}${threads}${timeBudgetBlock}${urgentExemptionBlock}${recoveryNoteB
|
|
|
674
697
|
|
|
675
698
|
# WeChat direct conversation
|
|
676
699
|
This message is from the project owner on WeChat — a real multi-turn conversation, not a one-off command. The project session is shared, so keep continuity across turns instead of restarting context each time. If the owner asks you to get work done, actually create and assign the task (team_assign_task — cards carry the project KEY-N number; do not create project-less task-N cards on the host shared board) instead of only acknowledging it. If you need the owner to decide something, use ask_user_question: it is forwarded to their WeChat and their reply comes back to you.` : ''
|
|
700
|
+
// DSHBOTS-272 用户提问锚点:触发消息是人发的(sender_type='user')才注入;通知降级与
|
|
701
|
+
// bot @mention 唤醒触发的运行(sender_type='bot')不注入——事故根因之一是通知内容与用户
|
|
702
|
+
// 提问同窗时无优先级指示,bot 回了条轻备忘就算处理完。
|
|
703
|
+
const ownerQuestionBlock = chatMessage?.sender_type === 'user' ? `
|
|
704
|
+
|
|
705
|
+
# Owner question priority
|
|
706
|
+
This message is from the project owner — answer it directly in this reply. Background notifications in context must not substitute for the answer; handle them only after (or skip to next turn).` : ''
|
|
677
707
|
return `${header}
|
|
678
708
|
|
|
679
|
-
Reply as ${bot.name} in the project group. Keep it under ${this.config.chatReplyMaxChars} characters unless the user asked for a full write-up. If the message requires another bot to do work, use team_assign_task instead of pretending to assign it. Use team_recall with a m_/c_/run id when you need the full text of a truncated item.${wechatBlock}
|
|
709
|
+
Reply as ${bot.name} in the project group. Keep it under ${this.config.chatReplyMaxChars} characters unless the user asked for a full write-up. If the message requires another bot to do work, use team_assign_task instead of pretending to assign it. Use team_recall with a m_/c_/run id when you need the full text of a truncated item.${wechatBlock}${ownerQuestionBlock}
|
|
680
710
|
|
|
681
711
|
# Group message
|
|
682
712
|
${(chatMessage?.body ?? '(no message)').slice(0, 4_000)}
|
|
@@ -9,8 +9,10 @@ import { applyRetention, reconcileUsage } from '../usage-reconcile.js'
|
|
|
9
9
|
import { id, jsonParse, nowIso, text } from '../util.js'
|
|
10
10
|
import { MAX_RATE_LIMIT_RETRIES, isQuotaExhausted, isRetryChainError } from './backoff.js'
|
|
11
11
|
import { ensureTaskRelationsSchema, unsatisfiedBlockedDeps } from './task-relations.js'
|
|
12
|
+
import { TASK_BUDGET_ROUNDS, TRANSITIONS, taskDeadlineInfo } from './tasks.js'
|
|
12
13
|
import { effectiveWriteScopes, writeScopesOverlap } from './write-scopes.js'
|
|
13
14
|
import { runReviewWatchdog } from './review-watchdog.js'
|
|
15
|
+
import { staleCardHit } from './notify.js'
|
|
14
16
|
|
|
15
17
|
// plan_wait 到期派生的宽限上界:退避到期超过这个时长视为时过境迁,不再自动续跑。
|
|
16
18
|
const PLAN_RETRY_MAX_LATE_SECONDS = 24 * 3600
|
|
@@ -307,6 +309,15 @@ export const schedulerMethods = {
|
|
|
307
309
|
return `project:${run.bot_id}:${run.project_id}`
|
|
308
310
|
},
|
|
309
311
|
|
|
312
|
+
// DSHBOTS-272 用户提问运行判定:kind='chat' 且触发消息是人发的(messages.sender_type='user')。
|
|
313
|
+
// 通知降级(notify.ts 成员通知落群聊流)与 bot 回复 @mention 唤醒(run-finish.ts)触发的
|
|
314
|
+
// chat 运行,触发消息 sender_type='bot',不算用户提问;出队置顶与合批保护只给人发的提问用。
|
|
315
|
+
isUserQuestionRun(run) {
|
|
316
|
+
if (run?.kind !== 'chat' || !run.trigger_ref) return false
|
|
317
|
+
const message = this.store.get('SELECT sender_type FROM messages WHERE id = ?', run.trigger_ref)
|
|
318
|
+
return message?.sender_type === 'user'
|
|
319
|
+
},
|
|
320
|
+
|
|
310
321
|
// 入站消息合批(DSHBOTS-231):领取一条运行后,把同一条绑定道内仍在排队的消息运行合并
|
|
311
322
|
// 进来——被合并的运行置 canceled(error_code='MERGED'),消息 id 记入本运行的
|
|
312
323
|
// merged_messages,executeRun 据此把多条消息一次性注入同一轮上下文,不再一条消息一次运行。
|
|
@@ -319,6 +330,10 @@ export const schedulerMethods = {
|
|
|
319
330
|
const windowMs = Number(this.store.setting('coalesce_window_ms', String(COALESCE_WINDOW_MS_DEFAULT)))
|
|
320
331
|
const claimedQueuedAt = Date.parse(run.queued_at)
|
|
321
332
|
const laneKey = this.claimKey(run)
|
|
333
|
+
// DSHBOTS-272 合批保护:用户提问(触发消息 sender_type='user')不并入非用户触发的运行——
|
|
334
|
+
// 并入后提问只能挤在 Merged messages 段、没有直接回答锚点(事故形态=通知运行回一条轻备忘
|
|
335
|
+
// 就算处理完)。留队保住「一条提问一条正式回复」;出队序已把用户提问置顶,下一拍即领取。
|
|
336
|
+
const claimedIsUserQuestion = this.isUserQuestionRun(run)
|
|
322
337
|
const candidates = this.store.all(
|
|
323
338
|
`SELECT * FROM runs WHERE status = 'queued' AND id != ? AND kind = ?
|
|
324
339
|
ORDER BY queued_at LIMIT 100`,
|
|
@@ -330,6 +345,8 @@ export const schedulerMethods = {
|
|
|
330
345
|
if (mergedIds.length >= max - 1) break
|
|
331
346
|
if (!candidate.trigger_ref) continue
|
|
332
347
|
if (this.claimKey(candidate) !== laneKey) continue
|
|
348
|
+
// DSHBOTS-272:候选是用户提问而本运行非用户触发(通知/伙伴唤醒)→ 不并,见上方注释。
|
|
349
|
+
if (!claimedIsUserQuestion && this.isUserQuestionRun(candidate)) continue
|
|
333
350
|
const queuedAt = Date.parse(candidate.queued_at)
|
|
334
351
|
if (!Number.isFinite(queuedAt) || !Number.isFinite(claimedQueuedAt)) continue
|
|
335
352
|
if (queuedAt < claimedQueuedAt || queuedAt - claimedQueuedAt > windowMs) continue
|
|
@@ -496,6 +513,100 @@ export const schedulerMethods = {
|
|
|
496
513
|
return canceled
|
|
497
514
|
},
|
|
498
515
|
|
|
516
|
+
// 滞留阈值兜底扫描(DSHBOTS-280):in_review 超 24h / blocked 超 48h 的卡向项目
|
|
517
|
+
// owner 发一条滞留提醒(notify.ts notifyStaleCardOwner),只提醒不自动处置——
|
|
518
|
+
// 不自动关单、不自动转状态、不重派。选点:复用 scheduler 周期 tick(与 D-194
|
|
519
|
+
// 验收守门同拍接入,不新增常驻进程);判定与防重见 notify.ts 的纯函数与每日上限。
|
|
520
|
+
// 逐卡容错:单卡异常只 warn,不拖垮整轮扫描。
|
|
521
|
+
scanStaleCards() {
|
|
522
|
+
const rows = this.store.all(
|
|
523
|
+
`SELECT t.id, t.status, t.updated_at, t.number,
|
|
524
|
+
p.id AS project_id, p.key AS project_key, p.owner_bot_id
|
|
525
|
+
FROM tasks t JOIN projects p ON p.id = t.project_id
|
|
526
|
+
WHERE t.status IN ('in_review', 'blocked') AND p.status = 'active'`,
|
|
527
|
+
)
|
|
528
|
+
const reminded = []
|
|
529
|
+
for (const row of rows) {
|
|
530
|
+
try {
|
|
531
|
+
const hit = staleCardHit(row)
|
|
532
|
+
if (!hit) continue
|
|
533
|
+
const delivered = this.notifyStaleCardOwner({
|
|
534
|
+
project: { id: row.project_id, key: row.project_key, owner_bot_id: row.owner_bot_id },
|
|
535
|
+
task: row,
|
|
536
|
+
hit,
|
|
537
|
+
})
|
|
538
|
+
if (delivered) reminded.push(row.id)
|
|
539
|
+
} catch (error ) {
|
|
540
|
+
this.logger.warn?.('dsh-team: 滞留扫描单卡失败 %s: %s', row.id, error?.message ?? error)
|
|
541
|
+
}
|
|
542
|
+
}
|
|
543
|
+
return reminded
|
|
544
|
+
},
|
|
545
|
+
|
|
546
|
+
// 任务级死线判定(DSHBOTS-274):纯同步——领取路径不得为此引入 await 边界, // 否则 claim 与状态写之间会开窗(tests/members.test.js 站内竞态教训)。
|
|
547
|
+
taskPastDeadline(task) {
|
|
548
|
+
const { deadlineMs } = taskDeadlineInfo(this.store, task)
|
|
549
|
+
return deadlineMs !== null && Date.now() > deadlineMs
|
|
550
|
+
},
|
|
551
|
+
|
|
552
|
+
// 任务级死线升级(DSHBOTS-274;DSHBOTS-281 口径升级):超线的 queued 施工运行拒绝启动
|
|
553
|
+
// ——运行置 canceled(TASK_DEADLINE,run_events 留 task_deadline 锚),卡面落系统评论
|
|
554
|
+
// (D-269 口径=原因+事实:点名项目总监,重派即自动重置死线窗口,不再写转接 owner 的话术),
|
|
555
|
+
// status 转 blocked(走 updateTask 单一状态流转漏斗,非法转换只评论不转),并复用系统消息
|
|
556
|
+
// 通道通知项目总监(notify.ts insertMessage 同款,进任务线程 + mentions 点名 owner_bot_id)。
|
|
557
|
+
// 幂等:enqueueTaskRun 同卡同时只有一条活跃运行,运行取消后不再进候选,每次派发至多升级
|
|
558
|
+
// 一次;重派即新窗口(基准=最新 queued_at),新窗口内预算×2 耗尽仍照常拦截。
|
|
559
|
+
async escalateTaskPastDeadline(task, runId) {
|
|
560
|
+
const { deadlineMs } = taskDeadlineInfo(this.store, task)
|
|
561
|
+
if (deadlineMs === null || Date.now() <= deadlineMs) return false
|
|
562
|
+
const project = this.projectRow(task.project_id)
|
|
563
|
+
const identifier = `${project?.key ?? 'T'}-${task.number}`
|
|
564
|
+
const deadlineIso = new Date(deadlineMs).toISOString()
|
|
565
|
+
// 点名总监(projects.owner_bot_id):留痕正文带总监花名,mentions 同点位名,收件人即第一负责人。
|
|
566
|
+
const directorName = project?.owner_bot_id
|
|
567
|
+
? this.botRow(project.owner_bot_id)?.name ?? '总监'
|
|
568
|
+
: '总监'
|
|
569
|
+
const windowMinutes = Number(task.time_budget_minutes) * TASK_BUDGET_ROUNDS
|
|
570
|
+
const body = `【${identifier}】超时中止(预算×${TASK_BUDGET_ROUNDS})已到,任务未关单:总监 ${directorName} 重派即自动重置死线窗口(新窗口剩余 ${windowMinutes} 分钟),请消化处理(死线 ${deadlineIso})。`
|
|
571
|
+
const now = nowIso()
|
|
572
|
+
const commentId = id('cmt')
|
|
573
|
+
this.store.transaction(() => {
|
|
574
|
+
this.store.run(
|
|
575
|
+
"UPDATE runs SET status = 'canceled', ended_at = ?, error_code = 'TASK_DEADLINE', error_message = ? WHERE id = ? AND status = 'queued'",
|
|
576
|
+
now, body, runId,
|
|
577
|
+
)
|
|
578
|
+
this.store.run(
|
|
579
|
+
'INSERT INTO run_events(id, run_id, type, message, created_at) VALUES (?, ?, ?, ?, ?)',
|
|
580
|
+
id('evt'), runId, 'task_deadline', `整卡死线已到(${deadlineIso}),拒绝启动新施工运行`, now,
|
|
581
|
+
)
|
|
582
|
+
this.store.run(
|
|
583
|
+
'INSERT INTO task_comments(id, task_id, author_type, author_id, body, created_at) VALUES (?, ?, ?, ?, ?, ?)',
|
|
584
|
+
commentId, task.id, 'system', 'system', body, now,
|
|
585
|
+
)
|
|
586
|
+
this.addTaskEvent(task.id, 'commented', { type: 'system', id: 'system' }, { commentId, deadline: deadlineIso, runId })
|
|
587
|
+
})
|
|
588
|
+
if (TRANSITIONS[task.status]?.has('blocked')) {
|
|
589
|
+
try {
|
|
590
|
+
await this.updateTask(task.id, { status: 'blocked' }, { type: 'system', id: 'system' })
|
|
591
|
+
} catch (error ) {
|
|
592
|
+
this.logger.warn?.('dsh-team: 死线转 blocked 失败 %s: %s', identifier, error?.message ?? error)
|
|
593
|
+
}
|
|
594
|
+
}
|
|
595
|
+
try {
|
|
596
|
+
this.insertMessage({
|
|
597
|
+
projectId: task.project_id,
|
|
598
|
+
senderType: 'system',
|
|
599
|
+
senderId: 'system',
|
|
600
|
+
taskId: task.id,
|
|
601
|
+
body,
|
|
602
|
+
mentions: project?.owner_bot_id ? [project.owner_bot_id] : [],
|
|
603
|
+
})
|
|
604
|
+
} catch (error ) {
|
|
605
|
+
this.logger.warn?.('dsh-team: 死线总监通知失败 %s: %s', identifier, error?.message ?? error)
|
|
606
|
+
}
|
|
607
|
+
return true
|
|
608
|
+
},
|
|
609
|
+
|
|
499
610
|
async tick() {
|
|
500
611
|
if (this.ticking || !this.started) return
|
|
501
612
|
this.ticking = true
|
|
@@ -513,6 +624,9 @@ export const schedulerMethods = {
|
|
|
513
624
|
// 验收滞留守门(DSHBOTS-194):in_review 卡静置超阈值即注入 owner 系统提醒;
|
|
514
625
|
// 内部自容错(异常只 warn),阈值 0 = 关闭时直接返回。
|
|
515
626
|
runReviewWatchdog(this)
|
|
627
|
+
// 滞留阈值兜底(DSHBOTS-280):in_review/blocked 超 24h/48h 向 owner 只提醒
|
|
628
|
+
// 不自动处置;内部逐卡容错(单卡异常只 warn)。
|
|
629
|
+
this.scanStaleCards()
|
|
516
630
|
const activeRows = this.store.all(
|
|
517
631
|
// 写占用只看「进行中」的任务(DSHBOTS-73,用户口径):join tasks 取任务当前状态,
|
|
518
632
|
// 任务不在 in_progress 的写运行不再占槽——离开进行中即释放占用/锁定。
|
|
@@ -546,13 +660,19 @@ export const schedulerMethods = {
|
|
|
546
660
|
if (activeTotal >= maxRuns + 1) return
|
|
547
661
|
// 出队顺序:任务运行按任务优先级(urgent 先),其余运行视同 high——用户交互不能被任务积压拖死;
|
|
548
662
|
// 同优先级按入队时间,保证 FIFO 不饿死老运行。
|
|
663
|
+
// DSHBOTS-272 用户提问不被静默压后:不动用户运行的档位(同车道先入队的 review/handle
|
|
664
|
+
// 等人机运行契约不变,见 tests/avatar-lanes.test.js),改为把通知/伙伴唤醒触发的 chat
|
|
665
|
+
// 运行(触发消息 sender_type='bot')降到机器流量档——同车道内用户提问(tier 1)必然
|
|
666
|
+
// 先于它领取,即使提问后到;通知备忘让位于一切人机交互与 urgent/high 任务。
|
|
549
667
|
// 候选过滤(DSHBOTS-110):任务已终态(done/canceled)的 queued 任务运行不再领取;
|
|
550
668
|
// 只排 kind='task',非任务运行不受影响。LEFT JOIN 出 NULL 的孤儿任务运行不过滤。
|
|
551
669
|
const candidates = this.store.all(
|
|
552
670
|
`SELECT r.* FROM runs r LEFT JOIN tasks t ON t.id = r.task_id
|
|
671
|
+
LEFT JOIN messages m ON m.id = r.trigger_ref
|
|
553
672
|
WHERE r.status = 'queued'
|
|
554
673
|
AND NOT (r.kind = 'task' AND COALESCE(t.status, '') IN ('done', 'canceled'))
|
|
555
674
|
ORDER BY CASE
|
|
675
|
+
WHEN r.kind = 'chat' AND m.sender_type = 'bot' THEN 2
|
|
556
676
|
WHEN r.kind <> 'task' OR t.priority IS NULL THEN 1
|
|
557
677
|
WHEN t.priority = 'urgent' THEN 0
|
|
558
678
|
WHEN t.priority = 'high' THEN 1
|
|
@@ -642,6 +762,16 @@ export const schedulerMethods = {
|
|
|
642
762
|
}
|
|
643
763
|
continue
|
|
644
764
|
}
|
|
765
|
+
// 任务级死线(DSHBOTS-274):施工运行(kind='task')领取前检查整卡死线——
|
|
766
|
+
// 死线 = 派发基准 + time_budget_minutes × TASK_BUDGET_ROUNDS(默认 2 轮,
|
|
767
|
+
// 延期阀见 tasks.ts updateTask)。超线即拒绝启动新施工运行并自动升级
|
|
768
|
+
// (评论 + blocked + owner 通知,escalateTaskPastDeadline);
|
|
769
|
+
// review/handle 等派生运行不走此闸(kind!=='task' 豁免,死线只拦施工轮)。
|
|
770
|
+
// 判定同步、升级异步放行:不为本闸在领取路径引入 await 边界。
|
|
771
|
+
if (taskRow && this.taskPastDeadline(taskRow)) {
|
|
772
|
+
void this.escalateTaskPastDeadline(taskRow, run.id).catch(error => this.logger.warn?.('dsh-team: 死线升级失败 %s: %s', run.id, error?.message ?? error))
|
|
773
|
+
continue
|
|
774
|
+
}
|
|
645
775
|
}
|
|
646
776
|
const claimed = this.store.run(
|
|
647
777
|
"UPDATE runs SET status = 'running', started_at = ?, last_progress_at = ? WHERE id = ? AND status = 'queued'",
|
|
@@ -11,6 +11,12 @@ import { filterIgnoredWriteScopes, parseWriteScopes, parseWriteScopesFromDescrip
|
|
|
11
11
|
import { resolveSandboxOverride } from './file-policy.js'
|
|
12
12
|
import { ensureTaskSandboxSchema } from './task-sandbox-schema.js'
|
|
13
13
|
|
|
14
|
+
// 勘误并卡提示(DSHBOTS-276,P1 派单质量前置)的判定常量:新卡标题含修正类关键词 +
|
|
15
|
+
// 同项目同承接人近 7 天 done 卡写范围有交集,双条件同时命中才在卡面描述尾部追加并卡提示行。
|
|
16
|
+
// 仅提示不拦截:D-82 重复卡闸、D-270 领取闸等既有拒绝行为一概不变。
|
|
17
|
+
const CORRECTION_TITLE_RE = /修正|勘误|补充|改写/
|
|
18
|
+
const RECENT_DONE_WINDOW_MS = 7 * 86_400_000
|
|
19
|
+
|
|
14
20
|
export const taskDispatchMethods = {
|
|
15
21
|
// 派发门禁(DSHBOTS-262):暂停中的 Bot 不接新卡——新建派单、改派、重派一律拒绝,
|
|
16
22
|
// 先恢复(bot status → active)再派;归档/移除等其余不可用态沿用 assertMember 既有口径。
|
|
@@ -19,12 +25,36 @@ export const taskDispatchMethods = {
|
|
|
19
25
|
if (bot?.status === 'paused') throw badRequest(`Bot ${bot.name} 已暂停,先恢复再派卡`)
|
|
20
26
|
this.assertMember(projectId, botId)
|
|
21
27
|
},
|
|
28
|
+
// 无主卡领取开工的写范围声明闸门(DSHBOTS-270):与 assignFromBot「派单必须声明写
|
|
29
|
+
// 范围」同口径的预判——卡面清单优先,空则从描述「写范围:」行兜底解析
|
|
30
|
+
// (parseWriteScopesFromDescription),再过忽略规则清洗(全部命中 .gitignore 被剔除
|
|
31
|
+
// = 实际未声明)。拒绝文案按 D-269 口径 = 原因 + 替代命令;带声明的卡直接放行。
|
|
32
|
+
assertClaimScopes(task, project) {
|
|
33
|
+
const declared = readWriteScopes(task.write_scopes)
|
|
34
|
+
const scopes = filterIgnoredWriteScopes(
|
|
35
|
+
declared.length ? declared : parseWriteScopesFromDescription(task.description),
|
|
36
|
+
this.projectWorkDir(project),
|
|
37
|
+
).kept
|
|
38
|
+
if (scopes.length) return
|
|
39
|
+
throw badRequest(
|
|
40
|
+
`领取开工必须声明写范围:任务 ${project.key}-${task.number} 未声明写范围,`
|
|
41
|
+
+ '其写运行会与同项目其他写运行全部保守串行,一条卡即可堵死队列。'
|
|
42
|
+
+ '请先补声明再领取:用工作台 update_task / task_update_scopes 给该卡补 write_scopes,'
|
|
43
|
+
+ '或在卡面描述里写一行「写范围:a, b」(由 parseWriteScopesFromDescription 兜底解析);'
|
|
44
|
+
+ '补声明后重新领取即可按声明范围并行。',
|
|
45
|
+
)
|
|
46
|
+
},
|
|
22
47
|
// sandboxOverride 选项(DSHBOTS-156):undefined=不动(重派未传 sandbox 时保留旧覆盖);
|
|
23
48
|
// null=显式清掉回落 bot 配置档(重派传 sandbox='inherit');字符串=收紧档(派发入口已校验)。
|
|
24
49
|
async assignTask(taskId, botId, actor = this.actor(), { autoStart = true, sandboxOverride } = {} ) {
|
|
25
50
|
const task = this.taskRow(taskId)
|
|
26
51
|
const project = this.projectRow(task.project_id)
|
|
27
52
|
this.assertDispatchTarget(project.id, botId)
|
|
53
|
+
// 共享板无主卡声明闸门(DSHBOTS-270):无主卡获得 owner 即领取开工——assignTask 稍后
|
|
54
|
+
// 会排 task_run outbox 进施工,未声明写范围的写运行被调度器按「未声明」保守串行
|
|
55
|
+
// (scheduler 既定语义不动),既堵自己也占道。与总监派单同口径在入口拒绝;
|
|
56
|
+
// 改派(卡已有 owner)与 autoStart=false 的重派/存量补挂流程不在此口。
|
|
57
|
+
if (!task.assignee_bot_id && autoStart !== false) this.assertClaimScopes(task, project)
|
|
28
58
|
const active = this.store.get(
|
|
29
59
|
"SELECT id, kind FROM runs WHERE task_id = ? AND status IN ('queued', 'running')",
|
|
30
60
|
taskId,
|
|
@@ -188,7 +218,14 @@ export const taskDispatchMethods = {
|
|
|
188
218
|
if (!binding) throw forbidden('只有项目群中的 Bot 可以执行该操作')
|
|
189
219
|
if (!binding.project_id) throw forbidden('单聊会话不在项目中,无法管理任务')
|
|
190
220
|
const project = this.projectRow(binding.project_id)
|
|
191
|
-
|
|
221
|
+
// DSHBOTS-269 失败语义引导:拒绝文案 = 原因 + 替代命令/正确下一步。
|
|
222
|
+
if (project.owner_bot_id !== binding.bot_id) {
|
|
223
|
+
throw forbidden(
|
|
224
|
+
'只有项目总监 Bot 可以执行该操作:总监命令(如 team_assign_task 派单/重派、'
|
|
225
|
+
+ 'team_review_task 验收关单、team_block_task 强制受阻)由项目 owner 总监 Bot 调用;'
|
|
226
|
+
+ '成员请在项目群内向总监请求代为执行。',
|
|
227
|
+
)
|
|
228
|
+
}
|
|
192
229
|
return { binding, project, actor: { type: 'bot', id: binding.bot_id } }
|
|
193
230
|
},
|
|
194
231
|
// 派单侧同写范围重复检测(DSHBOTS-82):同一文件上叠 N 张小卡时,各卡各立各排,
|
|
@@ -210,6 +247,26 @@ export const taskDispatchMethods = {
|
|
|
210
247
|
}
|
|
211
248
|
return null
|
|
212
249
|
},
|
|
250
|
+
// 勘误并卡引导(DSHBOTS-276):找同项目同承接人「近 7 天内 done」且写范围与本次声明有
|
|
251
|
+
// 交集(元素级,路径前缀不展开——与 D-82 完全一致检测同一口径的读取函数)的最近一张卡。
|
|
252
|
+
// 时间基准用 COALESCE(completed_at, updated_at):done 终态时刻优先(D-69 回填保证存量行有值)。
|
|
253
|
+
findRecentDoneOverlapTask(projectId, assigneeBotId, scopes) {
|
|
254
|
+
const target = new Set(scopes)
|
|
255
|
+
if (!target.size) return null
|
|
256
|
+
const since = new Date(Date.now() - RECENT_DONE_WINDOW_MS).toISOString()
|
|
257
|
+
const candidates = this.store.all(
|
|
258
|
+
`SELECT id, number, title, write_scopes FROM tasks
|
|
259
|
+
WHERE project_id = ? AND assignee_bot_id = ? AND status = 'done'
|
|
260
|
+
AND COALESCE(completed_at, updated_at) >= ?
|
|
261
|
+
ORDER BY COALESCE(completed_at, updated_at) DESC`,
|
|
262
|
+
projectId, assigneeBotId, since,
|
|
263
|
+
)
|
|
264
|
+
for (const row of candidates) {
|
|
265
|
+
const other = readWriteScopes(row.write_scopes)
|
|
266
|
+
if (other.some(scope => target.has(scope))) return row
|
|
267
|
+
}
|
|
268
|
+
return null
|
|
269
|
+
},
|
|
213
270
|
// 派单清洗(DSHBOTS-168):写范围声明的程序化质量闸门,三入口共用——
|
|
214
271
|
// createTask(owner 派单路径权威清洗)、assignFromBot(经 createTask 同一清洗)、
|
|
215
272
|
// taskUpdateScopes(改卡入口)。两步:
|
|
@@ -278,16 +335,27 @@ export const taskDispatchMethods = {
|
|
|
278
335
|
const forceDuplicate = input.force_duplicate === true
|
|
279
336
|
const urgentExempt = input.priority === 'urgent'
|
|
280
337
|
if (duplicate && !forceDuplicate && !urgentExempt) {
|
|
338
|
+
// DSHBOTS-269 失败语义引导:拒绝文案 = 原因 + 替代命令(改卡重派而非新开卡)。
|
|
281
339
|
throw conflict(
|
|
282
|
-
`已存在同写范围任务 ${project.key}-${duplicate.number} ${duplicate.title}
|
|
283
|
-
+ '
|
|
284
|
-
+ '
|
|
340
|
+
`已存在同写范围任务 ${project.key}-${duplicate.number} ${duplicate.title},请勿重复派单:`
|
|
341
|
+
+ '请用 team_assign_task 重派原任务改卡(task=原任务号,附 title/description 即修正卡面),不要为新内容新开卡;'
|
|
342
|
+
+ '确需第二张同范围卡时传 force_duplicate=true(每次使用留审计);紧急任务(priority=urgent)豁免此拦截,可立即派单施工。',
|
|
285
343
|
)
|
|
286
344
|
}
|
|
345
|
+
// 勘误并卡提示(DSHBOTS-276,不强制):双条件命中(标题关键词 + 近期 done 卡写范围交集)
|
|
346
|
+
// 时在卡面描述尾部追加一行并卡建议。仅追加描述,不拦截派单、不改任何拒绝行为;
|
|
347
|
+
// 未命中时描述原样透传,createTask 路径零变化。
|
|
348
|
+
let description = input.description
|
|
349
|
+
if (CORRECTION_TITLE_RE.test(String(input.title ?? ''))) {
|
|
350
|
+
const overlap = this.findRecentDoneOverlapTask(project.id, member.id, scopes)
|
|
351
|
+
if (overlap) {
|
|
352
|
+
description = `${input.description ? `${input.description}\n` : ''}(note: recent done card ${project.key}-${overlap.number} overlaps these scopes — consider merging follow-up corrections into one card)`
|
|
353
|
+
}
|
|
354
|
+
}
|
|
287
355
|
const task = await this.createTask({
|
|
288
356
|
projectId: project.id,
|
|
289
357
|
title: input.title,
|
|
290
|
-
description
|
|
358
|
+
description,
|
|
291
359
|
assigneeBotId: member.id,
|
|
292
360
|
priority: input.priority,
|
|
293
361
|
// 透传原始清单(DSHBOTS-168):清洗+审计由 createTask 统一做(审计挂真实 taskId)。
|
|
@@ -21,7 +21,15 @@ export const taskReviewMethods = {
|
|
|
21
21
|
/** 验收/退回的共用流程:owner Bot 会话与微信管理员共用。 */
|
|
22
22
|
async applyReview(task, project, decision, comment, actor) {
|
|
23
23
|
const identifier = `${project.key}-${task.number}`
|
|
24
|
-
|
|
24
|
+
// DSHBOTS-269 失败语义引导:拒绝文案 = 原因 + 替代命令/正确下一步(工具调用返回层,
|
|
25
|
+
// 与 run-finish 群聊/微信失败终态白名单契约不同层,互不影响)。
|
|
26
|
+
if (task.status !== 'in_review') {
|
|
27
|
+
throw conflict(
|
|
28
|
+
`任务 ${identifier} 不在待验收状态(当前 ${task.status}):`
|
|
29
|
+
+ 'team_review_task 只验收已交付(in_review)的卡,请先等负责人交付;'
|
|
30
|
+
+ '若卡面或目标有误,用 team_assign_task 重派原任务(task=原任务号,附 title/description 即改卡)修正。',
|
|
31
|
+
)
|
|
32
|
+
}
|
|
25
33
|
if (decision === 'accept') {
|
|
26
34
|
await this.updateTask(task.id, { status: 'done' }, actor)
|
|
27
35
|
if (comment) await this.addComment(task.id, comment, actor)
|
|
@@ -147,7 +147,13 @@ export const taskRunMethods = {
|
|
|
147
147
|
return runId
|
|
148
148
|
},
|
|
149
149
|
enqueueOwnerTaskRun(task, kind) {
|
|
150
|
-
|
|
150
|
+
// 验收竞态收敛(DSHBOTS-275):入队前终态预检——任务已 done/canceled 时直接跳过,
|
|
151
|
+
// 不产生 review/handle run 行(GS 实测交付排队与 owner 关单竞态空跑约 1/8)。
|
|
152
|
+
// 以库内最新状态为准(调用方传入的 task 行可能已过期);入队后才到终态的残留排队运行
|
|
153
|
+
// 仍由 cancelSupersededQueuedRuns「任务已结束,排队运行被清理」兜底清理,语义不变。
|
|
154
|
+
const current = this.taskRow(task.id)
|
|
155
|
+
if (!current || current.status === 'done' || current.status === 'canceled') return null
|
|
156
|
+
const project = this.projectRow(current.project_id)
|
|
151
157
|
const owner = project.owner_bot_id
|
|
152
158
|
? this.store.get("SELECT * FROM bots WHERE id = ? AND status = 'active'", project.owner_bot_id)
|
|
153
159
|
: null
|
package/src/services/tasks.js
CHANGED
|
@@ -58,6 +58,63 @@ export function ensureTaskTerminalTimeBackfill(store) {
|
|
|
58
58
|
store.run('INSERT INTO schema_migrations(name, applied_at) VALUES (?, ?) ON CONFLICT(name) DO NOTHING', 'tasks_terminal_time_backfill', nowIso())
|
|
59
59
|
}
|
|
60
60
|
|
|
61
|
+
// 任务级死线 schema(DSHBOTS-274):tasks.budget_updated_at 记最近一次 time_budget_minutes
|
|
62
|
+
// 实改时刻,标记新预算生效点(死线长度随新预算重算;基准锚点见 taskDeadlineInfo=最新派发)。
|
|
63
|
+
export function ensureTaskDeadlineSchema(store) {
|
|
64
|
+
if (store.get('SELECT 1 AS ok FROM schema_migrations WHERE name = ?', 'tasks_budget_updated_at')) return
|
|
65
|
+
const columns = store.all('PRAGMA table_info(tasks)').map(row => row.name)
|
|
66
|
+
if (!columns.includes('budget_updated_at')) {
|
|
67
|
+
store.exec('ALTER TABLE tasks ADD COLUMN budget_updated_at TEXT')
|
|
68
|
+
}
|
|
69
|
+
store.run('INSERT INTO schema_migrations(name, applied_at) VALUES (?, ?) ON CONFLICT(name) DO NOTHING', 'tasks_budget_updated_at', nowIso())
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
// 允许轮数(DSHBOTS-274):整卡死线 = 派发基准 + time_budget_minutes × TASK_BUDGET_ROUNDS。
|
|
73
|
+
export const TASK_BUDGET_ROUNDS = 2
|
|
74
|
+
|
|
75
|
+
// 任务级死线纯计算(DSHBOTS-274 立卡,DSHBOTS-281 基准升级):
|
|
76
|
+
// 治「time_budget_minutes 只约束单运行、多轮派发各自重置预算、累计耗时无上限」。
|
|
77
|
+
// - 派发基准 = 最新一次派发(latestDispatchAt):重派即新窗口——重派动作本身视为预算决策,
|
|
78
|
+
// 存量老卡重派不再立即升级,也无需先实改 time_budget_minutes 延期;
|
|
79
|
+
// - 延期阀兼容:owner 实改预算后死线按新预算×轮数从最新派发基准重算;改后尚未重派时
|
|
80
|
+
// 基准仍取最新派发(不早于旧口径的「退回首次派发」),budget_updated_at 只标记新预算生效;
|
|
81
|
+
// - 真超时仍拦截:新窗口内预算×轮数耗尽后,下一次领取照常升级(escalateTaskPastDeadline);
|
|
82
|
+
// - 无预算(NULL/≤0)或无派发记录返回 null,死线不约束(与 D-131 无时效口径一致)。
|
|
83
|
+
export const taskDeadlineMs = ({ budgetMinutes, firstDispatchAt, latestDispatchAt }) => {
|
|
84
|
+
const minutes = Number(budgetMinutes)
|
|
85
|
+
if (!Number.isFinite(minutes) || minutes <= 0) return null
|
|
86
|
+
const baseRaw = latestDispatchAt ?? firstDispatchAt
|
|
87
|
+
const base = baseRaw ? Date.parse(baseRaw) : NaN
|
|
88
|
+
if (!Number.isFinite(base)) return null
|
|
89
|
+
return base + minutes * TASK_BUDGET_ROUNDS * 60_000
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
// 任务级死线信息(DSHBOTS-274;DSHBOTS-281 基准=最新派发):派发时刻口径 = runs 表
|
|
93
|
+
// kind='task' 的 queued_at(入队即派发;started_at 领取后才写,未被领取过的派发拿不到
|
|
94
|
+
// 时刻,故不用 started_at)。review/handle 等派生运行 kind!=='task' 天然不计入派发史。
|
|
95
|
+
export function taskDeadlineInfo(store, task) {
|
|
96
|
+
ensureTaskDeadlineSchema(store)
|
|
97
|
+
const firstDispatchAt = store.get(
|
|
98
|
+
"SELECT MIN(queued_at) AS v FROM runs WHERE task_id = ? AND kind = 'task'",
|
|
99
|
+
task.id,
|
|
100
|
+
)?.v ?? null
|
|
101
|
+
// 重派即新窗口:每次派发都把死线基准推到本次 queued_at,重派(含无主卡领取)自动重置,
|
|
102
|
+
// 不依赖任何显式预算实改(与 D-274「改后首个派发」同构,把重派本身视为预算决策)。
|
|
103
|
+
const latestDispatchAt = store.get(
|
|
104
|
+
"SELECT MAX(queued_at) AS v FROM runs WHERE task_id = ? AND kind = 'task'",
|
|
105
|
+
task.id,
|
|
106
|
+
)?.v ?? null
|
|
107
|
+
return {
|
|
108
|
+
deadlineMs: taskDeadlineMs({
|
|
109
|
+
budgetMinutes: task.time_budget_minutes,
|
|
110
|
+
firstDispatchAt,
|
|
111
|
+
latestDispatchAt,
|
|
112
|
+
}),
|
|
113
|
+
firstDispatchAt,
|
|
114
|
+
latestDispatchAt,
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
|
|
61
118
|
export const tasksMethods = {
|
|
62
119
|
// 缺省任务预算(DSHBOTS-198):env > 无进展看门狗(分钟)生效值 > 兜底 20,
|
|
63
120
|
// 建档补挂与重派补挂共用同一缺省链。
|
|
@@ -273,6 +330,18 @@ export const tasksMethods = {
|
|
|
273
330
|
}
|
|
274
331
|
if (patch.assigneeBotId !== undefined) {
|
|
275
332
|
if (patch.assigneeBotId) this.assertDispatchTarget(task.project_id, patch.assigneeBotId)
|
|
333
|
+
// 共享板无主卡声明闸门(DSHBOTS-270):无主卡经编辑表单获得 owner,与 assignTask
|
|
334
|
+
// 领取是同一条「获得 owner 即将进施工」链路,同口径过闸;同一次 patch 里补的
|
|
335
|
+
// write_scopes / 描述「写范围:」行一并纳入判定——补声明后放行。
|
|
336
|
+
if (patch.assigneeBotId && !task.assignee_bot_id) {
|
|
337
|
+
this.assertClaimScopes({
|
|
338
|
+
...task,
|
|
339
|
+
write_scopes: patch.writeScopes === undefined
|
|
340
|
+
? task.write_scopes
|
|
341
|
+
: JSON.stringify(parseWriteScopes(patch.writeScopes)),
|
|
342
|
+
description: patch.description === undefined ? task.description : text(patch.description, 40_000),
|
|
343
|
+
}, this.projectRow(task.project_id))
|
|
344
|
+
}
|
|
276
345
|
updates.assignee_bot_id = patch.assigneeBotId || null
|
|
277
346
|
}
|
|
278
347
|
if (patch.position !== undefined) updates.position = Number(patch.position) || 0
|
|
@@ -281,6 +350,18 @@ export const tasksMethods = {
|
|
|
281
350
|
if (patch.writeScopes !== undefined) {
|
|
282
351
|
updates.write_scopes = JSON.stringify(parseWriteScopes(patch.writeScopes))
|
|
283
352
|
}
|
|
353
|
+
// 延期阀(DSHBOTS-274):update_task 实改 time_budget_minutes 后整卡死线按新预算重算——
|
|
354
|
+
// 记 budget_updated_at 标记新预算生效;派发基准统一为最新派发(DSHBOTS-281 重派即新窗口),
|
|
355
|
+
// 改预算与重派各自独立生效,改后重派拿到「新预算 × 新窗口」。
|
|
356
|
+
if (patch.timeBudgetMinutes !== undefined || patch.time_budget_minutes !== undefined) {
|
|
357
|
+
const rawBudget = patch.timeBudgetMinutes ?? patch.time_budget_minutes
|
|
358
|
+
const budget = Number(rawBudget) > 0 ? clampNumber(rawBudget, 1, 10_080, null) : null
|
|
359
|
+
if (budget !== task.time_budget_minutes) {
|
|
360
|
+
ensureTaskDeadlineSchema(this.store)
|
|
361
|
+
updates.time_budget_minutes = budget
|
|
362
|
+
updates.budget_updated_at = nowIso()
|
|
363
|
+
}
|
|
364
|
+
}
|
|
284
365
|
if (patch.status !== undefined) {
|
|
285
366
|
if (!TASK_STATUSES.has(patch.status)) throw badRequest('任务状态无效')
|
|
286
367
|
this.assertTransition(task.status, patch.status)
|
package/src/tools.js
CHANGED
|
@@ -70,7 +70,7 @@ export function installTeamTools(ctx, service) {
|
|
|
70
70
|
|
|
71
71
|
ctx.tools.register({
|
|
72
72
|
name: 'team_assign_task',
|
|
73
|
-
description: '
|
|
73
|
+
description: 'The regular path to dispatch project work to a bot: create a new task card or re-dispatch/amend an existing one. Only the project owner bot may use this. With `task` it re-dispatches that existing task (retry to the same bot, or hand it to a different bot); passing title/description/priority alongside `task` amends the card — the assignee is immediately notified to correct course per the new card. Without `task`, creates a new task that starts automatically. A new task whose write scopes exactly match an unfinished task in the same project is rejected as a duplicate dispatch; merge the cards into the existing task instead, or pass force_duplicate to override (audited). Consolidate before dispatch: one requirement goes into one card — amend and re-dispatch the existing card (or add subtask cards with parent_task_id for genuinely independent parts) instead of opening several sibling cards for the same need.',
|
|
74
74
|
parameters: {
|
|
75
75
|
type: 'object',
|
|
76
76
|
properties: {
|
|
@@ -101,7 +101,7 @@ export function installTeamTools(ctx, service) {
|
|
|
101
101
|
|
|
102
102
|
ctx.tools.register({
|
|
103
103
|
name: 'team_block_task',
|
|
104
|
-
description: '
|
|
104
|
+
description: 'The regular path to force-stop a task: force-move an in-progress task to blocked and abort its queued/running runs. Only the project owner bot may use this. The stop rides the existing abort chain and is audited as task.force_blocked (run error code TASK_FORCE_BLOCKED). Use it to stop a runaway or wrong-headed execution; typically followed by re-dispatching the same task with an amended card.',
|
|
105
105
|
parameters: {
|
|
106
106
|
type: 'object',
|
|
107
107
|
properties: {
|
|
@@ -120,7 +120,7 @@ export function installTeamTools(ctx, service) {
|
|
|
120
120
|
|
|
121
121
|
ctx.tools.register({
|
|
122
122
|
name: 'team_review_task',
|
|
123
|
-
description: '
|
|
123
|
+
description: 'The one and only regular path to close or return a delivered task: `accept` marks a waiting-for-review card done; `return` sends it back to the assignee for rework (a reason is required). Hand-editing the store or any side door does not close a card — when the delivery is not reviewable yet, wait for the assignee to deliver, or amend and re-dispatch it with team_assign_task. Only the project owner bot may use this.',
|
|
124
124
|
parameters: {
|
|
125
125
|
type: 'object',
|
|
126
126
|
properties: {
|
|
@@ -343,14 +343,14 @@ export function installTeamTools(ctx, service) {
|
|
|
343
343
|
|
|
344
344
|
ctx.tools.register({
|
|
345
345
|
name: 'team_propose_evolution',
|
|
346
|
-
description: 'Propose a versioned change to your own instructions or to the shared skill library. A human must review and approve it; this tool never applies the change.',
|
|
346
|
+
description: 'Facts/preferences go to memory first (team_remember); propose evolution only for structural instruction/skill changes whose benefit clearly exceeds memory. Propose a versioned change to your own instructions or to the shared skill library. A human must review and approve it; this tool never applies the change.',
|
|
347
347
|
parameters: {
|
|
348
348
|
type: 'object',
|
|
349
349
|
properties: {
|
|
350
350
|
kind: { type: 'string', enum: ['bot_update', 'skill'], description: 'bot_update changes your role/instructions; skill publishes a reusable skill proposal' },
|
|
351
351
|
title: { type: 'string', description: 'Short proposal title' },
|
|
352
352
|
rationale: { type: 'string', description: 'Why this change is useful and what evidence supports it' },
|
|
353
|
-
instructions: { type: 'string', description: 'For bot_update:
|
|
353
|
+
instructions: { type: 'string', description: 'For bot_update: 进化指令必须是原指令+新增内容的融合稿:保留原有指令全文再写改动,不是全新替换稿也不是增量补丁(保留率低于 60% 会被系统拒绝)' },
|
|
354
354
|
slug: { type: 'string', description: 'For skill: lowercase skill slug' },
|
|
355
355
|
name: { type: 'string', description: 'For skill: display name' },
|
|
356
356
|
description: { type: 'string', description: 'For skill: one-line description' },
|