dsh-team-bots 0.2.364 → 0.2.393
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 +260 -72
- package/src/services/scheduler.js +26 -118
- package/src/services/task-dispatch.js +39 -0
- package/src/services/task-review.js +10 -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
|
@@ -7,7 +7,7 @@ import { randomUUID } from 'node:crypto'
|
|
|
7
7
|
import { AppError } from '../../errors.js'
|
|
8
8
|
import { id } from '../../util.js'
|
|
9
9
|
import { attachSessionToWorkspace, resolveSessionCwd, workspaceGroupTitle } from '../workspace-groups.js'
|
|
10
|
-
import { effectiveRunLimits, effectiveSessionRolloverTurns, rolloverPendingKey, rolloverSummaryKey } from '../../config.js'
|
|
10
|
+
import { effectiveRunLimits, effectiveSessionCompactTurns, effectiveSessionRolloverTurns, rolloverPendingKey, rolloverSummaryKey, sessionCompactMarkerKey } from '../../config.js'
|
|
11
11
|
import { imageContentPartsForMessage } from '../message-images.js'
|
|
12
12
|
import { restoreDefaultModel } from './model-restore.js'
|
|
13
13
|
import { applyBotToolPolicy } from '../../tools.js'
|
|
@@ -22,13 +22,41 @@ export const modelOverrideFor = (run) => (run?.model_provider && run?.model_mode
|
|
|
22
22
|
|
|
23
23
|
// 运行时效上限(DSHBOTS-131):任务派生运行(runs.task_id 非空)若任务声明了时效要求,
|
|
24
24
|
// 其超时上限取 min(全局 runTimeoutMs, 时效分钟数);无时效、非任务运行沿用全局值。
|
|
25
|
-
//
|
|
26
|
-
|
|
25
|
+
// DSHBOTS-302 预算分档:review/handle(owner_task 运行族)在全局上限内再收紧到 review
|
|
26
|
+
// 分档——director 单车道(one_active_run_per_bot)上验收长尾会堵住同 bot 的 chat 消息
|
|
27
|
+
// 运行,验收类运行不需要全局跑满;chat/dm/solo 沿用全局,task 时效取 min 逻辑不变。
|
|
28
|
+
export const runTimeoutMsForTaskRun = ({ run, task, globalRunTimeoutMs, reviewTimeoutMs = null }) => {
|
|
29
|
+
if ((run?.kind === 'review' || run?.kind === 'handle') && Number(reviewTimeoutMs) > 0) {
|
|
30
|
+
return Math.min(globalRunTimeoutMs, reviewTimeoutMs)
|
|
31
|
+
}
|
|
27
32
|
const minutes = Number(task?.timeBudgetMinutes ?? task?.time_budget_minutes)
|
|
28
33
|
if (!run?.task_id || !Number.isFinite(minutes) || minutes <= 0) return globalRunTimeoutMs
|
|
29
34
|
return Math.min(globalRunTimeoutMs, minutes * 60_000)
|
|
30
35
|
}
|
|
31
36
|
|
|
37
|
+
// review 分档分钟(DSHBOTS-302):env 显式 > settings 保存整数([3,60])> 默认 10,
|
|
38
|
+
// 与 effectiveRunLimits 同款生效链;env 值同样钳到范围,防误配。消费点在 startRun
|
|
39
|
+
// 启动瞬间参与定格,改值只影响之后启动的运行,进行中不追溯。
|
|
40
|
+
export const REVIEW_TIMEOUT_KEY = 'review_timeout_minutes'
|
|
41
|
+
export const REVIEW_TIMEOUT_MINUTES_DEFAULT = 10
|
|
42
|
+
export const REVIEW_TIMEOUT_MINUTES_RANGE = [3, 60]
|
|
43
|
+
|
|
44
|
+
export function effectiveReviewTimeoutMinutes(getSetting = () => null) {
|
|
45
|
+
const env = process.env.DSH_TEAM_REVIEW_TIMEOUT_MINUTES
|
|
46
|
+
if (env !== undefined && String(env).trim() !== '') {
|
|
47
|
+
const envMinutes = Number(env)
|
|
48
|
+
if (Number.isFinite(envMinutes) && envMinutes > 0) {
|
|
49
|
+
const [min, max] = REVIEW_TIMEOUT_MINUTES_RANGE
|
|
50
|
+
return Math.min(max, Math.max(min, Math.round(envMinutes)))
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
const [min, max] = REVIEW_TIMEOUT_MINUTES_RANGE
|
|
54
|
+
const raw = String(getSetting?.(REVIEW_TIMEOUT_KEY) ?? '')
|
|
55
|
+
const saved = Number(raw)
|
|
56
|
+
if (raw !== '' && Number.isInteger(saved) && saved >= min && saved <= max) return saved
|
|
57
|
+
return REVIEW_TIMEOUT_MINUTES_DEFAULT
|
|
58
|
+
}
|
|
59
|
+
|
|
32
60
|
export const sessionMethods = {
|
|
33
61
|
bindingOfSession(sessionId) {
|
|
34
62
|
if (!sessionId) return undefined
|
|
@@ -199,11 +227,14 @@ export const sessionMethods = {
|
|
|
199
227
|
return {}
|
|
200
228
|
}
|
|
201
229
|
},
|
|
202
|
-
// 结转判定(DSHBOTS-145):旧会话是否该让位。返回 true
|
|
203
|
-
// false 表示继续复用旧会话。
|
|
204
|
-
// - 阈值 0 = 关闭(配置见 config.js 的 sessionRolloverTurns);
|
|
230
|
+
// 结转判定(DSHBOTS-145 / DSHBOTS-286):旧会话是否该让位。返回 true 表示旧绑定已
|
|
231
|
+
// alias 化、调用方走新建路径;false 表示继续复用旧会话。
|
|
232
|
+
// - 阈值 0 = 关闭(配置见 config.js 的 sessionRolloverTurns,默认 512);
|
|
205
233
|
// - 「轮次」口径 = runs 表按 session_id 的 COUNT(*),不限 status——失败运行的提示词同样
|
|
206
234
|
// 已进会话历史,count-all 才与真实增长一致;
|
|
235
|
+
// - 幂等(DSHBOTS-286):alias 行永不触发结转——本判定只在活跃绑定上被调用
|
|
236
|
+
// (唯一调用点 ensureSession 以 !existing.alias 前置短路保证);已结转的旧会话
|
|
237
|
+
// 即使轮数继续增长(承接未终态任务流量),也只参与闲时压缩,不再二次结转;
|
|
207
238
|
// - 摘要就绪(settings 有值)→ 立即结转;
|
|
208
239
|
// - 摘要未就绪 → 幂等补排一条摘要运行(不阻塞对话,摘要就绪后下一次复用自然结转)。
|
|
209
240
|
shouldRolloverSession({ bot, project, existing }) {
|
|
@@ -248,13 +279,102 @@ export const sessionMethods = {
|
|
|
248
279
|
)
|
|
249
280
|
return runId
|
|
250
281
|
},
|
|
251
|
-
|
|
282
|
+
// 任务亲和绑定解析(DSHBOTS-286,ADR「结转后的任务亲和路由」):按 task_id + 当前负责人
|
|
283
|
+
// 从 runs 表显式解析其施工会话(负责人自己的 task 运行)——结转只把绑定行 alias 化,
|
|
284
|
+
// runs.task_id→session_id 的历史归属不动。
|
|
285
|
+
// 未终态任务的后续消息据此回到原会话(即使该绑定已是 alias 行,如结转后的旧控制面会话);
|
|
286
|
+
// 新任务(无 runs 归属)与终态任务不享亲和,落活跃绑定/新建路径。
|
|
287
|
+
// 与 D-285 单绑定模型共存:kind='bot' 唯一活跃绑定只决定「新流量」的家,任务亲和优先于它。
|
|
288
|
+
taskAffinityBinding(task) {
|
|
289
|
+
if (!task?.id) return undefined
|
|
290
|
+
// 终态任务的会话不再需要亲和:done/canceled 与调度器候选过滤同口径。
|
|
291
|
+
if (task.status === 'done' || task.status === 'canceled') return undefined
|
|
292
|
+
// 验收期话语权在总监(DSHBOTS-306):in_review 期间卡线程消息(验收指令/评论直通/
|
|
293
|
+
// dm_reply 回投等经此亲和的流量)改判项目 owner bot 的控制面绑定——否则按 assignee
|
|
294
|
+
// 亲和落承接 bot 会话,验收动作 team_review_task 又仅 owner bot 可调(D-190),指令
|
|
295
|
+
// 路由与动作权限错位、每卡都要绕一步。本分支优先于下方 runs 归属亲和(含 D-286 结转
|
|
296
|
+
// 后的 alias 旧会话):验收期短暂,按当前 owner 会话走;退回 todo 即恢复 assignee 亲和。
|
|
297
|
+
if (task.status === 'in_review') {
|
|
298
|
+
const ownerBotId = this.store.get('SELECT owner_bot_id FROM projects WHERE id = ?', task.project_id)?.owner_bot_id
|
|
299
|
+
if (!ownerBotId) return undefined
|
|
300
|
+
return this.store.get(
|
|
301
|
+
"SELECT * FROM session_bindings WHERE bot_id = ? AND project_id = ? AND kind = 'bot' AND alias = 0",
|
|
302
|
+
ownerBotId, task.project_id,
|
|
303
|
+
)
|
|
304
|
+
}
|
|
305
|
+
// runs 归属亲和只认「当前负责人的施工运行」(bot_id=assignee + kind='task'):
|
|
306
|
+
// 卡上的 owner 侧运行(review_delegate 核验/handle 受阻处置)虽带 task_id,但落的是
|
|
307
|
+
// 总监控制面会话——不过滤时退回重做后的重做运行与卡线程回信会按「最近一条运行」
|
|
308
|
+
// 亲和到总监会话(成员任务卡发给总监)。改派新负责人同理无旧归属,落新建任务会话。
|
|
309
|
+
if (!task.assignee_bot_id) return undefined
|
|
310
|
+
const row = this.store.get(
|
|
311
|
+
`SELECT session_id AS sid FROM runs
|
|
312
|
+
WHERE task_id = ? AND session_id IS NOT NULL AND bot_id = ? AND kind = 'task'
|
|
313
|
+
ORDER BY COALESCE(started_at, queued_at) DESC, rowid DESC LIMIT 1`,
|
|
314
|
+
task.id, task.assignee_bot_id,
|
|
315
|
+
)
|
|
316
|
+
if (!row?.sid) return undefined
|
|
317
|
+
return this.store.get('SELECT * FROM session_bindings WHERE session_id = ?', row.sid)
|
|
318
|
+
},
|
|
319
|
+
// 入队闲时总结压缩运行(DSHBOTS-286):复用旧会话(runs.session_id 指明)的只读运行,
|
|
320
|
+
// 让 bot 在原上下文里自写连续性总结。压缩 ≠ 结转:不 alias 化绑定、不写 rollover_pending,
|
|
321
|
+
// 总结完成后留原会话。幂等键带窗口号:同窗口并发/重复触发由唯一约束兜住。
|
|
322
|
+
enqueueSessionCompact({ bot, project, sessionId, window }) {
|
|
323
|
+
const runId = id('run')
|
|
324
|
+
const queuedAt = new Date().toISOString()
|
|
325
|
+
const inserted = this.store.run(
|
|
326
|
+
`INSERT OR IGNORE INTO runs(id, project_id, bot_id, kind, mode, trigger_type, trigger_ref, status, attempt, idempotency_key, session_id, queued_at)
|
|
327
|
+
VALUES (?, ?, ?, 'session_compact', 'read', 'session_compact', ?, 'queued', 1, ?, ?, ?)`,
|
|
328
|
+
runId,
|
|
329
|
+
project?.id ?? null,
|
|
330
|
+
bot.id,
|
|
331
|
+
sessionId,
|
|
332
|
+
`session_compact:${sessionId}:${window}`,
|
|
333
|
+
sessionId,
|
|
334
|
+
queuedAt,
|
|
335
|
+
)
|
|
336
|
+
if (!inserted.changes) return runId
|
|
337
|
+
this.store.run(
|
|
338
|
+
'INSERT INTO run_events(id, run_id, type, message, created_at) VALUES (?, ?, ?, ?, ?)',
|
|
339
|
+
id('evt'),
|
|
340
|
+
runId,
|
|
341
|
+
'queued',
|
|
342
|
+
`会话轮次跨过 ${window * effectiveSessionCompactTurns(this.config, key => this.store.setting(key))} 边界,闲时排队总结压缩`,
|
|
343
|
+
queuedAt,
|
|
344
|
+
)
|
|
345
|
+
return runId
|
|
346
|
+
},
|
|
347
|
+
// 闲时压缩判定(DSHBOTS-286):会话复用时检查轮次是否跨过新的压缩窗口。
|
|
348
|
+
// - 窗口 N 轮(session_compact_turns,0 = 关闭),轮次口径与结转一致;
|
|
349
|
+
// - 闲 = 该会话除当前运行外无 queued/running 运行;忙时推后(标记不动,下次复用再判,
|
|
350
|
+
// 跨多个窗口合批为一次);
|
|
351
|
+
// - 对仍在承接流量的会话生效:含 alias 旧会话(未终态任务流量)与 task/dm 短生命周期会话。
|
|
352
|
+
maybeCompactSession({ bot, project, existing, currentRunId }) {
|
|
353
|
+
const every = effectiveSessionCompactTurns(this.config, key => this.store.setting(key))
|
|
354
|
+
if (every <= 0 || !existing?.session_id) return
|
|
355
|
+
const sessionId = existing.session_id
|
|
356
|
+
const turns = this.store.get('SELECT COUNT(*) AS n FROM runs WHERE session_id = ?', sessionId)?.n ?? 0
|
|
357
|
+
const marker = Number(this.store.setting(sessionCompactMarkerKey(sessionId)) ?? 0)
|
|
358
|
+
const window = Math.floor(turns / every)
|
|
359
|
+
if (window <= Math.floor(marker / every)) return
|
|
360
|
+
const busy = this.store.get(
|
|
361
|
+
"SELECT id FROM runs WHERE session_id = ? AND status IN ('queued', 'running') AND id != ?",
|
|
362
|
+
sessionId,
|
|
363
|
+
currentRunId ?? '',
|
|
364
|
+
)
|
|
365
|
+
if (busy) return
|
|
366
|
+
this.enqueueSessionCompact({ bot, project, sessionId, window })
|
|
367
|
+
// 标记推进到窗口下界:即使压缩运行后续失败也不回退,失败留痕由下一窗口再补。
|
|
368
|
+
this.store.setSetting(sessionCompactMarkerKey(sessionId), String(window * every))
|
|
369
|
+
},
|
|
370
|
+
async ensureSession(bot, project, { run, task, modelOverride } = {} ) {
|
|
252
371
|
// DSHBOTS-238:成员 bot 工具层兜底——隐藏总监专权工具(子代理生成工具由 preset 全局
|
|
253
372
|
// 注册,成员默认可见)。仅作用于项目绑定会话;solo 无项目上下文,本策略自然不触发。
|
|
254
373
|
const isOwner = Boolean(project && bot && project.owner_bot_id === bot.id)
|
|
255
|
-
// 结转摘要运行(DSHBOTS-145
|
|
256
|
-
//
|
|
257
|
-
|
|
374
|
+
// 结转摘要运行(DSHBOTS-145)与闲时压缩运行(DSHBOTS-286):必须回到原会话执行,
|
|
375
|
+
// 且不能参与阈值判定——否则这两类运行自己又会触发结转/压缩;solo 摘要运行也没有
|
|
376
|
+
// 项目上下文,不查项目。
|
|
377
|
+
if ((run?.kind === 'rollover_summary' || run?.kind === 'session_compact') && run.session_id) {
|
|
258
378
|
const resolved = await this.ctx.sessionController.resolveAgent(run.session_id)
|
|
259
379
|
if (!resolved?.error) {
|
|
260
380
|
this.applyFilePolicy(resolved, bot, run, task)
|
|
@@ -264,107 +384,168 @@ export const sessionMethods = {
|
|
|
264
384
|
}
|
|
265
385
|
this.retireBinding(run.session_id)
|
|
266
386
|
}
|
|
267
|
-
//
|
|
268
|
-
//
|
|
269
|
-
//
|
|
270
|
-
const
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
387
|
+
// ② 任务亲和(DSHBOTS-286/289,ADR §5.2-3):未终态任务按 runs 归属回原会话——含
|
|
388
|
+
// dm_reply 经 sessionTaskFor 解析出的任务(executeRun 已把任务行透传进来)与结转后
|
|
389
|
+
// alias 行上的旧任务流量;终态任务不享亲和,落下方控制面单绑定。
|
|
390
|
+
const affinity = this.taskAffinityBinding(task)
|
|
391
|
+
if (affinity) {
|
|
392
|
+
const resolved = await this.ctx.sessionController.resolveAgent(affinity.session_id)
|
|
393
|
+
if (!resolved?.error) {
|
|
394
|
+
this.applyFilePolicy(resolved, bot, run, task)
|
|
395
|
+
applyBotToolPolicy(resolved, { isOwner, logger: this.logger })
|
|
396
|
+
await this.selectModel(affinity.session_id, bot, modelOverride)
|
|
397
|
+
// 任务会话同样在承接流量:跨过压缩窗口且闲时排队总结压缩
|
|
398
|
+
// (结转仍豁免——任务会话生命周期短,DSHBOTS-286 口径不变)。
|
|
399
|
+
this.maybeCompactSession({ bot, project, existing: affinity, currentRunId: run?.id })
|
|
400
|
+
return affinity.session_id
|
|
401
|
+
}
|
|
402
|
+
this.retireBinding(affinity.session_id)
|
|
403
|
+
}
|
|
404
|
+
// ②½ 任务专属会话(单 bot 多任务并行):施工运行(kind='task')无亲和会话时(首次
|
|
405
|
+
// 开工)不开进控制面绑定,而是建一张「bot 名+任务号」专属会话——每张卡一个独立
|
|
406
|
+
// 上下文,同 bot 多卡并行互不串味;返工/续做的后续运行经上方 ② 任务亲和回到本会话。
|
|
407
|
+
if (run?.kind === 'task' && task && project) {
|
|
408
|
+
return this.ensureTaskSession(bot, project, { run, task, modelOverride })
|
|
409
|
+
}
|
|
410
|
+
// ③ 控制面会话收敛(DSHBOTS-289/299,ADR §5.2-3):其余运行(chat/dm/dm_reply/
|
|
411
|
+
// review/handle/vision 及无任务上下文的派生运行)一律落该 bot 在所属域的 kind='bot'
|
|
412
|
+
// 控制面会话——项目运行落 (bot, project) 绑定(每项目每 bot 一条,DSHBOTS-299 三分域),
|
|
413
|
+
// solo_chat/项目外流量落 (bot, NULL) 绑定即该 bot 专属工作区 solo 会话;ensureBotBinding
|
|
414
|
+
// 是唯一建行点。
|
|
415
|
+
return this.ensureBotBinding(bot, project, { run, task, modelOverride })
|
|
416
|
+
},
|
|
417
|
+
// 控制面会话兜底与唯一建行点(DSHBOTS-285 落机制,289 生产接线,299 会话域三分,
|
|
418
|
+
// ADR §4.1/§5.2-3):ensureSession 收敛顺序的终点——rollover/compact 复用、任务亲和
|
|
419
|
+
// 之外的运行一律解析到该 bot 在 (bot, project) 维度的 kind='bot' 控制面绑定;已有则复用
|
|
420
|
+
// (宿主失效或结转时退役重建)。project 为 NULL 即 solo 域:绑定行 project_id 亦为 NULL,
|
|
421
|
+
// 会话 cwd 落该 bot 专属工作区(resolveSessionCwd 的 solo 分支),与项目控制面双域分离。
|
|
422
|
+
// 复用分支补齐与旧 ensureSession 同款的策略面:applyFilePolicy / applyBotToolPolicy /
|
|
423
|
+
// selectModel / maybeCompactSession / rolloverPending 消费闸。
|
|
424
|
+
// 任务专属会话(单 bot 多任务并行):每张施工卡一张「bot 名+任务号」会话——会话列表
|
|
425
|
+
// 显示名 `${bot.name} · ${project.key}-${task.number}`(D-315 命名口径),绑定行 kind='task'
|
|
426
|
+
// 带 task_id(任务亲和解析与 one_bot_binding 的 kind='bot' 谓词互不干扰)。生命周期短:
|
|
427
|
+
// 不参与结转(会话轮次上限照旧兜底),压缩由亲和复用分支的 maybeCompactSession 承接。
|
|
428
|
+
// 并发首建去重键带 task 维度,同任务并发首建共用同一 in-flight Promise。
|
|
429
|
+
async ensureTaskSession(bot, project, { run, task, modelOverride } = {} ) {
|
|
430
|
+
const isOwner = Boolean(project && bot && project.owner_bot_id === bot.id)
|
|
431
|
+
const key = `${bot.id}:${project.id}:task:${task.id}`
|
|
432
|
+
if (this.sessionCreates.has(key)) return this.sessionCreates.get(key)
|
|
433
|
+
const creating = (async () => {
|
|
434
|
+
const cwd = await resolveSessionCwd(this.config, { solo: false, bot, project })
|
|
435
|
+
const created = await this.ctx.sessionController.create({
|
|
436
|
+
cwd,
|
|
437
|
+
...(await this.defaultAgentPreset('project')),
|
|
438
|
+
})
|
|
439
|
+
const sessionId = created.sessionId
|
|
440
|
+
const agent = await this.ctx.sessionController.resolveAgent(sessionId)
|
|
441
|
+
if (agent?.error) throw new AppError('SESSION_FAILED', `创建 DSH Session 失败:${agent.error.message ?? '未知错误'}`)
|
|
442
|
+
if (this.ctx.sessionTitle?.rename) {
|
|
443
|
+
const liveSession = agent?.agent?.session ?? agent?.session
|
|
444
|
+
if (liveSession) {
|
|
445
|
+
try { this.ctx.sessionTitle.rename(liveSession, `${bot.name} · ${project.key}-${task.number}`) } catch {}
|
|
446
|
+
}
|
|
447
|
+
}
|
|
448
|
+
this.applyFilePolicy(agent, bot, run, task)
|
|
449
|
+
applyBotToolPolicy(agent, { isOwner, logger: this.logger })
|
|
450
|
+
this.store.run(
|
|
451
|
+
`INSERT INTO session_bindings(session_id, bot_id, project_id, kind, task_id, created_at, alias)
|
|
452
|
+
VALUES (?, ?, ?, 'task', ?, ?, 0)
|
|
453
|
+
ON CONFLICT(session_id) DO NOTHING`,
|
|
454
|
+
sessionId, bot.id, project.id, task.id, new Date().toISOString(),
|
|
283
455
|
)
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
456
|
+
await this.selectModel(sessionId, bot, modelOverride)
|
|
457
|
+
// 挂到项目工作区分组(与控制面会话同组):任务会话在宿主列表里归入所属项目。
|
|
458
|
+
const registry = this.ctx.get?.('workspaceRegistry')
|
|
459
|
+
if (registry) {
|
|
460
|
+
await attachSessionToWorkspace(registry, {
|
|
461
|
+
cwd,
|
|
462
|
+
title: workspaceGroupTitle({ kind: 'bot', project, bot }),
|
|
463
|
+
sessionId,
|
|
464
|
+
}, this.logger)
|
|
465
|
+
}
|
|
466
|
+
return sessionId
|
|
467
|
+
})()
|
|
468
|
+
this.sessionCreates.set(key, creating)
|
|
469
|
+
try {
|
|
470
|
+
return await creating
|
|
471
|
+
} finally {
|
|
472
|
+
this.sessionCreates.delete(key)
|
|
473
|
+
}
|
|
474
|
+
},
|
|
475
|
+
async ensureBotBinding(bot, project, { run, task, modelOverride } = {} ) {
|
|
476
|
+
const isOwner = Boolean(project && bot && project.owner_bot_id === bot.id)
|
|
477
|
+
// DSHBOTS-299:查询带 project 维度(IS 比 = 多兼容 NULL——solo 域按 (bot, NULL) 命中),
|
|
478
|
+
// 同一 bot 跨项目各自独立成会话,项目流量不再被单条全局绑定吸走。
|
|
479
|
+
const existing = this.store.get(
|
|
480
|
+
"SELECT * FROM session_bindings WHERE bot_id = ? AND kind = 'bot' AND alias = 0 AND project_id IS ?",
|
|
481
|
+
bot.id,
|
|
482
|
+
project?.id ?? null,
|
|
483
|
+
)
|
|
295
484
|
let rolledOver = false
|
|
296
485
|
if (existing) {
|
|
297
486
|
const resolved = await this.ctx.sessionController.resolveAgent(existing.session_id)
|
|
298
487
|
if (!resolved?.error) {
|
|
299
|
-
//
|
|
300
|
-
//
|
|
301
|
-
//
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
&& this.shouldRolloverSession({ bot, project, existing })
|
|
305
|
-
rolledOver = rollover
|
|
306
|
-
if (!rollover) {
|
|
488
|
+
// 结转(DSHBOTS-145):控制面会话长期承接全量流量,照旧阈值判定——摘要就绪即
|
|
489
|
+
// alias 化落到下方重建路径,摘要由新会话首轮提示词注入;未就绪补排摘要运行,
|
|
490
|
+
// 本轮继续复用。alias 行不可能走到这里(查询条件 alias=0),幂等由查询保证。
|
|
491
|
+
rolledOver = this.shouldRolloverSession({ bot, project, existing })
|
|
492
|
+
if (!rolledOver) {
|
|
307
493
|
this.applyFilePolicy(resolved, bot, run, task)
|
|
308
494
|
applyBotToolPolicy(resolved, { isOwner, logger: this.logger })
|
|
309
495
|
await this.selectModel(existing.session_id, bot, modelOverride)
|
|
496
|
+
// 闲时压缩(DSHBOTS-286):复用即「仍在承接流量」,跨过窗口且闲时排队总结压缩。
|
|
497
|
+
this.maybeCompactSession({ bot, project, existing, currentRunId: run?.id })
|
|
310
498
|
return existing.session_id
|
|
311
499
|
}
|
|
312
500
|
}
|
|
313
|
-
//
|
|
314
|
-
// 落到下方新建路径(DSHBOTS-256:别名保留旧 session 的 bot/项目归属)。
|
|
501
|
+
// 会话已失效(宿主查不到)或结转判定要求开新会话:旧绑定降级为授权别名(DSHBOTS-256)。
|
|
315
502
|
this.retireBinding(existing.session_id)
|
|
316
503
|
}
|
|
317
|
-
|
|
504
|
+
// DSHBOTS-299:建行去重键同样带 project 维度——同 bot 在 A 项目与 solo 域并发首建
|
|
505
|
+
// 互不挤兑,也不共用同一条 in-flight Promise(solo 会话 cwd 与项目会话不同)。
|
|
506
|
+
const key = `${bot.id}:${project?.id ?? 'solo'}:bot`
|
|
318
507
|
if (this.sessionCreates.has(key)) return this.sessionCreates.get(key)
|
|
319
508
|
const creating = (async () => {
|
|
509
|
+
const solo = !project
|
|
320
510
|
const cwd = await resolveSessionCwd(this.config, { solo, bot, project })
|
|
321
511
|
const created = await this.ctx.sessionController.create({
|
|
322
512
|
cwd,
|
|
323
|
-
...(await this.defaultAgentPreset(
|
|
513
|
+
...(await this.defaultAgentPreset('project')),
|
|
324
514
|
})
|
|
325
515
|
const sessionId = created.sessionId
|
|
326
516
|
const agent = await this.ctx.sessionController.resolveAgent(sessionId)
|
|
327
517
|
if (agent?.error) throw new AppError('SESSION_FAILED', `创建 DSH Session 失败:${agent.error.message ?? '未知错误'}`)
|
|
328
518
|
if (this.ctx.sessionTitle?.rename) {
|
|
329
|
-
const title = kind === 'task'
|
|
330
|
-
? `${bot.name} · ${project.key}-${task.number}`
|
|
331
|
-
: kind === 'dm'
|
|
332
|
-
? `${bot.name} · ${project.name} · DM`
|
|
333
|
-
: solo
|
|
334
|
-
? `${bot.name} · 单聊`
|
|
335
|
-
: `${bot.name} · ${project.name}`
|
|
336
|
-
// rename 只接受活动 agent 的 session 对象;没有就不改标题,
|
|
337
|
-
// 不再传 sessionId 字符串兜底(宿主会抛 "not live in this store")。
|
|
338
|
-
// rename 是同步接口(返回标题快照而非 Promise),失败不得阻断会话创建。
|
|
339
519
|
const liveSession = agent?.agent?.session ?? agent?.session
|
|
340
520
|
if (liveSession) {
|
|
341
|
-
|
|
521
|
+
// 会话列表显示名(DSHBOTS-315):bot 级会话后缀取 8 位 uuid 前缀——
|
|
522
|
+
// 「控制面」在中文语境是生造词,用户看不懂(英文 controller 无妨)。
|
|
523
|
+
try { this.ctx.sessionTitle.rename(liveSession, `${bot.name} · ${sessionId.slice(0, 8)}`) } catch {}
|
|
342
524
|
}
|
|
343
525
|
}
|
|
344
526
|
this.applyFilePolicy(agent, bot, run, task)
|
|
345
527
|
applyBotToolPolicy(agent, { isOwner, logger: this.logger })
|
|
346
528
|
this.store.run(
|
|
347
529
|
`INSERT INTO session_bindings(session_id, bot_id, project_id, kind, task_id, created_at, alias)
|
|
348
|
-
VALUES (?, ?, ?,
|
|
530
|
+
VALUES (?, ?, ?, 'bot', NULL, ?, 0)
|
|
349
531
|
ON CONFLICT(session_id) DO NOTHING`,
|
|
350
532
|
sessionId,
|
|
351
533
|
bot.id,
|
|
352
|
-
|
|
353
|
-
kind,
|
|
354
|
-
taskId,
|
|
534
|
+
project?.id ?? null,
|
|
355
535
|
new Date().toISOString(),
|
|
356
536
|
)
|
|
357
537
|
// 结转消费闸(DSHBOTS-213):新会话成为摘要的唯一注入目标——否则旧会话在摘要就绪
|
|
358
538
|
// 前后被非结转路径顶替(如宿主重启失效)时,替身会话的普通轮次会把摘要提前消费掉。
|
|
359
|
-
if (rolledOver) this.store.setSetting(rolloverPendingKey(bot.id,
|
|
539
|
+
if (rolledOver) this.store.setSetting(rolloverPendingKey(bot.id, project?.id ?? null), sessionId)
|
|
360
540
|
await this.selectModel(sessionId, bot, modelOverride)
|
|
361
|
-
// 挂到 DSH
|
|
362
|
-
//
|
|
541
|
+
// 挂到 DSH 原生工作区分组(可选增强):控制面会话落在项目工作区时归项目组
|
|
542
|
+
// (solo 无项目上下文,cwd 已是 bot 专属目录,天然一组,无需挂载)。
|
|
543
|
+
// registry 经 ctx.get 读取,宿主未装就整体跳过;挂载失败只在 workspace-groups 内 warn。
|
|
363
544
|
const registry = this.ctx.get?.('workspaceRegistry')
|
|
364
|
-
if (registry) {
|
|
545
|
+
if (registry && project) {
|
|
365
546
|
await attachSessionToWorkspace(registry, {
|
|
366
547
|
cwd,
|
|
367
|
-
title: workspaceGroupTitle({ kind, project, bot }),
|
|
548
|
+
title: workspaceGroupTitle({ kind: 'bot', project, bot }),
|
|
368
549
|
sessionId,
|
|
369
550
|
}, this.logger)
|
|
370
551
|
}
|
|
@@ -386,7 +567,8 @@ export const sessionMethods = {
|
|
|
386
567
|
const effectiveBot = modelOverride
|
|
387
568
|
? { ...bot, provider: modelOverride.provider, model: modelOverride.model, effort: modelOverride.effort ?? null }
|
|
388
569
|
: bot
|
|
389
|
-
|
|
570
|
+
// sessionKind 入参保留兼容(调用方仍传),控制面收敛后不再参与会话解析。
|
|
571
|
+
const sessionId = await this.ensureSession(bot, project, { run, task, modelOverride })
|
|
390
572
|
let prompt = this.promptFor({ run, bot, project, task, chatMessage, extraMessages, sessionId })
|
|
391
573
|
// recovery note(DSHBOTS-135):恢复标记已被上面的首条提示词消费,即刻清零——
|
|
392
574
|
// 恢复提醒只对「重排队后的第一次执行」注入一次,后续运行回到标准提示词。
|
|
@@ -419,8 +601,14 @@ export const sessionMethods = {
|
|
|
419
601
|
this.pendingByRun.set(run.id, pending)
|
|
420
602
|
// 运行时效在启动瞬间定格:之后页面改值只影响后续启动的运行,进行中不追溯。
|
|
421
603
|
// 任务时效要求(DSHBOTS-131)在全局上限内再收紧:到期未完成即超时中止。
|
|
422
|
-
|
|
423
|
-
|
|
604
|
+
// review/handle 再走分档收紧(DSHBOTS-302):min(全局, review_timeout_minutes)。
|
|
605
|
+
const limits = effectiveRunLimits(this.config, key => this.store.setting(key))
|
|
606
|
+
pending.runTimeoutMs = runTimeoutMsForTaskRun({
|
|
607
|
+
run,
|
|
608
|
+
task,
|
|
609
|
+
globalRunTimeoutMs: limits.runTimeoutMs,
|
|
610
|
+
reviewTimeoutMs: effectiveReviewTimeoutMinutes(key => this.store.setting(key)) * 60_000,
|
|
611
|
+
})
|
|
424
612
|
pending.timer = setTimeout(() => {
|
|
425
613
|
void this.timeoutRun(run.id)
|
|
426
614
|
}, pending.runTimeoutMs)
|