dsh-team-bots 0.3.79 → 0.3.98
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/cordis.patch.yml +100 -0
- package/index.js +5 -2
- package/package.json +1 -1
- package/src/api.js +5 -1
- package/src/db-migrations.js +3 -0
- package/src/db.js +53 -0
- package/src/runtime.js +13 -1
- package/src/service.js +5 -0
- package/src/services/approvals.js +7 -2
- package/src/services/bots.js +8 -1
- package/src/services/chat-stores.js +564 -0
- package/src/services/chat.js +60 -28
- package/src/services/core.js +30 -10
- package/src/services/feishu.js +1 -1
- package/src/services/memory.js +14 -12
- package/src/services/model-fallback.js +3 -1
- package/src/services/notify.js +18 -8
- package/src/services/projects.js +25 -19
- package/src/services/questions.js +2 -1
- package/src/services/review-watchdog.js +31 -12
- package/src/services/run-finish.js +17 -13
- package/src/services/runs.js +29 -41
- package/src/services/runtime/context-window.js +6 -4
- package/src/services/runtime/lifecycle.js +1 -0
- package/src/services/runtime/prompt.js +21 -9
- package/src/services/runtime/sandbox-policy.js +1 -1
- package/src/services/runtime/session.js +22 -13
- package/src/services/runtime/trajectory.js +3 -1
- package/src/services/scheduler.js +63 -54
- package/src/services/system.js +9 -7
- package/src/services/task-dispatch.js +28 -21
- package/src/services/task-review.js +34 -25
- package/src/services/task-runs.js +40 -21
- package/src/services/tasks.js +34 -18
- package/src/services/terminal.js +25 -14
- package/src/services/vision-proxy.js +9 -6
- package/src/services/wechat.js +1 -1
- package/src/subagent-ledger.js +23 -6
- package/src/usage-reconcile.js +93 -32
- package/src/usage.js +125 -50
- package/ui/i18n/en/settings.js +1 -0
- package/ui/i18n/zh/settings.js +1 -0
- package/ui/modules/modal-forms.js +8 -8
- package/ui/modules/tasks.js +1 -1
- package/ui/styles/cards.css +13 -0
- package/ui/styles/modal.css +2 -0
|
@@ -11,7 +11,7 @@ import { MAX_RATE_LIMIT_RETRIES, isQuotaExhausted, isRetryChainError } from './b
|
|
|
11
11
|
import { INTERNAL_FALLBACK_KINDS } from './model-fallback.js'
|
|
12
12
|
import { ensureTaskRelationsSchema, unsatisfiedBlockedDeps } from './task-relations.js'
|
|
13
13
|
import { TASK_BUDGET_ROUNDS, TRANSITIONS, taskDeadlineInfo } from './tasks.js'
|
|
14
|
-
import { effectiveWriteScopes, writeScopesOverlap } from './write-scopes.js'
|
|
14
|
+
import { effectiveWriteScopes, readWriteScopes, writeScopesOverlap } from './write-scopes.js'
|
|
15
15
|
import { runReviewWatchdog } from './review-watchdog.js'
|
|
16
16
|
import { pollGithubSources } from './github-source.js'
|
|
17
17
|
import { staleCardHit } from './notify.js'
|
|
@@ -229,7 +229,7 @@ export const schedulerMethods = {
|
|
|
229
229
|
const reviewRunId = this.enqueueMemoryReview(project, bot, task, sourceRun.id)
|
|
230
230
|
if (!reviewRunId) {
|
|
231
231
|
// 记忆审查未启用:按规则沉淀兜底,保证任务交付有沉淀。
|
|
232
|
-
this.learnFromTask(project, bot, task, sourceRun
|
|
232
|
+
this.learnFromTask(project, bot, task, this.runOutputOf(sourceRun) ?? '', sourceRun.id)
|
|
233
233
|
}
|
|
234
234
|
return reviewRunId
|
|
235
235
|
}
|
|
@@ -340,9 +340,13 @@ export const schedulerMethods = {
|
|
|
340
340
|
<= CAST(strftime('%s', 'now') AS REAL)
|
|
341
341
|
AND CAST(strftime('%s', COALESCE(r.ended_at, r.queued_at)) AS REAL) + r.retry_after_ms / 1000.0
|
|
342
342
|
> CAST(strftime('%s', 'now') AS REAL) - ${PLAN_RETRY_MAX_LATE_SECONDS}
|
|
343
|
-
|
|
343
|
+
`,
|
|
344
344
|
)
|
|
345
345
|
for (const run of expired) {
|
|
346
|
+
// 幂等闸(Phase 3 起事件住聊天库):已派生过的运行跳过。
|
|
347
|
+
if (this.runChatStore(run).get(
|
|
348
|
+
"SELECT 1 AS ok FROM run_events WHERE run_id = ? AND type = 'plan_retry'", run.id,
|
|
349
|
+
)) continue
|
|
346
350
|
// DSHBOTS-266:重试链判定(限流码 ∪ 网络瞬态)在 JS 做;非链内错误码不派生(现状不变)。
|
|
347
351
|
if (!isRetryChainError({ code: run.error_code, message: run.error_message })) continue
|
|
348
352
|
// DSHBOTS-99:额度耗尽(已达到限额)的运行不再自动派生重试——额度窗口用尽时重打主模型
|
|
@@ -370,11 +374,8 @@ export const schedulerMethods = {
|
|
|
370
374
|
runId, run.project_id, run.bot_id, run.task_id, run.id, run.task_id,
|
|
371
375
|
(run.attempt ?? 1) + 1, `plan_retry:${run.id}`, run.write_scopes, createdAt,
|
|
372
376
|
)
|
|
373
|
-
this.
|
|
374
|
-
|
|
375
|
-
id('evt'), run.id, 'plan_retry',
|
|
376
|
-
`限流重试到期,派生新运行 ${runId}(attempt ${(run.attempt ?? 1) + 1})`, createdAt,
|
|
377
|
-
)
|
|
377
|
+
this.insertRunEvent(run.id, 'plan_retry',
|
|
378
|
+
`限流重试到期,派生新运行 ${runId}(attempt ${(run.attempt ?? 1) + 1})`)
|
|
378
379
|
} catch (error ) {
|
|
379
380
|
// idempotency_key UNIQUE 撞车 = 另一个 tick 已派生过,跳过即可。
|
|
380
381
|
if (String(error).includes('UNIQUE')) continue
|
|
@@ -395,15 +396,13 @@ export const schedulerMethods = {
|
|
|
395
396
|
runId,
|
|
396
397
|
)
|
|
397
398
|
if (Number(changed?.changes ?? 0) !== 1) return false
|
|
398
|
-
const
|
|
399
|
+
const run = this.store.get('SELECT id, project_id, bot_id FROM runs WHERE id = ?', runId)
|
|
400
|
+
const alreadyNoted = run && this.runChatStore(run).get(
|
|
399
401
|
"SELECT 1 AS ok FROM run_events WHERE run_id = ? AND type = 'stale_superseded'",
|
|
400
402
|
runId,
|
|
401
403
|
)
|
|
402
404
|
if (!alreadyNoted) {
|
|
403
|
-
this.
|
|
404
|
-
'INSERT INTO run_events(id, run_id, type, message, created_at) VALUES (?, ?, ?, ?, ?)',
|
|
405
|
-
id('evt'), runId, 'stale_superseded', '任务已结束,排队运行被清理(SUPERSEDED)', nowIso(),
|
|
406
|
-
)
|
|
405
|
+
this.insertRunEvent(runId, 'stale_superseded', '任务已结束,排队运行被清理(SUPERSEDED)')
|
|
407
406
|
}
|
|
408
407
|
return true
|
|
409
408
|
},
|
|
@@ -484,14 +483,11 @@ export const schedulerMethods = {
|
|
|
484
483
|
"UPDATE runs SET status = 'canceled', ended_at = ?, error_code = 'TASK_DEADLINE', error_message = ? WHERE id = ? AND status = 'queued'",
|
|
485
484
|
now, body, runId,
|
|
486
485
|
)
|
|
487
|
-
this.
|
|
488
|
-
|
|
489
|
-
id('evt'), runId, 'task_deadline', `整卡死线已到(${deadlineIso}),拒绝启动新施工运行`, now,
|
|
490
|
-
)
|
|
491
|
-
this.store.run(
|
|
486
|
+
this.insertRunEvent(runId, 'task_deadline', `整卡死线已到(${deadlineIso}),拒绝启动新施工运行`)
|
|
487
|
+
this.store.afterCommit(() => this.groupChatStore(project.id).run(
|
|
492
488
|
'INSERT INTO task_comments(id, task_id, author_type, author_id, body, created_at) VALUES (?, ?, ?, ?, ?, ?)',
|
|
493
489
|
commentId, task.id, 'system', 'system', body, now,
|
|
494
|
-
)
|
|
490
|
+
))
|
|
495
491
|
this.addTaskEvent(task.id, 'commented', { type: 'system', id: 'system' }, { commentId, deadline: deadlineIso, runId })
|
|
496
492
|
})
|
|
497
493
|
if (TRANSITIONS[task.status]?.has('blocked')) {
|
|
@@ -522,6 +518,8 @@ export const schedulerMethods = {
|
|
|
522
518
|
// 有工作或窗口到期则照常全量。提醒类扫描(滞留卡/验收守门)与到期重试派生在慢拍粒度
|
|
523
519
|
// (默认 15s)下运行,阈值均为分钟级,无感;入队路径的事件唤醒 tick() 与测试直调不经此门。
|
|
524
520
|
tickIdleGate() {
|
|
521
|
+
// 已停机不再探库:停机后底层库已关闭,这里的读会抛错并变成未处理的 rejection。
|
|
522
|
+
if (this.stopped || !this.started) return null
|
|
525
523
|
const idleMs = Number(this.config.schedulerIdleIntervalMs ?? 15_000)
|
|
526
524
|
if (Date.now() - this.lastFullTickAt < idleMs) {
|
|
527
525
|
const probe = this.store.get(
|
|
@@ -536,7 +534,7 @@ export const schedulerMethods = {
|
|
|
536
534
|
},
|
|
537
535
|
|
|
538
536
|
async tick() {
|
|
539
|
-
if (this.ticking || !this.started) return
|
|
537
|
+
if (this.ticking || !this.started || this.stopped) return
|
|
540
538
|
this.ticking = true
|
|
541
539
|
this.lastFullTickAt = Date.now()
|
|
542
540
|
try {
|
|
@@ -571,20 +569,25 @@ export const schedulerMethods = {
|
|
|
571
569
|
// claimKey 串行域 = (bot, 工作区)(DSHBOTS-308):键只读 bot_id/project_id(均在列面),
|
|
572
570
|
// 在跑侧与候选侧算的是同一 run 行的同一列,串行不失效(承 DSHBOTS-219 口径)。
|
|
573
571
|
`SELECT r.bot_id, r.kind, r.task_id, r.project_id, r.mode, r.write_scopes, r.trigger_type,
|
|
574
|
-
t.status AS task_status
|
|
572
|
+
t.status AS task_status, t.ignored_write_scopes AS task_ignored_scopes
|
|
575
573
|
FROM runs r LEFT JOIN tasks t ON t.id = r.task_id
|
|
576
574
|
WHERE r.status = 'running'`,
|
|
577
575
|
)
|
|
578
576
|
const activeKeys = new Set(activeRows.map(row => this.claimKey(row)))
|
|
579
|
-
// 同项目在跑的写运行:按写范围判定能否并行。写范围为空 = 未声明 =
|
|
577
|
+
// 同项目在跑的写运行:按写范围判定能否并行。写范围为空 = 未声明 = 保守串行;
|
|
578
|
+
// 染色例外(命中 .gitignore):写范围只剩染色条目的任务运行不参与任何锁——
|
|
579
|
+
// 染色路径没有写保护价值,即使与别的任务写同一文件也放行并行。
|
|
580
580
|
const activeWriteScopes = new Map()
|
|
581
581
|
for (const row of activeRows) {
|
|
582
582
|
if (row.mode !== 'write' || !row.project_id) continue
|
|
583
583
|
// 任务不在「进行中」就不占写槽(DSHBOTS-73):写占用随任务离开进行中即时释放。
|
|
584
584
|
if (!runHoldsWriteLock({ kind: row.kind, mode: row.mode, taskStatus: row.task_status })) continue
|
|
585
|
+
const effective = effectiveWriteScopes(row.write_scopes)
|
|
586
|
+
// 全染色运行不进冲突名单:与任何候选(含未声明候选)互不阻塞。
|
|
587
|
+
if (!effective.length && readWriteScopes(row.task_ignored_scopes).length) continue
|
|
585
588
|
const list = activeWriteScopes.get(row.project_id) ?? []
|
|
586
589
|
// package.json 是版本钩子机械改动,比对前剥掉(DSHBOTS-133),否则全队列判交叠串行。
|
|
587
|
-
list.push(
|
|
590
|
+
list.push(effective)
|
|
588
591
|
activeWriteScopes.set(row.project_id, list)
|
|
589
592
|
}
|
|
590
593
|
let activeTotal = activeRows.length
|
|
@@ -603,21 +606,18 @@ export const schedulerMethods = {
|
|
|
603
606
|
// 候选过滤(DSHBOTS-110):任务已终态(done/canceled)的 queued 任务运行不再领取;
|
|
604
607
|
// 只排 kind='task',非任务运行不受影响。LEFT JOIN 出 NULL 的孤儿任务运行不过滤。
|
|
605
608
|
const candidates = this.store.all(
|
|
606
|
-
`SELECT r
|
|
607
|
-
|
|
609
|
+
`SELECT r.*, t.priority AS task_priority, t.ignored_write_scopes AS task_ignored_scopes
|
|
610
|
+
FROM runs r LEFT JOIN tasks t ON t.id = r.task_id
|
|
608
611
|
WHERE r.status = 'queued'
|
|
609
612
|
AND NOT (r.kind = 'task' AND COALESCE(t.status, '') IN ('done', 'canceled'))
|
|
610
|
-
|
|
611
|
-
WHEN r.kind = 'chat' AND m.sender_type = 'bot' THEN 2
|
|
612
|
-
WHEN r.kind <> 'task' OR t.priority IS NULL THEN 1
|
|
613
|
-
WHEN t.priority = 'urgent' THEN 0
|
|
614
|
-
WHEN t.priority = 'high' THEN 1
|
|
615
|
-
WHEN t.priority = 'medium' THEN 2
|
|
616
|
-
WHEN t.priority = 'low' THEN 3
|
|
617
|
-
ELSE 4
|
|
618
|
-
END, r.queued_at
|
|
619
|
-
LIMIT 50`,
|
|
613
|
+
LIMIT 200`,
|
|
620
614
|
)
|
|
615
|
+
// 出队定档移到 JS:分库后 messages 住各聊天库(顶库 messages 已空,
|
|
616
|
+
// 旧 LEFT JOIN 恒 NULL,DSHBOTS-272 的机器流量档自 Phase 1 起失效)——
|
|
617
|
+
// 群聊运行改按触发消息的 sender_type 实测定档,任务优先级档照旧。
|
|
618
|
+
for (const run of candidates) run.claim_tier = this.claimTierOf(run)
|
|
619
|
+
candidates.sort((a, b) =>
|
|
620
|
+
(a.claim_tier - b.claim_tier) || String(a.queued_at ?? '').localeCompare(String(b.queued_at ?? '')))
|
|
621
621
|
for (const run of candidates) {
|
|
622
622
|
const human = isHumanRun(run)
|
|
623
623
|
if (activeTotal >= maxRuns + (human && !humanSlotTaken ? 1 : 0)) break
|
|
@@ -629,8 +629,13 @@ export const schedulerMethods = {
|
|
|
629
629
|
// 单聊运行 project_id 为空,写锁落在各自独立的 _the_bots/<dir>,不占项目锁。
|
|
630
630
|
// 候选侧同样先剥 package.json(DSHBOTS-133):存量 queued 运行快照可能带它,
|
|
631
631
|
// 过滤后任一为空仍按「未声明」保守串行,不降低既有安全边界。
|
|
632
|
+
// 染色例外(命中 .gitignore):候选只剩染色条目时不参与锁,即使与在跑写运行
|
|
633
|
+
// 写同一文件也放行——染色路径没有写保护价值。
|
|
632
634
|
const writeScopes = run.mode === 'write' ? effectiveWriteScopes(run.write_scopes) : []
|
|
633
|
-
|
|
635
|
+
const ignoredOnly = run.mode === 'write'
|
|
636
|
+
&& !writeScopes.length
|
|
637
|
+
&& readWriteScopes(run.task_ignored_scopes).length > 0
|
|
638
|
+
if (run.mode === 'write' && run.project_id && !ignoredOnly) {
|
|
634
639
|
const runningScopes = activeWriteScopes.get(run.project_id) ?? []
|
|
635
640
|
if (runningScopes.some(other => writeScopesOverlap(writeScopes, other))) continue
|
|
636
641
|
}
|
|
@@ -660,7 +665,7 @@ export const schedulerMethods = {
|
|
|
660
665
|
if (INTERNAL_FALLBACK_KINDS.has(run.kind) && !run.model_provider) {
|
|
661
666
|
const quota = this.botQuotaState(run.bot_id, bot.provider)
|
|
662
667
|
if (quota?.exhausted) {
|
|
663
|
-
const alreadyNoted = this.
|
|
668
|
+
const alreadyNoted = this.runChatStore(run).get(
|
|
664
669
|
"SELECT 1 AS ok FROM run_events WHERE run_id = ? AND type = 'quota_wait'",
|
|
665
670
|
run.id,
|
|
666
671
|
)
|
|
@@ -687,18 +692,14 @@ export const schedulerMethods = {
|
|
|
687
692
|
const taskRow = this.store.get('SELECT * FROM tasks WHERE id = ?', run.task_id)
|
|
688
693
|
const blockers = unsatisfiedBlockedDeps(this.store, taskRow)
|
|
689
694
|
if (blockers.length) {
|
|
690
|
-
const alreadyNoted = this.
|
|
695
|
+
const alreadyNoted = this.runChatStore(run).get(
|
|
691
696
|
"SELECT 1 AS ok FROM run_events WHERE run_id = ? AND type = 'task_blocked_dep'",
|
|
692
697
|
run.id,
|
|
693
698
|
)
|
|
694
699
|
if (!alreadyNoted) {
|
|
695
700
|
const projectKeyRow = this.store.get('SELECT key FROM projects WHERE id = ?', run.project_id)
|
|
696
|
-
this.
|
|
697
|
-
|
|
698
|
-
id('evt'), run.id, 'task_blocked_dep',
|
|
699
|
-
`依赖未完结,暂缓领取:${blockers.map(row => `${projectKeyRow?.key ?? 'T'}-${row.number}`).join('、')}`,
|
|
700
|
-
nowIso(),
|
|
701
|
-
)
|
|
701
|
+
this.insertRunEvent(run.id, 'task_blocked_dep',
|
|
702
|
+
`依赖未完结,暂缓领取:${blockers.map(row => `${projectKeyRow?.key ?? 'T'}-${row.number}`).join('、')}`)
|
|
702
703
|
}
|
|
703
704
|
continue
|
|
704
705
|
}
|
|
@@ -723,10 +724,7 @@ export const schedulerMethods = {
|
|
|
723
724
|
// 借用了人的消息保底槽(超出并发上限放行):记事件并占住额外槽,下一次归还前不再放第二条。
|
|
724
725
|
if (human && activeTotal >= maxRuns) {
|
|
725
726
|
humanSlotTaken = true
|
|
726
|
-
this.
|
|
727
|
-
'INSERT INTO run_events(id, run_id, type, message, created_at) VALUES (?, ?, ?, ?, ?)',
|
|
728
|
-
id('evt'), run.id, 'human_slot', '并发已满,借人的消息保底槽放行', nowIso(),
|
|
729
|
-
)
|
|
727
|
+
this.insertRunEvent(run.id, 'human_slot', '并发已满,借人的消息保底槽放行')
|
|
730
728
|
}
|
|
731
729
|
activeTotal += 1
|
|
732
730
|
activeKeys.add(claimKey)
|
|
@@ -739,10 +737,7 @@ export const schedulerMethods = {
|
|
|
739
737
|
list.push(writeScopes)
|
|
740
738
|
activeWriteScopes.set(run.project_id, list)
|
|
741
739
|
if (parallel) {
|
|
742
|
-
this.
|
|
743
|
-
'INSERT INTO run_events(id, run_id, type, message, created_at) VALUES (?, ?, ?, ?, ?)',
|
|
744
|
-
id('evt'), run.id, 'run_parallel', '写范围与同项目在跑的写运行不重叠,并行放行', nowIso(),
|
|
745
|
-
)
|
|
740
|
+
this.insertRunEvent(run.id, 'run_parallel', '写范围与同项目在跑的写运行不重叠,并行放行')
|
|
746
741
|
}
|
|
747
742
|
}
|
|
748
743
|
if (run.task_id && run.kind === 'task') {
|
|
@@ -769,12 +764,26 @@ export const schedulerMethods = {
|
|
|
769
764
|
runEventDays: Number(this.store.setting('retention_run_event_days', '0') || 0),
|
|
770
765
|
messageDays: Number(this.store.setting('retention_message_days', '0') || 0),
|
|
771
766
|
reconciliationDays: Number(this.store.setting('retention_reconciliation_days', '30') || 0),
|
|
772
|
-
})
|
|
773
|
-
const reconciliation = reconcileUsage(this.store, { days: 30, expiredBefore: retention.usage.before })
|
|
767
|
+
}, this.allChatStores())
|
|
768
|
+
const reconciliation = reconcileUsage(this.store, { days: 30, expiredBefore: retention.usage.before, chatStoreFor: run => this.runChatStore(run) })
|
|
774
769
|
const reapedMessages = this.reapStuckMessages()
|
|
775
770
|
return { reconciliation, retention, reapedMessages }
|
|
776
771
|
},
|
|
777
772
|
|
|
773
|
+
// 领取定档(DSHBOTS-272 口径,分库修正):任务运行按任务优先级(urgent 先),
|
|
774
|
+
// 其余运行视同 high;群聊运行的触发消息来自 bot(@mention 派发)时降到机器流量档,
|
|
775
|
+
// 同车道内用户提问(tier 1)必然先于它领取。sender_type 住聊天库,按运行归属实测。
|
|
776
|
+
claimTierOf(run) {
|
|
777
|
+
if (run.kind === 'chat') {
|
|
778
|
+
const sender = run.project_id && run.trigger_ref
|
|
779
|
+
? this.groupChatStore(run.project_id).get('SELECT sender_type FROM messages WHERE id = ?', run.trigger_ref)?.sender_type
|
|
780
|
+
: null
|
|
781
|
+
return sender === 'bot' ? 2 : 1
|
|
782
|
+
}
|
|
783
|
+
if (run.kind !== 'task' || !run.task_priority) return 1
|
|
784
|
+
return { urgent: 0, high: 1, medium: 2, low: 3 }[run.task_priority] ?? 4
|
|
785
|
+
},
|
|
786
|
+
|
|
778
787
|
// 私信卡死回收:消息停在 pending 说明入队或投递链路断了。
|
|
779
788
|
// 没有 outbox 行的补一条,终态 failed 的行重置后重排,
|
|
780
789
|
// 免得一条私信永远停在「已入库但没人投递」的状态。
|
package/src/services/system.js
CHANGED
|
@@ -89,8 +89,9 @@ const systemMethodsBase = {
|
|
|
89
89
|
// runs 不含 updated_at:新运行看行数、启动/结束看 started_at/ended_at;
|
|
90
90
|
// last_progress_at 的巡检心跳不进签名——它不改变视图载荷,计入会重新引入高频失效。
|
|
91
91
|
sig(`SELECT COUNT(*) AS n, COALESCE(MAX(queued_at), '') || COALESCE(MAX(started_at), '') || COALESCE(MAX(ended_at), '') AS m FROM runs`),
|
|
92
|
-
|
|
93
|
-
|
|
92
|
+
// 聊天正文已分库(chat-db-split Phase 1):messages/solo_messages 的签名由各聊天库
|
|
93
|
+
// 的本连接累计变更数承接,任一库的写都会推进版本;读路径不触库。
|
|
94
|
+
...this.chatVersionParts(),
|
|
94
95
|
// bot_messages 的 status/reply 变更不推 rowid,靠 replied_at 捕捉。
|
|
95
96
|
sig(`SELECT COUNT(*) AS n, COALESCE(MAX(COALESCE(replied_at, created_at)), '') AS m FROM bot_messages`),
|
|
96
97
|
stamp('memories', 'updated_at'),
|
|
@@ -236,7 +237,8 @@ const systemMethodsBase = {
|
|
|
236
237
|
if (project) {
|
|
237
238
|
const group = this.groupOf(project.id)
|
|
238
239
|
view.project = this.projectView(project)
|
|
239
|
-
|
|
240
|
+
// 聊天正文住项目聊天库(chat-db-split Phase 1)。
|
|
241
|
+
const messages = this.groupChatStore(project.id).all(
|
|
240
242
|
`SELECT * FROM (SELECT * FROM messages WHERE project_id = ? ORDER BY created_at DESC LIMIT 200) ORDER BY created_at ASC`,
|
|
241
243
|
project.id,
|
|
242
244
|
)
|
|
@@ -286,11 +288,11 @@ const systemMethodsBase = {
|
|
|
286
288
|
// 归档契约(DSHBOTS-174):默认视图(归档窗口交集,或两维全关时的 200 条回退上限)隐去的
|
|
287
289
|
// 更早消息数。被隐去的都严格早于窗口最旧一条,UI 据此点亮「加载更早」入口,经 messagesPage
|
|
288
290
|
// 游标必可翻回;无隐去时 count=0 / hasMore=false,入口不出现。
|
|
289
|
-
const soloWindow = soloVisibleMessages(this.
|
|
291
|
+
const soloWindow = soloVisibleMessages(this.soloChatStore(soloBot.id), soloBot.id, {
|
|
290
292
|
days: this.config.dmArchiveDays,
|
|
291
293
|
keep: this.config.dmKeepRecent,
|
|
292
294
|
})
|
|
293
|
-
const soloTotal = Number(this.
|
|
295
|
+
const soloTotal = Number(this.soloChatStore(soloBot.id).get('SELECT COUNT(*) AS count FROM solo_messages WHERE bot_id = ?', soloBot.id)?.count ?? 0)
|
|
294
296
|
const archivedCount = Math.max(0, soloTotal - soloWindow.length)
|
|
295
297
|
// 单聊卡片条目(DSHBOTS-218):提问/审批在单聊会话里发起时落 solo_messages 卡片锚点,
|
|
296
298
|
// 状态查 questions/approvals 表(project_id 为空的那批)。
|
|
@@ -432,7 +434,7 @@ const systemMethodsBase = {
|
|
|
432
434
|
// 单聊分页与默认视图同源取数(DSHBOTS-171):翻页不过归档窗口,被归档的更早消息
|
|
433
435
|
// 仍可见——「归档」只是默认视图隐去,不是删除。群聊保持原口径,不受私信归档影响。
|
|
434
436
|
if (isSolo) {
|
|
435
|
-
const page = soloVisibleMessages(this.
|
|
437
|
+
const page = soloVisibleMessages(this.soloChatStore(soloBotId), soloBotId, { beforeCreatedAt, beforeId, limit: pageSize })
|
|
436
438
|
return {
|
|
437
439
|
messages: page.messages.map(row => this.soloMessageView(row)),
|
|
438
440
|
hasMore: page.hasMore,
|
|
@@ -440,7 +442,7 @@ const systemMethodsBase = {
|
|
|
440
442
|
}
|
|
441
443
|
// 游标口径:取「严格早于游标」的最近 pageSize 条(第 pageSize+1 条仅作 hasMore 探位,不进结果)。
|
|
442
444
|
// 先按 DESC 取回、slice 掉探位行、再翻回 ASC——顺序不能反,否则会丢掉紧邻游标的那条。
|
|
443
|
-
const rows = this.
|
|
445
|
+
const rows = this.groupChatStore(projectId).all(
|
|
444
446
|
`SELECT * FROM messages
|
|
445
447
|
WHERE project_id = ? AND (created_at < ? OR (created_at = ? AND id < ?))
|
|
446
448
|
ORDER BY created_at DESC, id DESC
|
|
@@ -66,15 +66,15 @@ export const taskDispatchMethods = {
|
|
|
66
66
|
},
|
|
67
67
|
// 无主卡领取开工的写范围声明闸门(DSHBOTS-270):与 assignFromBot「派单必须声明写
|
|
68
68
|
// 范围」同口径的预判——卡面清单优先,空则从描述「写范围:」行兜底解析
|
|
69
|
-
// (parseWriteScopesFromDescription
|
|
70
|
-
//
|
|
69
|
+
// (parseWriteScopesFromDescription)。声明非空即放行:有效范围进并行锁;
|
|
70
|
+
// 命中 .gitignore 的条目进染色清单(ignored_write_scopes),不参与锁也不触发拒绝。
|
|
71
71
|
assertClaimScopes(task, project) {
|
|
72
72
|
const declared = readWriteScopes(task.write_scopes)
|
|
73
|
-
const
|
|
73
|
+
const { kept, removed } = filterIgnoredWriteScopes(
|
|
74
74
|
declared.length ? declared : parseWriteScopesFromDescription(task.description),
|
|
75
75
|
this.projectWorkDir(project),
|
|
76
|
-
)
|
|
77
|
-
if (
|
|
76
|
+
)
|
|
77
|
+
if (kept.length || removed.length) return
|
|
78
78
|
throw badRequest(
|
|
79
79
|
`领取开工必须声明写范围:任务 ${project.key}-${task.number} 未声明写范围,`
|
|
80
80
|
+ '其写运行会与同项目其他写运行全部保守串行,一条卡即可堵死队列。'
|
|
@@ -309,17 +309,18 @@ export const taskDispatchMethods = {
|
|
|
309
309
|
}
|
|
310
310
|
return null
|
|
311
311
|
},
|
|
312
|
-
//
|
|
313
|
-
// createTask
|
|
314
|
-
//
|
|
315
|
-
//
|
|
316
|
-
//
|
|
317
|
-
//
|
|
312
|
+
// 派单清洗三入口共用——createTask(owner 派单路径权威清洗)、assignFromBot(经
|
|
313
|
+
// createTask 同一清洗)、taskUpdateScopes(改卡入口)。两步:
|
|
314
|
+
// ① ignored 染色——命中项目 .gitignore 的条目没有写保护价值,不进 write_scopes
|
|
315
|
+
// (并行锁只认有效范围),但保留进 ignored_write_scopes 供卡面展示:全染色卡
|
|
316
|
+
// 不再触发「必须声明写范围」拒绝,也不与任何写运行串行(同文件也并行)。审计落
|
|
317
|
+
// task.scopes_normalize(ignored=[...])。非 git 目录 / git 不可用时保守放行不清洗。
|
|
318
318
|
// ② 一级目录驳回——清洗后仍不含 / 的条目(src、tests 这类整目录声明)直接拒绝:
|
|
319
319
|
// 整目录会把同项目全部写运行判为冲突锁死并发,这不是清洗能救的,必须改卡。
|
|
320
|
+
// 染色条目不参与该闸(它们不进锁,无锁死风险)。
|
|
320
321
|
normalizeDispatchScopes(project, scopes, { actor, taskId }) {
|
|
321
322
|
const { kept, removed } = filterIgnoredWriteScopes(scopes, this.projectWorkDir(project))
|
|
322
|
-
if (removed.length) this.audit(actor, 'task.scopes_normalize', 'task', taskId, { removed })
|
|
323
|
+
if (removed.length) this.audit(actor, 'task.scopes_normalize', 'task', taskId, { ignored: removed })
|
|
323
324
|
const bare = kept.filter(scope => !scope.includes('/'))
|
|
324
325
|
if (bare.length) {
|
|
325
326
|
throw badRequest(
|
|
@@ -327,7 +328,7 @@ export const taskDispatchMethods = {
|
|
|
327
328
|
+ '整目录声明会锁死同项目全部写运行。',
|
|
328
329
|
)
|
|
329
330
|
}
|
|
330
|
-
return kept
|
|
331
|
+
return { scopes: kept, ignored: removed }
|
|
331
332
|
},
|
|
332
333
|
async assignFromBot(sessionId, input) {
|
|
333
334
|
const { project, actor } = this.ownerContextFor(sessionId)
|
|
@@ -353,7 +354,11 @@ export const taskDispatchMethods = {
|
|
|
353
354
|
// 核验纪律派单闸(DSHBOTS-303):重派/改卡按生效卡面(本次传入优先,缺省回落原卡)。
|
|
354
355
|
this.assertNotVerificationDispatch(project, member.id, input.title ?? task.title, input.description ?? task.description)
|
|
355
356
|
// 派单策略闸(DSHBOTS-314):重派按原卡声明写范围同闸(重派不改写范围,D-232 口径)。
|
|
356
|
-
|
|
357
|
+
// 有效范围 + 染色条目合并判定:染色路径一样会被写盘,不能因染色漏拦只读 bot。
|
|
358
|
+
this.assertWritableDispatch(member.id, [
|
|
359
|
+
...readWriteScopes(task.write_scopes),
|
|
360
|
+
...readWriteScopes(task.ignored_write_scopes),
|
|
361
|
+
])
|
|
357
362
|
// 任务卡修正闭环(DSHBOTS-232 ①):重派可同时改卡面(标题/描述/优先级)——先经
|
|
358
363
|
// updateTask 落卡,卡面变化由其统一触发点通知负责人修正,再走原重派链路。
|
|
359
364
|
// 写范围不在此口改:入队运行快照与调度闸门消费的不是同一份数据,
|
|
@@ -379,21 +384,23 @@ export const taskDispatchMethods = {
|
|
|
379
384
|
}
|
|
380
385
|
// 派单必须声明写范围(程序化闸门,不靠派单方自觉):未声明写范围的任务其写运行会与
|
|
381
386
|
// 同项目所有写运行判为冲突并保守串行,一条空范围任务就能把整队堵死。这里直接拒绝,
|
|
382
|
-
//
|
|
387
|
+
// 而不是留到调度器里把并发能力悄悄吃掉。命中 .gitignore 的染色条目算已声明:
|
|
388
|
+
// 它们进 ignored_write_scopes、不参与并行锁,全染色卡照常派发且不堵任何队列。
|
|
383
389
|
const parsedScopes = input.write_scopes === undefined || input.write_scopes === null
|
|
384
390
|
? parseWriteScopesFromDescription(input.description)
|
|
385
391
|
: parseWriteScopes(input.write_scopes)
|
|
386
|
-
|
|
387
|
-
// 拒绝。原清单透传 createTask 做权威清洗+审计(审计需真实 taskId),此处只预览不落账。
|
|
388
|
-
const scopes = filterIgnoredWriteScopes(parsedScopes, this.projectWorkDir(project)).kept
|
|
389
|
-
// 派单策略闸(DSHBOTS-314):写卡派只读 bot 在入口拒绝;重派分支按原卡声明补一道。
|
|
390
|
-
this.assertWritableDispatch(member.id, scopes)
|
|
391
|
-
if (!scopes.length) {
|
|
392
|
+
if (!parsedScopes.length) {
|
|
392
393
|
throw badRequest(
|
|
393
394
|
'派单必须声明写范围:传 write_scopes 路径数组,或在描述里写一行「写范围:a, b」。'
|
|
394
395
|
+ '未声明会让该任务的写运行与同项目其他写运行全部串行,一条任务即可堵死队列。',
|
|
395
396
|
)
|
|
396
397
|
}
|
|
398
|
+
// 闸门按清洗后清单预判(DSHBOTS-168):染色条目剔除后剩下的有效范围才参与
|
|
399
|
+
// 整目录闸 / 上限闸 / 重复卡闸。原清单透传 createTask 做权威清洗+落库(含染色清单)。
|
|
400
|
+
const scopes = filterIgnoredWriteScopes(parsedScopes, this.projectWorkDir(project)).kept
|
|
401
|
+
// 派单策略闸(DSHBOTS-314):写卡派只读 bot 在入口拒绝;重派分支按原卡声明补一道。
|
|
402
|
+
// 判定用完整声明清单:染色路径一样会被写盘,不能因染色漏拦只读 bot。
|
|
403
|
+
this.assertWritableDispatch(member.id, parsedScopes)
|
|
397
404
|
// 大卡拆分闸(产品化派单纪律):写范围条目超过 maxWriteScopesPerTask 时拒绝整卡
|
|
398
405
|
// 派发——超大范围卡与同项目一切写运行按保守串行互斥,一条卡即可吃掉整队并行能力
|
|
399
406
|
// (2026-11-13 生产实测 run_parallel 触发率仅 26%,主因即大范围声明)。拒绝文案
|
|
@@ -8,7 +8,7 @@
|
|
|
8
8
|
import { badRequest, conflict, forbidden } from '../errors.js'
|
|
9
9
|
import { bool } from '../config.js'
|
|
10
10
|
import { REVIEWER_BOT_NAME } from '../seed-bots.js'
|
|
11
|
-
import { id, nowIso, text } from '../util.js'
|
|
11
|
+
import { id, nowIso, text, jsonParse} from '../util.js'
|
|
12
12
|
import { parseWriteScopes, readWriteScopes } from './write-scopes.js'
|
|
13
13
|
import { effectiveSandbox } from './runtime/sandbox-policy.js'
|
|
14
14
|
|
|
@@ -54,30 +54,36 @@ export const taskReviewMethods = {
|
|
|
54
54
|
// 仅机器人路径:微信管理员/看板 owner 走 applyReview,不经过本闸。
|
|
55
55
|
assertReviewSubagentVerified(sessionId, project, task, comment) {
|
|
56
56
|
if (comment?.includes('兜底验收')) return
|
|
57
|
-
// run_events
|
|
58
|
-
const windowStart = this.
|
|
57
|
+
// run_events 住各运行归属的聊天库(Phase 3):先筛 runs 再逐运行查归属库,量级与旧「当运行」查询相同。
|
|
58
|
+
const windowStart = this.groupChatStore(task.project_id).get(
|
|
59
59
|
`SELECT MAX(created_at) AS t FROM task_events WHERE task_id = ?
|
|
60
60
|
AND (type = 'run_succeeded' OR (type = 'status' AND json_extract(data_json, '$.to') = 'in_review'))`,
|
|
61
61
|
task.id,
|
|
62
62
|
)?.t ?? ''
|
|
63
63
|
// 核验分档:本评审周期的交付运行为只读档(事件载荷 runId 指回交付运行)时免子代理
|
|
64
64
|
// 账目——与自动委派豁免同口径;无交付运行锚(如纯看板流转)时保持强制,不放宽。
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
65
|
+
// Phase 3:task_events 住项目聊天库(runs 仍顶库):先取事件,再回顶库取运行行。
|
|
66
|
+
const chat = this.groupChatStore(task.project_id)
|
|
67
|
+
const event = chat.get(
|
|
68
|
+
`SELECT data_json FROM task_events WHERE task_id = ? AND type = 'run_succeeded' AND created_at >= ?
|
|
69
|
+
ORDER BY created_at DESC LIMIT 1`,
|
|
69
70
|
task.id, windowStart,
|
|
70
71
|
)
|
|
72
|
+
const deliveredRunId = event ? (jsonParse(event.data_json, {}) ?? {}).runId : null
|
|
73
|
+
const delivered = deliveredRunId
|
|
74
|
+
? this.store.get('SELECT * FROM runs WHERE id = ?', deliveredRunId)
|
|
75
|
+
: null
|
|
71
76
|
if (delivered && this.readOnlyDelivery(delivered, task)) return
|
|
72
|
-
const
|
|
73
|
-
`SELECT
|
|
74
|
-
WHERE (
|
|
75
|
-
OR (
|
|
76
|
-
|
|
77
|
-
WHERE e.run_id = r.id AND e.type = 'subagent_spawned' AND e.created_at >= ?)
|
|
78
|
-
LIMIT 1`,
|
|
79
|
-
task.id, project.owner_bot_id, sessionId, task.id, windowStart,
|
|
77
|
+
const candidates = this.store.all(
|
|
78
|
+
`SELECT id, project_id, bot_id FROM runs
|
|
79
|
+
WHERE (task_id = ? AND bot_id = ?) OR session_id = ?
|
|
80
|
+
OR (task_id = ? AND trigger_type = 'review_delegate')`,
|
|
81
|
+
task.id, project.owner_bot_id, sessionId, task.id,
|
|
80
82
|
)
|
|
83
|
+
const ledger = candidates.some(run => this.runChatStore(run).get(
|
|
84
|
+
"SELECT 1 AS hit FROM run_events WHERE run_id = ? AND type = 'subagent_spawned' AND created_at >= ?",
|
|
85
|
+
run.id, windowStart,
|
|
86
|
+
))
|
|
81
87
|
if (ledger) return
|
|
82
88
|
// DSHBOTS-269 失败语义引导:拒绝文案 = 原因 + 正确下一步。
|
|
83
89
|
throw badRequest(
|
|
@@ -153,25 +159,26 @@ export const taskReviewMethods = {
|
|
|
153
159
|
const task = this.taskRow(taskId)
|
|
154
160
|
if (task.status === 'done' || task.status === 'canceled') throw conflict('任务已结束,不能修改写范围')
|
|
155
161
|
const parsedScopes = parseWriteScopes(writeScopesInput)
|
|
156
|
-
//
|
|
157
|
-
//
|
|
158
|
-
const scopes = this.normalizeDispatchScopes(this.projectRow(task.project_id), parsedScopes, { actor, taskId })
|
|
159
|
-
if (!scopes.length) throw badRequest('写范围不能为空:未声明的写运行会与同项目所有写运行保守串行')
|
|
162
|
+
// 派单清洗:与派单入口同口径——ignored 染色(进 ignored_write_scopes,不参与并行锁)
|
|
163
|
+
// + 一级目录驳回;有效与染色清单同时为空才落「写范围不能为空」拒绝。
|
|
164
|
+
const { scopes, ignored } = this.normalizeDispatchScopes(this.projectRow(task.project_id), parsedScopes, { actor, taskId })
|
|
165
|
+
if (!scopes.length && !ignored.length) throw badRequest('写范围不能为空:未声明的写运行会与同项目所有写运行保守串行')
|
|
160
166
|
const runningWrite = this.store.get(
|
|
161
167
|
"SELECT id FROM runs WHERE task_id = ? AND status = 'running' AND mode = 'write'",
|
|
162
168
|
taskId,
|
|
163
169
|
)
|
|
164
170
|
if (runningWrite) throw badRequest('任务有运行中的写运行,先等运行结束或取消,再修改写范围')
|
|
165
171
|
const queuedWrites = this.store.all(
|
|
166
|
-
"SELECT id FROM runs WHERE task_id = ? AND status = 'queued' AND mode = 'write'",
|
|
172
|
+
"SELECT id, project_id, bot_id FROM runs WHERE task_id = ? AND status = 'queued' AND mode = 'write'",
|
|
167
173
|
taskId,
|
|
168
174
|
)
|
|
169
175
|
const from = readWriteScopes(task.write_scopes)
|
|
170
176
|
const serialized = JSON.stringify(scopes)
|
|
177
|
+
const ignoredSerialized = JSON.stringify(ignored)
|
|
171
178
|
this.store.transaction(() => {
|
|
172
179
|
this.store.run(
|
|
173
|
-
'UPDATE tasks SET write_scopes = ?, version = version + 1, updated_at = ? WHERE id = ?',
|
|
174
|
-
serialized, nowIso(), taskId,
|
|
180
|
+
'UPDATE tasks SET write_scopes = ?, ignored_write_scopes = ?, version = version + 1, updated_at = ? WHERE id = ?',
|
|
181
|
+
serialized, ignoredSerialized, nowIso(), taskId,
|
|
175
182
|
)
|
|
176
183
|
for (const run of queuedWrites) {
|
|
177
184
|
this.store.run('UPDATE runs SET write_scopes = ? WHERE id = ?', serialized, run.id)
|
|
@@ -261,15 +268,17 @@ export const taskReviewMethods = {
|
|
|
261
268
|
}
|
|
262
269
|
throw error
|
|
263
270
|
}
|
|
264
|
-
this.
|
|
271
|
+
this.runChatStore({ project_id: project.id, bot_id: verifier.id }).run(
|
|
265
272
|
'INSERT INTO run_events(id, run_id, type, message, created_at) VALUES (?, ?, ?, ?, ?)',
|
|
266
273
|
id('evt'), runId, 'queued',
|
|
267
274
|
`${origin === 'auto' ? '交付后自动' : 'owner'}委派子代理核验(${actor.type}:${actor.id})`, createdAt,
|
|
268
275
|
)
|
|
269
276
|
if (routing.mode === 'fallback') {
|
|
270
|
-
this.
|
|
277
|
+
const run = this.store.get('SELECT * FROM runs WHERE id = ?', runId)
|
|
278
|
+
this.insertRunEvent(run.id, 'quota_fallback', `主 provider 额度耗尽,本次直接走备用模型 ${routing.ref}`)
|
|
271
279
|
} else if (routing.mode === 'wait') {
|
|
272
|
-
this.
|
|
280
|
+
const run = this.store.get('SELECT * FROM runs WHERE id = ?', runId)
|
|
281
|
+
this.insertRunEvent(run.id, 'quota_wait', `主 provider 额度耗尽且无可用备用模型,排队等待至 ${routing.resetAt ?? '恢复'}`)
|
|
273
282
|
}
|
|
274
283
|
this.audit(actor, origin === 'auto' ? 'review.auto_delegate' : 'task.review_delegate', 'task', task.id, { runId })
|
|
275
284
|
void this.tick()
|