dsh-team-bots 0.2.364 → 0.2.395
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/README.md +12 -2
- package/docs/README.zh.md +12 -2
- package/docs/assets/global-config.png +0 -0
- package/docs/assets/global-config.zh.png +0 -0
- package/docs/assets/tasks.png +0 -0
- package/docs/assets/tasks.zh.png +0 -0
- package/docs/assets/usage.png +0 -0
- package/docs/assets/usage.zh.png +0 -0
- package/index.js +2 -0
- package/package.json +1 -1
- package/src/config.js +55 -10
- package/src/db-migrations.js +144 -9
- package/src/paths.js +12 -0
- package/src/services/approvals.js +12 -2
- package/src/services/core.js +2 -1
- package/src/services/notify.js +24 -8
- package/src/services/questions.js +16 -3
- package/src/services/run-finish.js +18 -3
- package/src/services/runs.js +369 -34
- package/src/services/runtime/prompt.js +106 -89
- package/src/services/runtime/session.js +266 -72
- package/src/services/scheduler.js +26 -118
- package/src/services/task-dispatch.js +39 -0
- package/src/services/task-review.js +37 -0
- package/src/services/task-runs.js +46 -14
- package/src/services/wechat.js +23 -21
- package/src/subagent-ledger.js +165 -0
- package/src/wechat/commands.js +3 -1
- package/ui/app.js +4 -14
- package/ui/i18n/en/chat.js +11 -11
- package/ui/i18n/en/common.js +0 -12
- package/ui/i18n/en/nav.js +0 -4
- package/ui/i18n/en/settings.js +14 -9
- package/ui/i18n/en/tasks.js +0 -1
- package/ui/i18n/en/usage.js +4 -7
- package/ui/i18n/zh/chat.js +11 -11
- package/ui/i18n/zh/common.js +0 -12
- package/ui/i18n/zh/nav.js +0 -4
- package/ui/i18n/zh/settings.js +13 -9
- package/ui/i18n/zh/tasks.js +0 -1
- package/ui/i18n/zh/usage.js +4 -7
- package/ui/modules/cards.js +19 -5
- package/ui/modules/composer.js +16 -3
- package/ui/modules/i18n.js +10 -0
- package/ui/modules/modal-forms.js +15 -8
- package/ui/modules/settings.js +9 -0
- package/ui/modules/util.js +0 -2
- package/ui/styles/frame.css +0 -28
- package/ui/styles/wide.css +0 -3
|
@@ -242,6 +242,17 @@ export const runFinishMethods = {
|
|
|
242
242
|
if (this.rate[run.project_id]) delete this.rate[run.project_id]
|
|
243
243
|
return
|
|
244
244
|
}
|
|
245
|
+
if (run.kind === 'session_compact') {
|
|
246
|
+
// 闲时总结压缩(DSHBOTS-286):成功留痕即可——压缩 ≠ 结转,会话不换、不写
|
|
247
|
+
// rollover 槽;总结正文留在运行产出里,窗口标记已在入队时推进,失败由下一窗口再补。
|
|
248
|
+
this.insertRunEvent(
|
|
249
|
+
runId,
|
|
250
|
+
status === 'succeeded' && cleanOutput ? 'session_compact_done' : 'session_compact_failed',
|
|
251
|
+
status === 'succeeded' && cleanOutput ? '会话总结压缩完成,留原会话继续' : `会话总结压缩未生成:${error?.message ?? '模型未返回文本'}`,
|
|
252
|
+
)
|
|
253
|
+
if (this.rate[run.project_id]) delete this.rate[run.project_id]
|
|
254
|
+
return
|
|
255
|
+
}
|
|
245
256
|
if (run.kind === 'review' || run.kind === 'handle') {
|
|
246
257
|
const label = run.kind === 'review' ? '验收' : '受阻处理'
|
|
247
258
|
if (run.task_id) {
|
|
@@ -303,6 +314,8 @@ export const runFinishMethods = {
|
|
|
303
314
|
const task = this.taskRow(run.task_id)
|
|
304
315
|
const ownerReview = (task.review_target ?? 'user') === 'owner_bot'
|
|
305
316
|
let autoReviewQueued = false
|
|
317
|
+
// 子代理施工硬闸已解除(原 DSHBOTS-311):交付不再要求子代理施工账目,
|
|
318
|
+
// 施工方式由 bot 自选(亲做或派「bot 名+任务号」子代理),账本(D-294)仍照记。
|
|
306
319
|
this.store.transaction(() => {
|
|
307
320
|
this.store.run(
|
|
308
321
|
`UPDATE tasks SET status = 'in_review', version = version + 1, updated_at = ?
|
|
@@ -311,7 +324,9 @@ export const runFinishMethods = {
|
|
|
311
324
|
)
|
|
312
325
|
this.store.run(
|
|
313
326
|
'INSERT INTO task_comments(id, task_id, author_type, author_id, body, created_at) VALUES (?, ?, ?, ?, ?, ?)',
|
|
314
|
-
id('cmt'), task.id, 'bot', bot.id,
|
|
327
|
+
id('cmt'), task.id, 'bot', bot.id,
|
|
328
|
+
cleanOutput || '运行完成,但模型未返回文本。',
|
|
329
|
+
endedAt,
|
|
315
330
|
)
|
|
316
331
|
this.addTaskEvent(task.id, 'run_succeeded', { type: 'bot', id: bot.id }, { runId, identifier: `${project.key}-${task.number}` })
|
|
317
332
|
// 自动核验委派(DSHBOTS-113):与 in_review 迁移同一事务触发留痕;实际派生由
|
|
@@ -523,8 +538,8 @@ export const runFinishMethods = {
|
|
|
523
538
|
// ── Bot 群聊回复 mention 唤醒(DSHBOTS-252)────────────────────────────
|
|
524
539
|
// 与用户消息同语义(chat.ts sendMessage):@xxx 用 parseMentions 解析 active 成员、
|
|
525
540
|
// 显式 @ 优先(bot 路径无引用兜底道)、目标 slice(0,3);派发走既有 chat_run outbox →
|
|
526
|
-
// enqueueChatRun
|
|
527
|
-
//
|
|
541
|
+
// enqueueChatRun,串行车道经 DSHBOTS-289 收敛为 claimKey=bot:<bot>(bot 级串行),
|
|
542
|
+
// 不新建串行道。防循环三护栏:
|
|
528
543
|
// ① 禁自唤醒——解析结果剔除回复 bot 自身;
|
|
529
544
|
// ② 链路深度 ≤2——chatMentionChainDepth 回溯来源链,本回复链深达 2 即不再派发;
|
|
530
545
|
// ③ 任务卡双保险——taskId 非空的消息不参与派发(群聊 messages 表无 kind 列,
|
package/src/services/runs.js
CHANGED
|
@@ -16,6 +16,12 @@ import { isProviderUnavailableCode } from './model-fallback.js'
|
|
|
16
16
|
import { effectiveSandbox } from './runtime/prompt.js'
|
|
17
17
|
import { ensureAttentionSchema, parseAttention } from './attention-schema.js'
|
|
18
18
|
|
|
19
|
+
// 合批窗口与上限默认值(DSHBOTS-231/287,DSHBOTS-290 随语义回补从 scheduler.ts 迁来):
|
|
20
|
+
// 只合并入队时间相差 30 秒内的挂起消息,单组消息总数封顶 5;settings 可覆盖
|
|
21
|
+
// (coalesce_window_ms / coalesce_max_messages)。
|
|
22
|
+
const COALESCE_MAX_MESSAGES_DEFAULT = 5
|
|
23
|
+
const COALESCE_WINDOW_MS_DEFAULT = 30_000
|
|
24
|
+
|
|
19
25
|
// ── 只读运行工作区审计归因(DSHBOTS-94)─────────────────────────────────
|
|
20
26
|
// 指纹行形如 " M dir/file"(前两字符状态位 + 空格 + 路径);重命名 "R old -> new"
|
|
21
27
|
// 的新旧路径都算被触碰。带引号的 quoted 路径不解析(与产物清单同一口径,跳过)。
|
|
@@ -66,6 +72,273 @@ export function attributeWorkspaceChange({ before, after, concurrentWrites = []
|
|
|
66
72
|
|
|
67
73
|
export const runsMethods = {
|
|
68
74
|
|
|
75
|
+
// ── 争用口径 A(DSHBOTS-289,ADR §5.2-2,owner 拍板)─────────────────────────
|
|
76
|
+
// one_active_run_per_bot 激活后,bot 忙(有 queued/running 运行)时入站消息不落 runs 行
|
|
77
|
+
// (第二条 queued 会直接撞唯一索引),挂入按 bot 的待处理队列(settings 持久化,重启不丢);
|
|
78
|
+
// bot 空闲时 drainPendingMessages 把挂起消息建回运行——chat/solo_chat 同类合批成一条,
|
|
79
|
+
// dm/dm_reply 按一条来信一条回信契约逐条放行不合批;task/review 运行照常落 runs 行排队,
|
|
80
|
+
// 不被消息合批吞并(与本队列互斥串行,靠 claimKey=bot 的领取闸保证)。
|
|
81
|
+
// DSHBOTS-300:busy 闸是先查后插,多进程共享 team.sqlite 时两进程可同拍过检后各自
|
|
82
|
+
// INSERT,后者撞 one_active_run_per_bot——以唯一索引为串行化真值,命中即视为
|
|
83
|
+
// 「查后插之间 bot 转忙」,入队路径按争用口径 A 落挂起队列返 null(outbox 对 null
|
|
84
|
+
// 按「已消费/挂起」收单),绝不向上抛(直抛会被计为投递失败重试耗尽置 failed)。
|
|
85
|
+
// task_id 维度的 one_active_run_per_task 撞车不在内:重试下一拍 active 预检即自愈。
|
|
86
|
+
isRunBotUniqueViolation(error) {
|
|
87
|
+
const message = String(error?.message ?? error)
|
|
88
|
+
// DSHBOTS-308:索引改 (bot_id, COALESCE(project_id,'~solo')) 表达式后,撞车文本可能
|
|
89
|
+
// 只含 COALESCE 列名而不再含 runs.bot_id——放宽匹配并保持旧文本兼容;task 维度的
|
|
90
|
+
// one_active_run_per_task 撞车仍不在内(口径不变)。
|
|
91
|
+
return message.includes('UNIQUE')
|
|
92
|
+
&& (message.includes('runs.bot_id') || message.includes('one_active_run_per_bot')
|
|
93
|
+
|| message.includes('COALESCE(project_id'))
|
|
94
|
+
},
|
|
95
|
+
// DSHBOTS-304 同拍双投合并辅助:按幂等键 / task_id 找既有 active 运行(双投胜利方)。
|
|
96
|
+
activeRunByIdempotencyKey(key) {
|
|
97
|
+
return this.store.get(
|
|
98
|
+
"SELECT id FROM runs WHERE idempotency_key = ? AND status IN ('queued', 'running')",
|
|
99
|
+
key,
|
|
100
|
+
)?.id ?? null
|
|
101
|
+
},
|
|
102
|
+
activeRunByTaskId(taskId) {
|
|
103
|
+
return this.store.get(
|
|
104
|
+
"SELECT id FROM runs WHERE task_id = ? AND status IN ('queued', 'running')",
|
|
105
|
+
taskId,
|
|
106
|
+
)?.id ?? null
|
|
107
|
+
},
|
|
108
|
+
// DSHBOTS-304:挂起项是否已被既有运行覆盖或作废(true=drain 清扫时丢弃)。双投失败方的
|
|
109
|
+
// 挂起项与胜利方运行共存的两类恶果:消息族 drain 建回永撞全局 UNIQUE idempotency_key
|
|
110
|
+
// (任意状态)→返 null 永不消费→每 tick 队头 break 阻塞该 bot 全部挂起组;任务族幂等键
|
|
111
|
+
// 恒新会在原运行结束后重复建行。清扫口径:消息族=同幂等键运行已存在(胜利方已处理,
|
|
112
|
+
// 副本作废);task/owner_task=active 运行已在(跨进程合并消算)或任务已终态/不存在(快照作废)。
|
|
113
|
+
heldItemSuperseded(botId, item) {
|
|
114
|
+
if (item?.kind === 'task' || item?.kind === 'owner_task') {
|
|
115
|
+
if (this.activeRunByTaskId(item.taskId)) return true
|
|
116
|
+
const task = this.store.get('SELECT status FROM tasks WHERE id = ?', item.taskId)
|
|
117
|
+
return !task || task.status === 'done' || task.status === 'canceled'
|
|
118
|
+
}
|
|
119
|
+
const idem = item?.kind === 'chat' ? `chat:${item.messageId}:${botId}`
|
|
120
|
+
: item?.kind === 'solo_chat' ? `solo_chat:${item.messageId}`
|
|
121
|
+
: item?.kind === 'dm' ? `dm:${item.messageId}`
|
|
122
|
+
: item?.kind === 'dm_reply' ? `dm_reply:${item.messageId}` : null
|
|
123
|
+
if (!idem) return true
|
|
124
|
+
return Boolean(this.store.get('SELECT id FROM runs WHERE idempotency_key = ?', idem))
|
|
125
|
+
},
|
|
126
|
+
// DSHBOTS-308(owner 拍板跨工作区并行):bot 串行域从 bot 全局收窄为 (bot, 工作区),
|
|
127
|
+
// domain = project_id ?? '~solo',与 D-299 one_bot_binding 的 COALESCE 口径一致——
|
|
128
|
+
// 不同工作区的运行互不占位,可并行。
|
|
129
|
+
// 任务运行豁免(单 bot 多任务并行):任务在独立任务会话执行,不再占消息族的
|
|
130
|
+
// bot 车道——busy 判定只看非任务运行,chat/dm/solo 消息不必等任务跑完。
|
|
131
|
+
botBusy(botId, domain) {
|
|
132
|
+
return Boolean(this.store.get(
|
|
133
|
+
"SELECT id FROM runs WHERE bot_id = ? AND COALESCE(project_id, '~solo') = ? AND status IN ('queued', 'running') AND kind <> 'task'",
|
|
134
|
+
botId,
|
|
135
|
+
domain,
|
|
136
|
+
))
|
|
137
|
+
},
|
|
138
|
+
pendingMessagesKey(botId, domain) {
|
|
139
|
+
return `bot_pending_msgs:${botId}:${domain}`
|
|
140
|
+
},
|
|
141
|
+
holdPendingMessage(botId, domain, item) {
|
|
142
|
+
const key = this.pendingMessagesKey(botId, domain)
|
|
143
|
+
const list = jsonParse(this.store.setting(key), [])
|
|
144
|
+
const safe = Array.isArray(list) ? list : []
|
|
145
|
+
if (safe.some(existing => existing?.messageId === item.messageId)) return
|
|
146
|
+
this.store.setSetting(key, JSON.stringify([...safe, item]))
|
|
147
|
+
},
|
|
148
|
+
// 建回运行(drainPendingMessages 专用):直接落 runs 行(含合批 merged_messages),
|
|
149
|
+
// UNIQUE 撞车(并发领取窗口)返回 null 交由调用方把消息留队列下一拍重试。
|
|
150
|
+
insertHeldMessageRun(bot, group) {
|
|
151
|
+
const item = group.first
|
|
152
|
+
const mergedIds = group.merge.map(entry => entry.messageId)
|
|
153
|
+
const createdAt = nowIso()
|
|
154
|
+
const runId = id('run')
|
|
155
|
+
// 任务挂起项(DSHBOTS-290 busy 闸):按入队时快照建回写运行——列面与
|
|
156
|
+
// enqueueTaskRun 的直接 INSERT 一致(task_id/write_scopes/备用模型三列),
|
|
157
|
+
// UNIQUE 撞车(并发领取窗口)与消息族同口径返回 null 留队列。
|
|
158
|
+
if (item.kind === 'task') {
|
|
159
|
+
const routing = item.routing ?? {}
|
|
160
|
+
try {
|
|
161
|
+
this.store.run(
|
|
162
|
+
`INSERT INTO runs(id, project_id, bot_id, task_id, kind, mode, trigger_type, trigger_ref, status,
|
|
163
|
+
attempt, idempotency_key, write_scopes, model_provider, model_model, model_effort, queued_at)
|
|
164
|
+
VALUES (?, ?, ?, ?, 'task', 'write', ?, ?, 'queued', 1, ?, ?, ?, ?, ?, ?)`,
|
|
165
|
+
runId, item.projectId, bot.id, item.taskId, item.triggerType, item.taskId,
|
|
166
|
+
`task:${item.taskId}:${Date.now()}:${runId}`, item.writeScopes ?? '[]',
|
|
167
|
+
routing.provider ?? null, routing.model ?? null, routing.effort ?? null, createdAt,
|
|
168
|
+
)
|
|
169
|
+
} catch (error ) {
|
|
170
|
+
if (String(error).includes('UNIQUE')) return null
|
|
171
|
+
throw error
|
|
172
|
+
}
|
|
173
|
+
this.insertRunEvent(runId, 'queued', `bot 空闲,挂起任务建回运行(由 ${item.actorType}:${item.actorId} 触发)`)
|
|
174
|
+
if (routing.mode === 'fallback') {
|
|
175
|
+
this.insertRunEvent(runId, 'quota_fallback', `主 provider 额度耗尽,本次直接走备用模型 ${routing.ref ?? ''}(预计 ${routing.resetAt ?? '未知'} 恢复)`)
|
|
176
|
+
} else if (routing.mode === 'wait') {
|
|
177
|
+
this.insertRunEvent(runId, 'quota_wait', `主 provider 额度耗尽且无可用备用模型,排队等待至 ${routing.resetAt ?? '恢复'}`)
|
|
178
|
+
}
|
|
179
|
+
if (item.triggerType === 'board_resume') {
|
|
180
|
+
this.insertRunEvent(runId, 'board_resume', '看板拖入执行中,系统指令自动开工')
|
|
181
|
+
}
|
|
182
|
+
return runId
|
|
183
|
+
}
|
|
184
|
+
// owner 任务运行挂起项(review/handle/review_delegate,DSHBOTS-290 busy 闸):
|
|
185
|
+
// 只读任务运行,按入队时快照建回,列面与 enqueueOwnerTaskRun 一致。
|
|
186
|
+
if (item.kind === 'owner_task') {
|
|
187
|
+
try {
|
|
188
|
+
this.store.run(
|
|
189
|
+
`INSERT INTO runs(id, project_id, bot_id, task_id, kind, mode, trigger_type, trigger_ref, status,
|
|
190
|
+
attempt, idempotency_key, queued_at)
|
|
191
|
+
VALUES (?, ?, ?, ?, ?, 'read', ?, ?, 'queued', 1, ?, ?)`,
|
|
192
|
+
runId, item.projectId, bot.id, item.taskId, item.runKind, item.triggerType, item.taskId,
|
|
193
|
+
`${item.triggerType}:${item.taskId}:${Date.now()}:${runId}`, createdAt,
|
|
194
|
+
)
|
|
195
|
+
} catch (error ) {
|
|
196
|
+
if (String(error).includes('UNIQUE')) return null
|
|
197
|
+
throw error
|
|
198
|
+
}
|
|
199
|
+
this.insertRunEvent(runId, 'queued', 'bot 空闲,挂起的 owner 任务运行建回(验收/接管)')
|
|
200
|
+
return runId
|
|
201
|
+
}
|
|
202
|
+
const spec = item.kind === 'chat'
|
|
203
|
+
? { projectId: item.projectId, mode: 'read', trigger: item.origin === 'wechat' ? 'wechat' : 'mention', idem: `chat:${item.messageId}:${bot.id}` }
|
|
204
|
+
: item.kind === 'solo_chat'
|
|
205
|
+
? { projectId: null, mode: 'write', trigger: item.triggerType === 'wechat' ? 'wechat' : 'mention', idem: `solo_chat:${item.messageId}` }
|
|
206
|
+
: item.kind === 'dm'
|
|
207
|
+
? { projectId: item.projectId, mode: 'read', trigger: 'dm', idem: `dm:${item.messageId}` }
|
|
208
|
+
: { projectId: item.projectId, mode: 'read', trigger: 'dm_reply', idem: `dm_reply:${item.messageId}` }
|
|
209
|
+
try {
|
|
210
|
+
this.store.run(
|
|
211
|
+
`INSERT INTO runs(id, project_id, bot_id, kind, mode, trigger_type, trigger_ref, status,
|
|
212
|
+
attempt, idempotency_key, merged_messages, queued_at)
|
|
213
|
+
VALUES (?, ?, ?, ?, ?, ?, ?, 'queued', 1, ?, ?, ?)`,
|
|
214
|
+
runId, spec.projectId, bot.id, item.kind, spec.mode, spec.trigger, item.messageId,
|
|
215
|
+
spec.idem, mergedIds.length ? JSON.stringify(mergedIds) : null, createdAt,
|
|
216
|
+
)
|
|
217
|
+
} catch (error ) {
|
|
218
|
+
if (String(error).includes('UNIQUE')) return null
|
|
219
|
+
throw error
|
|
220
|
+
}
|
|
221
|
+
if (item.kind === 'dm') {
|
|
222
|
+
this.store.run(
|
|
223
|
+
"UPDATE bot_messages SET status = 'delivered', run_id = ? WHERE id = ?",
|
|
224
|
+
runId, item.messageId,
|
|
225
|
+
)
|
|
226
|
+
}
|
|
227
|
+
this.insertRunEvent(runId, 'queued',
|
|
228
|
+
`bot 空闲,待处理消息建回运行${mergedIds.length ? `(合批 ${mergedIds.length} 条)` : ''}`)
|
|
229
|
+
return runId
|
|
230
|
+
},
|
|
231
|
+
// D-272 用户提问判定(挂起消息向,DSHBOTS-290 语义回补):按触发消息的 sender_type
|
|
232
|
+
// 判定——chat 查 messages、solo_chat 查 solo_messages(单聊消息独立表)。用户发的消息
|
|
233
|
+
// 是「提问」,bot 触发(通知降级、@mention 唤醒、回信)不算。
|
|
234
|
+
// 注:卡面原文写「chat/solo_chat 查 messages」,实测单聊落 solo_messages 表,按实际表改;
|
|
235
|
+
// dm/dm_reply 查 bot_messages(DSHBOTS-291 覆盖补测实测)——该表无 sender_type 列且
|
|
236
|
+
// from_bot_id 恒为 bot(bot 间私信),不存在用户触发形态,恒为 false(逐条放行不参与合批,
|
|
237
|
+
// 该标记对 dm 组也无分组意义)。
|
|
238
|
+
heldItemIsUserQuestion(item) {
|
|
239
|
+
if (item?.kind === 'chat') {
|
|
240
|
+
const row = this.store.get('SELECT sender_type FROM messages WHERE id = ?', item.messageId)
|
|
241
|
+
return row?.sender_type === 'user'
|
|
242
|
+
}
|
|
243
|
+
if (item?.kind === 'solo_chat') {
|
|
244
|
+
const row = this.store.get('SELECT sender_type FROM solo_messages WHERE id = ?', item.messageId)
|
|
245
|
+
return row?.sender_type === 'user'
|
|
246
|
+
}
|
|
247
|
+
return false
|
|
248
|
+
},
|
|
249
|
+
// 争用口径 A 收口:bot 空闲时把挂起消息建回运行。chat/solo_chat 各取首条建运行、其余
|
|
250
|
+
// 同 kind 并入 merged_messages 同轮注入(合批成一条);dm/dm_reply 一次只放行一条(回信
|
|
251
|
+
// 契约),余量留队列下一拍。建行失败(并发撞 UNIQUE)的消息全部留队列,不丢消息。
|
|
252
|
+
// D-287 合批语义回补(DSHBOTS-290,ADR §2.3):①D-272 用户提问保护——用户提问不并入
|
|
253
|
+
// 非用户触发的挂起组(并入后提问只能挤在 Merged messages 段、没有直接回答锚点),提问
|
|
254
|
+
// 组单独成组且优先放行;②窗口 coalesce_window_ms(默认 30s)——与组首入队时间差超过
|
|
255
|
+
// 窗口不并;③上限 coalesce_max_messages(默认 5)——单组消息总数(首条+合并)封顶。
|
|
256
|
+
drainPendingMessages() {
|
|
257
|
+
const prefix = 'bot_pending_msgs:'
|
|
258
|
+
const max = Number(this.store.setting('coalesce_max_messages', String(COALESCE_MAX_MESSAGES_DEFAULT)))
|
|
259
|
+
// max<=1 = 关闭合批(与 D-287 入队合并同口径);非法值同样回退为不并。
|
|
260
|
+
const cap = Number.isFinite(max) && max > 1 ? max : 1
|
|
261
|
+
const windowMs = Number(this.store.setting('coalesce_window_ms', String(COALESCE_WINDOW_MS_DEFAULT)))
|
|
262
|
+
// DSHBOTS-308 旧键一次性迁移:308 前队列键无域段(bot_pending_msgs:<botId>),按
|
|
263
|
+
// item.projectId ?? '~solo' 并入对应域键后删旧键(先写后删、按 messageId 去重,中断
|
|
264
|
+
// 重跑幂等),防丢挂起消息——chat 项有 projectId、solo 项无=天然落 '~solo' 域。
|
|
265
|
+
for (const row of this.store.all(`SELECT key, value FROM settings WHERE key LIKE '${prefix}%'`)) {
|
|
266
|
+
const rest = row.key.slice(prefix.length)
|
|
267
|
+
if (rest.includes(':')) continue
|
|
268
|
+
const legacy = jsonParse(row.value, [])
|
|
269
|
+
if (Array.isArray(legacy)) {
|
|
270
|
+
for (const item of legacy) {
|
|
271
|
+
const domain = item?.projectId ?? '~solo'
|
|
272
|
+
const key = this.pendingMessagesKey(rest, domain)
|
|
273
|
+
const list = jsonParse(this.store.setting(key), [])
|
|
274
|
+
const safe = Array.isArray(list) ? list : []
|
|
275
|
+
if (!safe.some(existing => existing?.messageId === item?.messageId)) {
|
|
276
|
+
this.store.setSetting(key, JSON.stringify([...safe, item]))
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
}
|
|
280
|
+
this.store.run('DELETE FROM settings WHERE key = ?', row.key)
|
|
281
|
+
}
|
|
282
|
+
const rows = this.store.all(`SELECT key, value FROM settings WHERE key LIKE '${prefix}%'`)
|
|
283
|
+
for (const row of rows) {
|
|
284
|
+
const rest = row.key.slice(prefix.length)
|
|
285
|
+
const colon = rest.indexOf(':')
|
|
286
|
+
if (colon === -1) continue
|
|
287
|
+
const botId = rest.slice(0, colon)
|
|
288
|
+
const domain = rest.slice(colon + 1)
|
|
289
|
+
let items = jsonParse(row.value, [])
|
|
290
|
+
if (!Array.isArray(items) || !items.length) continue
|
|
291
|
+
// DSHBOTS-304 挂起项去重清扫(建回前预检):双投失败方挂起项与胜利方运行共存时——
|
|
292
|
+
// 消息族建回永撞全局 UNIQUE idempotency_key 成永生占位,任务族按恒新幂等键重复建行;
|
|
293
|
+
// 清扫在分组前完成,被覆盖/作废项直接消账,余项自然重组(同组合并不受影响)。
|
|
294
|
+
// 幂等键/task 维度与域无关(全局口径),保持现状。
|
|
295
|
+
const kept = items.filter(item => !this.heldItemSuperseded(botId, item))
|
|
296
|
+
if (kept.length !== items.length) {
|
|
297
|
+
this.store.setSetting(this.pendingMessagesKey(botId, domain), JSON.stringify(kept))
|
|
298
|
+
if (!kept.length) continue
|
|
299
|
+
items = kept
|
|
300
|
+
}
|
|
301
|
+
// DSHBOTS-308:busy 只看本域(bot, domain),他工作区队列本轮照常建回(并行)。
|
|
302
|
+
if (this.botBusy(botId, domain)) continue
|
|
303
|
+
const bot = this.store.get('SELECT * FROM bots WHERE id = ?', botId)
|
|
304
|
+
if (!bot || bot.status !== 'active') {
|
|
305
|
+
this.store.setSetting(this.pendingMessagesKey(botId, domain), '[]')
|
|
306
|
+
continue
|
|
307
|
+
}
|
|
308
|
+
// 相邻同 kind 的 chat/solo_chat 挂起消息合成一组(用户提问只并入同为用户触发的组、
|
|
309
|
+
// 窗口内、未超上限);dm/dm_reply/task 独立成组逐条放行。
|
|
310
|
+
const groups = []
|
|
311
|
+
for (const item of items) {
|
|
312
|
+
const last = groups[groups.length - 1]
|
|
313
|
+
const mergeableKind = item.kind === 'chat' || item.kind === 'solo_chat'
|
|
314
|
+
const withinWindow = Number.isFinite(windowMs)
|
|
315
|
+
&& Date.parse(item.queuedAt ?? '') - Date.parse(last?.first?.queuedAt ?? '') <= windowMs
|
|
316
|
+
if (last && last.first.kind === item.kind && mergeableKind
|
|
317
|
+
&& last.first.projectId === item.projectId
|
|
318
|
+
&& last.merge.length + 1 < cap
|
|
319
|
+
&& withinWindow
|
|
320
|
+
&& this.heldItemIsUserQuestion(item) === this.heldItemIsUserQuestion(last.first)) {
|
|
321
|
+
last.merge.push(item)
|
|
322
|
+
} else {
|
|
323
|
+
groups.push({ first: item, merge: [] })
|
|
324
|
+
}
|
|
325
|
+
}
|
|
326
|
+
// 用户提问组优先放行(D-272 出队优先的队列侧对偶),sort 稳定保住组间先后。
|
|
327
|
+
groups.sort((a, b) => Number(this.heldItemIsUserQuestion(b.first)) - Number(this.heldItemIsUserQuestion(a.first)))
|
|
328
|
+
const consumed = new Set()
|
|
329
|
+
for (const group of groups) {
|
|
330
|
+
if (this.botBusy(botId, domain)) break
|
|
331
|
+
const runId = this.insertHeldMessageRun(bot, group)
|
|
332
|
+
if (!runId) break
|
|
333
|
+
consumed.add(group.first.messageId)
|
|
334
|
+
for (const entry of group.merge) consumed.add(entry.messageId)
|
|
335
|
+
if (group.first.kind === 'dm' || group.first.kind === 'dm_reply') break
|
|
336
|
+
}
|
|
337
|
+
const leftover = items.filter(item => !consumed.has(item?.messageId))
|
|
338
|
+
this.store.setSetting(this.pendingMessagesKey(botId, domain), JSON.stringify(leftover))
|
|
339
|
+
}
|
|
340
|
+
},
|
|
341
|
+
|
|
69
342
|
// 建私信执行运行:目标 Bot 的 dm 会话收到来信,产出回信写入 bot_messages.reply。
|
|
70
343
|
enqueueDmRun(message, target) {
|
|
71
344
|
const existing = this.store.get(
|
|
@@ -73,14 +346,31 @@ export const runsMethods = {
|
|
|
73
346
|
message.id,
|
|
74
347
|
)
|
|
75
348
|
if (existing) return existing.id
|
|
349
|
+
// 争用口径 A:bot 忙时不落 runs 行,挂待处理队列(一条来信一条回信,不合批)。
|
|
350
|
+
// 域=dm 所属项目(项目外私信 project_id 为 NULL → '~solo',DSHBOTS-308)。
|
|
351
|
+
const domain = message.project_id ?? '~solo'
|
|
352
|
+
if (this.botBusy(target.id, domain)) {
|
|
353
|
+
this.holdPendingMessage(target.id, domain, { kind: 'dm', messageId: message.id, projectId: message.project_id, queuedAt: nowIso() })
|
|
354
|
+
return null
|
|
355
|
+
}
|
|
76
356
|
const runId = id('run')
|
|
77
357
|
const createdAt = nowIso()
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
358
|
+
try {
|
|
359
|
+
this.store.run(
|
|
360
|
+
`INSERT INTO runs(id, project_id, bot_id, kind, mode, trigger_type, trigger_ref, status,
|
|
361
|
+
attempt, idempotency_key, queued_at)
|
|
362
|
+
VALUES (?, ?, ?, 'dm', 'read', 'dm', ?, 'queued', 1, ?, ?)`,
|
|
363
|
+
runId, message.project_id, target.id, message.id, `dm:${message.id}`, createdAt,
|
|
364
|
+
)
|
|
365
|
+
} catch (error ) {
|
|
366
|
+
if (this.isRunBotUniqueViolation(error)) {
|
|
367
|
+
const merged = this.activeRunByIdempotencyKey(`dm:${message.id}`)
|
|
368
|
+
if (merged) return merged
|
|
369
|
+
this.holdPendingMessage(target.id, domain, { kind: 'dm', messageId: message.id, projectId: message.project_id, queuedAt: nowIso() })
|
|
370
|
+
return null
|
|
371
|
+
}
|
|
372
|
+
throw error
|
|
373
|
+
}
|
|
84
374
|
this.store.run(
|
|
85
375
|
"UPDATE bot_messages SET status = 'delivered', run_id = ? WHERE id = ?",
|
|
86
376
|
runId, message.id,
|
|
@@ -93,15 +383,17 @@ export const runsMethods = {
|
|
|
93
383
|
deliverBotReply(message) {
|
|
94
384
|
const caller = this.store.get('SELECT * FROM bots WHERE id = ?', message.from_bot_id)
|
|
95
385
|
if (!caller || caller.status !== 'active') return null
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
386
|
+
// 控制面收敛(DSHBOTS-289/299):回信目标按收敛顺序解析——来源任务未终态时按 runs 归属
|
|
387
|
+
// 亲和回原任务会话(steer/排运行都指向它),否则落发送方在 (bot, project) 维度的
|
|
388
|
+
// kind='bot' 控制面绑定(project_id IS 兼容项目外私信的 NULL)。
|
|
389
|
+
const originTask = message.origin_kind === 'task' && message.origin_task_id
|
|
390
|
+
? this.store.get('SELECT * FROM tasks WHERE id = ?', message.origin_task_id)
|
|
391
|
+
: null
|
|
392
|
+
const binding = (originTask ? this.runtime.taskAffinityBinding(originTask) : undefined)
|
|
393
|
+
?? this.store.get(
|
|
394
|
+
"SELECT * FROM session_bindings WHERE bot_id = ? AND kind = 'bot' AND alias = 0 AND project_id IS ?",
|
|
395
|
+
caller.id,
|
|
396
|
+
message.project_id ?? null,
|
|
105
397
|
)
|
|
106
398
|
// 无绑定(会话被轮换清理或首次私信):走 dm_reply 运行,由 startRun 重建会话。
|
|
107
399
|
let plan
|
|
@@ -132,13 +424,24 @@ export const runsMethods = {
|
|
|
132
424
|
// 幂等:outbox 重投时不能重复建运行,否则唯一索引会直接把重试打成失败。
|
|
133
425
|
const existingRun = this.store.get('SELECT id FROM runs WHERE idempotency_key = ?', `dm_reply:${message.id}`)
|
|
134
426
|
if (existingRun) return existingRun.id
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
427
|
+
try {
|
|
428
|
+
this.store.run(
|
|
429
|
+
`INSERT INTO runs(id, project_id, bot_id, task_id, kind, mode, trigger_type, trigger_ref, status,
|
|
430
|
+
attempt, idempotency_key, queued_at)
|
|
431
|
+
VALUES (?, ?, ?, NULL, 'dm_reply', 'read', 'dm_reply', ?, 'queued', 1, ?, ?)`,
|
|
432
|
+
runId, message.project_id, caller.id,
|
|
433
|
+
message.id, `dm_reply:${message.id}`, createdAt,
|
|
434
|
+
)
|
|
435
|
+
} catch (error ) {
|
|
436
|
+
if (this.isRunBotUniqueViolation(error)) {
|
|
437
|
+
const merged = this.activeRunByIdempotencyKey(`dm_reply:${message.id}`)
|
|
438
|
+
if (merged) return merged
|
|
439
|
+
const domain = message.project_id ?? '~solo'
|
|
440
|
+
this.holdPendingMessage(caller.id, domain, { kind: 'dm_reply', messageId: message.id, projectId: message.project_id, queuedAt: nowIso() })
|
|
441
|
+
return null
|
|
442
|
+
}
|
|
443
|
+
throw error
|
|
444
|
+
}
|
|
142
445
|
this.insertRunEvent(runId, 'queued', '私信回信排队投递给发送方会话')
|
|
143
446
|
return runId
|
|
144
447
|
},
|
|
@@ -151,14 +454,30 @@ export const runsMethods = {
|
|
|
151
454
|
bot.id,
|
|
152
455
|
)
|
|
153
456
|
if (existing) return existing.id
|
|
457
|
+
// 争用口径 A:bot 忙时不落 runs 行,挂待处理队列,空闲后由 drainPendingMessages 合批建回。
|
|
458
|
+
// solo_chat 运行 project_id 恒 NULL → 域恒 '~solo'(DSHBOTS-308)。
|
|
459
|
+
if (this.botBusy(bot.id, '~solo')) {
|
|
460
|
+
this.holdPendingMessage(bot.id, '~solo', { kind: 'solo_chat', messageId, triggerType, queuedAt: nowIso() })
|
|
461
|
+
return null
|
|
462
|
+
}
|
|
154
463
|
const runId = id('run')
|
|
155
464
|
const createdAt = nowIso()
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
465
|
+
try {
|
|
466
|
+
this.store.run(
|
|
467
|
+
`INSERT INTO runs(id, project_id, bot_id, kind, mode, trigger_type, trigger_ref, status,
|
|
468
|
+
attempt, idempotency_key, queued_at)
|
|
469
|
+
VALUES (?, NULL, ?, 'solo_chat', 'write', ?, ?, 'queued', 1, ?, ?)`,
|
|
470
|
+
runId, bot.id, triggerType, messageId, `solo_chat:${messageId}`, createdAt,
|
|
471
|
+
)
|
|
472
|
+
} catch (error ) {
|
|
473
|
+
if (this.isRunBotUniqueViolation(error)) {
|
|
474
|
+
const merged = this.activeRunByIdempotencyKey(`solo_chat:${messageId}`)
|
|
475
|
+
if (merged) return merged
|
|
476
|
+
this.holdPendingMessage(bot.id, '~solo', { kind: 'solo_chat', messageId, triggerType, queuedAt: nowIso() })
|
|
477
|
+
return null
|
|
478
|
+
}
|
|
479
|
+
throw error
|
|
480
|
+
}
|
|
162
481
|
this.store.run(
|
|
163
482
|
'INSERT INTO run_events(id, run_id, type, message, created_at) VALUES (?, ?, ?, ?, ?)',
|
|
164
483
|
id('evt'), runId, 'queued',
|
|
@@ -175,16 +494,32 @@ export const runsMethods = {
|
|
|
175
494
|
bot.id,
|
|
176
495
|
)
|
|
177
496
|
if (existing) return existing.id
|
|
497
|
+
// 争用口径 A:bot 忙时不落 runs 行,挂待处理队列,空闲后由 drainPendingMessages 合批建回。
|
|
498
|
+
// 域=chat 所属项目(DSHBOTS-308)。
|
|
499
|
+
if (this.botBusy(bot.id, project.id)) {
|
|
500
|
+
this.holdPendingMessage(bot.id, project.id, { kind: 'chat', messageId, projectId: project.id, origin, queuedAt: nowIso() })
|
|
501
|
+
return null
|
|
502
|
+
}
|
|
178
503
|
const runId = id('run')
|
|
179
504
|
const createdAt = nowIso()
|
|
180
505
|
// 微信直达(DSHBOTS-208):trigger_type 记 'wechat',运行收尾据此把 bot 回复推回微信管理员。
|
|
181
506
|
const triggerType = origin === 'wechat' ? 'wechat' : 'mention'
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
507
|
+
try {
|
|
508
|
+
this.store.run(
|
|
509
|
+
`INSERT INTO runs(id, project_id, bot_id, kind, mode, trigger_type, trigger_ref, status,
|
|
510
|
+
attempt, idempotency_key, queued_at)
|
|
511
|
+
VALUES (?, ?, ?, 'chat', 'read', ?, ?, 'queued', 1, ?, ?)`,
|
|
512
|
+
runId, project.id, bot.id, triggerType, messageId, `chat:${messageId}:${bot.id}`, createdAt,
|
|
513
|
+
)
|
|
514
|
+
} catch (error ) {
|
|
515
|
+
if (this.isRunBotUniqueViolation(error)) {
|
|
516
|
+
const merged = this.activeRunByIdempotencyKey(`chat:${messageId}:${bot.id}`)
|
|
517
|
+
if (merged) return merged
|
|
518
|
+
this.holdPendingMessage(bot.id, project.id, { kind: 'chat', messageId, projectId: project.id, origin, queuedAt: nowIso() })
|
|
519
|
+
return null
|
|
520
|
+
}
|
|
521
|
+
throw error
|
|
522
|
+
}
|
|
188
523
|
this.store.run(
|
|
189
524
|
'INSERT INTO run_events(id, run_id, type, message, created_at) VALUES (?, ?, ?, ?, ?)',
|
|
190
525
|
id('evt'), runId, 'queued', triggerType === 'wechat' ? '微信群聊消息触发(直达)' : '群聊消息触发', createdAt,
|