dsh-team-bots 0.2.338 → 0.2.347
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 +81 -19
- package/docs/README.zh.md +83 -21
- package/package.json +1 -1
- package/src/service.js +4 -0
- package/src/services/backoff.js +34 -0
- package/src/services/notify.js +70 -0
- package/src/services/run-finish.js +72 -15
- package/src/services/scheduler.js +7 -6
- package/src/services/task-dispatch.js +315 -0
- package/src/services/task-notify.js +6 -4
- package/src/services/task-runs.js +179 -0
- package/src/services/tasks.js +8 -465
- package/ui/app.js +1 -1
- package/ui/i18n/en/chat.js +1 -1
- package/ui/i18n/en/common.js +0 -2
- package/ui/i18n/en/nav.js +0 -1
- package/ui/i18n/en/settings.js +2 -4
- package/ui/i18n/en/tasks.js +1 -4
- package/ui/i18n/en/wechat.js +1 -2
- package/ui/i18n/zh/chat.js +1 -1
- package/ui/i18n/zh/common.js +0 -2
- package/ui/i18n/zh/nav.js +0 -1
- package/ui/i18n/zh/settings.js +2 -4
- package/ui/i18n/zh/tasks.js +1 -4
- package/ui/i18n/zh/wechat.js +1 -2
- package/ui/modules/cards.js +1 -1
- package/ui/modules/global-pages.js +2 -4
- package/ui/modules/modal-forms.js +3 -3
- package/ui/modules/tasks.js +2 -3
- package/ui/styles/global-config.css +0 -3
package/README.md
CHANGED
|
@@ -2,48 +2,110 @@
|
|
|
2
2
|
|
|
3
3
|
[中文](docs/README.zh.md)
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
`dsh-team-bots` is a DSH plugin that turns one workspace into a team: project group chats, a task board, write-scope parallel scheduling, shared memory and skills, approval cards, usage and cost, and a personal WeChat channel. Every run keeps its trajectory, duration, token count and cost.
|
|
6
6
|
|
|
7
|
-
|
|
7
|
+
## Requirements
|
|
8
8
|
|
|
9
|
-
|
|
9
|
+
- Node.js ≥ 22.19
|
|
10
|
+
- the DSH CLI (`dsh`)
|
|
11
|
+
|
|
12
|
+
The plugin runs inside the DSH Web process. It has no standalone service and no port of its own.
|
|
10
13
|
|
|
11
|
-
|
|
14
|
+
## Install
|
|
12
15
|
|
|
13
16
|
```sh
|
|
14
17
|
dsh plugin --profile <name> add dsh-team-bots
|
|
15
|
-
dsh --profile <name> # read the tokenised dsh web entry from the startup log
|
|
16
18
|
```
|
|
17
19
|
|
|
18
|
-
|
|
20
|
+
From a checkout (packs the working tree and installs it into the profile):
|
|
21
|
+
|
|
22
|
+
```sh
|
|
23
|
+
npm run profile:install -- --profile <name>
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
Plugin files inside a profile are a snapshot. After changing code, reinstall — otherwise the page keeps serving the old build.
|
|
19
27
|
|
|
20
|
-
##
|
|
28
|
+
## Open the workspace
|
|
21
29
|
|
|
22
30
|
```text
|
|
23
|
-
http://<host>:<port>/ auto-login
|
|
24
|
-
http://<host>:<port>/api/dsh-team/ui the
|
|
25
|
-
http://<host>:<port>/index.html the native DSH web UI
|
|
31
|
+
http://<host>:<port>/ DSH web, auto-login into the team workspace
|
|
32
|
+
http://<host>:<port>/api/dsh-team/ui the team workspace itself
|
|
26
33
|
```
|
|
27
34
|
|
|
28
|
-
The first visit
|
|
35
|
+
The first visit exchanges the current startup token for a 30-day login cookie. Auto-login means anyone who can reach the port becomes owner, so listen on the local machine or a trusted network; for public use put an HTTPS reverse proxy in front that forwards Host and WebSocket correctly.
|
|
36
|
+
|
|
37
|
+
## What the workspace does
|
|
38
|
+
|
|
39
|
+
### Group chats and dispatch
|
|
40
|
+
|
|
41
|
+
Every project is a group chat. A bot can also be addressed one-to-one in a dispatch view. Session bindings run in four lanes — task, solo, dm, project — so a group chat, a task run and a direct message never block each other.
|
|
42
|
+
|
|
43
|
+
### Task board
|
|
44
|
+
|
|
45
|
+
States: backlog, todo, in progress, blocked, in review, done, canceled. A card carries assignee, priority, parent/child links, tags, write scopes, an optional time budget (a hard limit that aborts the run), comments, and its own usage. Rows are grouped by state and can be dragged onto another group to change status.
|
|
46
|
+
|
|
47
|
+
### Write scopes and parallel scheduling
|
|
48
|
+
|
|
49
|
+
A card declares the paths it will write, relative to the project working directory. Two runs of the same project run in parallel only when both declare scopes and the path prefixes do not overlap; a card with no declared scope is treated as serial. An urgent card may be exempted from the queue — the exemption is written into the card's creation message rather than applied silently.
|
|
50
|
+
|
|
51
|
+
### Memory, skills, evolution proposals
|
|
52
|
+
|
|
53
|
+
Memory entries are written, reviewed, frozen and archived by the owner, and are readable by every bot in the project. Skills come from a shared skill root plus per-project skills; changing them goes through a proposal, an approval and an apply, with a canary stage before promotion to full effect. The same path can change a bot's role instructions.
|
|
54
|
+
|
|
55
|
+
### Approval and question cards
|
|
56
|
+
|
|
57
|
+
When a bot asks for a tool approval, a secret, or an answer, the workspace shows a card in the chat. The owner approves, denies or answers in place, and the decision is recorded on the card.
|
|
58
|
+
|
|
59
|
+
### Usage and cost
|
|
60
|
+
|
|
61
|
+
The usage page breaks the numbers down by model, bot and project, exports CSV, and reads model prices from configuration. Run limits (timeout abort, no-progress watchdog, review idle threshold) and budgets are set per workspace.
|
|
62
|
+
|
|
63
|
+
### Terminal, channels, secrets
|
|
64
|
+
|
|
65
|
+
A per-project terminal runs commands in the project directory. The WeChat channel binds by QR code and pushes task activity to the owner's WeChat. Secrets are scoped to the team or to one project; values are only ever shown as a fingerprint and are handed out as one-time runtime copies that expire.
|
|
29
66
|
|
|
30
67
|
## Configuration
|
|
31
68
|
|
|
32
|
-
|
|
69
|
+
| Variable | Default | Meaning |
|
|
70
|
+
| --- | --- | --- |
|
|
71
|
+
| `DSH_TEAM_WORKSPACE` | `~/.dsh/workspace/team-bots` | team workspace root |
|
|
72
|
+
| `DSH_TEAM_DATA` | `<workspace>/.data` | database and run records |
|
|
33
73
|
|
|
34
|
-
|
|
74
|
+
Every other variable, its default, and its resolution order (explicit env > value saved in the workspace > code default) live in `src/config.ts`; `.env.example` lists the common ones. Do not put a `.env` file in the repository root — it would leak `DSH_*` into `npm test` and `npm run dev`.
|
|
35
75
|
|
|
36
|
-
|
|
76
|
+
`DSH_TEAM_DATA` holds `team.sqlite`, `wechat-channel.sqlite`, `secret.key`, `memory/`, `messages/` and `secrets-runtime/`. Back up `secret.key` together with the data: without it the stored ciphertext cannot be decrypted.
|
|
37
77
|
|
|
38
|
-
##
|
|
78
|
+
## Develop
|
|
39
79
|
|
|
40
|
-
|
|
80
|
+
```sh
|
|
81
|
+
npm install
|
|
82
|
+
npm run build # compile TypeScript into the shipped .js files
|
|
83
|
+
npm run typecheck
|
|
84
|
+
npm test # build, then node --test over tests/ and scripts/
|
|
85
|
+
npm run dev # local dev server on http://127.0.0.1:4317
|
|
86
|
+
npm run doctor # check node, dsh, paths and profile references
|
|
87
|
+
```
|
|
41
88
|
|
|
42
|
-
|
|
89
|
+
Additional harnesses: `npm run backup` / `npm run restore-check` for data, `npm run verify:agent-team` for the agent-team capability, `npm run load-test` and `npm run fault-test` for the runtime.
|
|
43
90
|
|
|
44
|
-
|
|
91
|
+
Repository layout:
|
|
92
|
+
|
|
93
|
+
```text
|
|
94
|
+
ui/ the browser workspace (modules, styles, zh/en copy)
|
|
95
|
+
src/ HTTP API, database, runtime and domain services
|
|
96
|
+
src/services/ domain logic: chat, tasks, scheduler, memory, skills, approvals, usage
|
|
97
|
+
src/tools.ts the tool surface bots see (team_* tools)
|
|
98
|
+
scripts/ build, dev server, doctor, release, backup, test harnesses
|
|
99
|
+
tests/ node:test suites
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
## Release
|
|
103
|
+
|
|
104
|
+
```sh
|
|
105
|
+
npm run release # add --dry-run to print the plan without touching anything
|
|
106
|
+
```
|
|
45
107
|
|
|
46
|
-
|
|
108
|
+
The tool pushes `dev`, lands it on `main` (fast-forward when possible, otherwise a content snapshot), pushes `main` and the tag, then syncs `dev` back. Publishing uses npm OIDC trusted publishing — no `NPM_TOKEN` — and the workflow installs npm ≥ 11.5.1 before publishing.
|
|
47
109
|
|
|
48
110
|
## License
|
|
49
111
|
|
package/docs/README.zh.md
CHANGED
|
@@ -2,49 +2,111 @@
|
|
|
2
2
|
|
|
3
3
|
[English](../README.md)
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
`dsh-team-bots` 是一个 DSH 插件,把一个工作区变成一支团队:项目群聊、任务板、写范围并行调度、共享记忆与技能、审批卡、用量与成本,以及个人微信通道。每次运行都留下轨迹、时长、token 数与成本。
|
|
6
6
|
|
|
7
|
-
|
|
7
|
+
## 运行前提
|
|
8
8
|
|
|
9
|
-
|
|
9
|
+
- Node.js ≥ 22.19
|
|
10
|
+
- DSH 命令行(`dsh`)
|
|
11
|
+
|
|
12
|
+
插件运行在 DSH Web 进程内,没有独立服务,也不单独占端口。
|
|
10
13
|
|
|
11
|
-
|
|
14
|
+
## 安装
|
|
12
15
|
|
|
13
16
|
```sh
|
|
14
17
|
dsh plugin --profile <name> add dsh-team-bots
|
|
15
|
-
dsh --profile <name> # 从启动日志里读带 token 的 dsh web 入口
|
|
16
18
|
```
|
|
17
19
|
|
|
18
|
-
|
|
20
|
+
从源码装(打包当前工作树并装进指定 profile):
|
|
21
|
+
|
|
22
|
+
```sh
|
|
23
|
+
npm run profile:install -- --profile <name>
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
profile 里的插件文件是一份快照。改完代码要重新安装,否则页面一直跑旧版本。
|
|
19
27
|
|
|
20
|
-
##
|
|
28
|
+
## 打开工作台
|
|
21
29
|
|
|
22
30
|
```text
|
|
23
|
-
http://<host>:<port>/
|
|
24
|
-
http://<host>:<port>/api/dsh-team/ui
|
|
25
|
-
http://<host>:<port>/index.html DSH 原生 Web UI
|
|
31
|
+
http://<host>:<port>/ DSH Web 首页,自动登录进团队工作台
|
|
32
|
+
http://<host>:<port>/api/dsh-team/ui 团队工作台本身
|
|
26
33
|
```
|
|
27
34
|
|
|
28
|
-
|
|
35
|
+
首次访问会把当前启动 token 换成 30 天登录 cookie。自动登录意味着能访问该端口的人就是 owner,请只监听本机或可信网络;要对外暴露,前面放一层转发 Host 与 WebSocket 的 HTTPS 反向代理。
|
|
36
|
+
|
|
37
|
+
## 工作台能做什么
|
|
38
|
+
|
|
39
|
+
### 群聊与调度
|
|
40
|
+
|
|
41
|
+
每个项目是一个群聊;单个 Bot 也可以被单独一对一调度。会话绑定分四道车道——task、solo、dm、project——群聊、任务运行和私信互不阻塞。
|
|
42
|
+
|
|
43
|
+
### 任务板
|
|
44
|
+
|
|
45
|
+
状态:待规划、待执行、进行中、受阻、待验收、已完成、已取消。一张卡带负责人、优先级、父子任务、标签、写范围、可选的时效要求(硬性时限,到期中止运行)、评论,以及本卡自己的用量。任务行按状态分组,可拖到别的分组直接改状态。
|
|
46
|
+
|
|
47
|
+
### 写范围与并行调度
|
|
48
|
+
|
|
49
|
+
每张卡声明自己会写哪些路径(相对项目工作目录)。同一项目下两个运行只有在双方都声明了写范围、且路径前缀互不重叠时才并行;没声明写范围的卡按串行处理。紧急卡可以豁免排队,但豁免会写进建卡消息,不做静默处理。
|
|
50
|
+
|
|
51
|
+
### 记忆、技能与进化提案
|
|
52
|
+
|
|
53
|
+
记忆由 owner 写入、复核、冻结与归档,项目内所有 Bot 都能读到。技能来自共享技能根目录加项目级技能;改动走提案 → 审批 → 落地,先灰度再转全量。同一条路径也能改 Bot 的角色指令。
|
|
29
54
|
|
|
30
|
-
|
|
55
|
+
### 审批卡与提问卡
|
|
31
56
|
|
|
32
|
-
|
|
57
|
+
Bot 要工具审批、要密钥或要人回答时,工作台在聊天里出一张卡。owner 就地批准、拒绝或回答,结论记在卡上。
|
|
33
58
|
|
|
34
|
-
|
|
59
|
+
### 用量与成本
|
|
35
60
|
|
|
36
|
-
|
|
61
|
+
用量页按模型、按 Bot、按项目拆分,可导出 CSV,模型价格来自配置。运行限制(超时中止、无进展看门狗、验收滞留阈值)与预算按工作区设置。
|
|
37
62
|
|
|
38
|
-
|
|
63
|
+
### 终端、通道与密钥
|
|
39
64
|
|
|
40
|
-
|
|
65
|
+
每个项目有终端,在项目目录里执行命令。微信通道扫码绑定后,把任务动态推给 owner 的微信。密钥分团队级与项目级,界面上只显示指纹,取值以一次性运行时副本下发,到期即失效。
|
|
41
66
|
|
|
42
|
-
|
|
67
|
+
## 配置
|
|
43
68
|
|
|
44
|
-
|
|
69
|
+
| 变量 | 默认值 | 含义 |
|
|
70
|
+
| --- | --- | --- |
|
|
71
|
+
| `DSH_TEAM_WORKSPACE` | `~/.dsh/workspace/team-bots` | 团队工作区根目录 |
|
|
72
|
+
| `DSH_TEAM_DATA` | `<workspace>/.data` | 数据库与运行记录 |
|
|
73
|
+
|
|
74
|
+
其余变量的默认值与解析顺序(显式环境变量 > 页面上保存的值 > 代码默认值)都写在 `src/config.ts`,常用项见 `.env.example`。不要在仓库根目录放 `.env`,否则 `DSH_*` 会泄进 `npm test` 和 `npm run dev`。
|
|
75
|
+
|
|
76
|
+
`DSH_TEAM_DATA` 下有 `team.sqlite`、`wechat-channel.sqlite`、`secret.key`、`memory/`、`messages/` 与 `secrets-runtime/`。备份时把 `secret.key` 和数据一起带走——没有它,已存的密文解不开。
|
|
77
|
+
|
|
78
|
+
## 开发
|
|
79
|
+
|
|
80
|
+
```sh
|
|
81
|
+
npm install
|
|
82
|
+
npm run build # 把 TypeScript 编译成发布的 .js
|
|
83
|
+
npm run typecheck
|
|
84
|
+
npm test # 先 build,再对 tests/ 与 scripts/ 跑 node --test
|
|
85
|
+
npm run dev # 本地开发服务器:http://127.0.0.1:4317
|
|
86
|
+
npm run doctor # 检查 node、dsh、路径与 profile 引用
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
另有几套工具:`npm run backup` / `npm run restore-check` 备份与校验数据,`npm run verify:agent-team` 验证 agent team 能力,`npm run load-test` 与 `npm run fault-test` 压运行时。
|
|
90
|
+
|
|
91
|
+
仓库结构:
|
|
92
|
+
|
|
93
|
+
```text
|
|
94
|
+
ui/ 浏览器端工作台(modules、styles、中英文案)
|
|
95
|
+
src/ HTTP 接口、数据库、运行时与领域服务
|
|
96
|
+
src/services/ 领域逻辑:chat、tasks、scheduler、memory、skills、approvals、usage
|
|
97
|
+
src/tools.ts Bot 看到的工具面(team_* 系列)
|
|
98
|
+
scripts/ 构建、开发服务器、doctor、发版、备份与测试工具
|
|
99
|
+
tests/ node:test 用例
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
## 发版
|
|
103
|
+
|
|
104
|
+
```sh
|
|
105
|
+
npm run release # 加 --dry-run 可只打印计划,不做任何改动
|
|
106
|
+
```
|
|
45
107
|
|
|
46
|
-
|
|
108
|
+
工具先把 `dev` 推上去,再把 dev 落到 `main`(能快进就快进,否则落一份内容快照),推 `main` 与 tag,最后把 `dev` 同步回来。发布走 npm OIDC trusted publishing,不用 `NPM_TOKEN`,工作流在发布前会把 npm 升到 ≥ 11.5.1。
|
|
47
109
|
|
|
48
|
-
##
|
|
110
|
+
## 许可
|
|
49
111
|
|
|
50
112
|
Apache-2.0,见 `LICENSE` 与 `NOTICE`。
|
package/package.json
CHANGED
package/src/service.js
CHANGED
|
@@ -23,6 +23,8 @@ import { botsMethods } from './services/bots.js'
|
|
|
23
23
|
import { projectsMethods } from './services/projects.js'
|
|
24
24
|
import { chatMethods } from './services/chat.js'
|
|
25
25
|
import { tasksMethods } from './services/tasks.js'
|
|
26
|
+
import { taskDispatchMethods } from './services/task-dispatch.js'
|
|
27
|
+
import { taskRunMethods } from './services/task-runs.js'
|
|
26
28
|
import { taskNotifyMethods } from './services/task-notify.js'
|
|
27
29
|
import { notifyMethods } from './services/notify.js'
|
|
28
30
|
import { taskReviewMethods } from './services/task-review.js'
|
|
@@ -88,6 +90,8 @@ defineMethods(TeamService.prototype, [
|
|
|
88
90
|
projectsMethods,
|
|
89
91
|
chatMethods,
|
|
90
92
|
tasksMethods,
|
|
93
|
+
taskDispatchMethods,
|
|
94
|
+
taskRunMethods,
|
|
91
95
|
taskNotifyMethods,
|
|
92
96
|
notifyMethods,
|
|
93
97
|
taskReviewMethods,
|
package/src/services/backoff.js
CHANGED
|
@@ -90,3 +90,37 @@ export function needsSynthesizedBackoff(error) {
|
|
|
90
90
|
if (isQuotaExhausted(error)) return false
|
|
91
91
|
return isRateLimitError(error) && !(Number(error?.retryAfterMs) > 0)
|
|
92
92
|
}
|
|
93
|
+
|
|
94
|
+
// ── 网络瞬态失败(DSHBOTS-266)──────────────────────────────────────────────
|
|
95
|
+
// provider 网络抖动(连接重置/超时/5xx 等)是过性失败:退避后重打大概率自愈,
|
|
96
|
+
// 直接常规失败转 blocked 等 owner 手动重试是误伤。判定只接住限流/额度分类漏掉的
|
|
97
|
+
// 过性错误——优先级:额度耗尽 > 限流码 > 瞬态(429 仍走限流路径吃服务端 retry_after)。
|
|
98
|
+
// 正则参考 dsh-superagent-prototype executor/dsh.ts 的 PASSING(去掉 429 与 rate limit:
|
|
99
|
+
// 那两个信号归限流分类,瞬态不抢)。错误码与错误正文双重匹配,任一命中即可。
|
|
100
|
+
// 数字锚点只对数字设界:HTTP_503 / status 502 命中,1408 / 75038 之类的长数字不误伤。
|
|
101
|
+
export const TRANSIENT_PATTERN = /(ECONNRESET|ECONNREFUSED|ETIMEDOUT|EAI_AGAIN|EPIPE|UND_ERR|socket hang up|fetch failed|overloaded|(?<!\d)408(?!\d)|(?<!\d)50[0-4](?!\d))/i
|
|
102
|
+
|
|
103
|
+
// 瞬态退避时长:15s / 30s / 60s(15s × 2^(n-1),attempt 缺失或非法按 1 计)。
|
|
104
|
+
// 比限流链(1min 起步)短——网络抖动通常秒级自愈;重试上限复用 MAX_RATE_LIMIT_RETRIES=3。
|
|
105
|
+
export function transientBackoffMs(attempt) {
|
|
106
|
+
const n = Math.max(1, Number(attempt) || 1)
|
|
107
|
+
return 15_000 * 2 ** (n - 1)
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
// 这次失败是不是网络瞬态。限流码与额度耗尽先排除(各自走既有路径)。
|
|
111
|
+
export function isTransientNetworkError(error) {
|
|
112
|
+
if (isRateLimitError(error)) return false
|
|
113
|
+
if (isQuotaExhausted(error)) return false
|
|
114
|
+
return TRANSIENT_PATTERN.test(`${String(error?.code ?? '')} ${String(error?.message ?? '')}`)
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
// 瞬态失败且 provider 没给可用的 Retry-After → 需要合成指数退避(真 Retry-After 优先)。
|
|
118
|
+
export function needsTransientBackoff(error) {
|
|
119
|
+
return isTransientNetworkError(error) && !(Number(error?.retryAfterMs) > 0)
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
// 这次失败是否进「退避等待 → plan_retry 到期派生」既有重试链(限流或网络瞬态)。
|
|
123
|
+
export function isRetryChainError(error) {
|
|
124
|
+
if (isQuotaExhausted(error)) return false
|
|
125
|
+
return isRateLimitError(error) || isTransientNetworkError(error)
|
|
126
|
+
}
|
package/src/services/notify.js
CHANGED
|
@@ -9,7 +9,45 @@
|
|
|
9
9
|
import { badRequest, notFound } from '../errors.js'
|
|
10
10
|
import { text } from '../util.js'
|
|
11
11
|
|
|
12
|
+
// ── 防循环三上限(DSHBOTS-267,配置默认值写死常量,KISS)──────────────────
|
|
13
|
+
// 真实事故背书:伙伴回执淹没主对话、委派续跑近百轮重发已推翻目标。
|
|
14
|
+
// ① 单 run 内经 notify/chat 产生的伙伴提醒(按去重目标计条数)上限:超出静默丢弃并记日志;
|
|
15
|
+
// ② 同类提醒事件(同 run + 同目标 bot)每日次数上限:超出静默丢弃并记日志。
|
|
16
|
+
// 每条放行的提醒在 run_events 记 type='partner_notice',message 存 partner_notice:<目标bot>,
|
|
17
|
+
// 两条上限都从这张事件流计数,不新建并行状态表。
|
|
18
|
+
export const RUN_PARTNER_NOTICE_CAP = 3
|
|
19
|
+
export const PARTNER_NOTICE_DAILY_CAP = 5
|
|
20
|
+
|
|
12
21
|
export const notifyMethods = {
|
|
22
|
+
// 伙伴提醒配额闸(DSHBOTS-267):放行返回 true;超限静默丢弃(warn 留痕)返回 false。
|
|
23
|
+
// run 归属不到(发起方无活跃运行)时不设限——空闲/人工会话的零星通知无循环风险。
|
|
24
|
+
allowPartnerNotice(runId, targetId) {
|
|
25
|
+
if (!runId || !targetId) return true
|
|
26
|
+
const distinctTargets = this.store.get(
|
|
27
|
+
"SELECT COUNT(DISTINCT message) AS n FROM run_events WHERE run_id = ? AND type = 'partner_notice'",
|
|
28
|
+
runId,
|
|
29
|
+
).n
|
|
30
|
+
if (distinctTargets >= RUN_PARTNER_NOTICE_CAP) {
|
|
31
|
+
this.logger.warn?.('dsh-team: 伙伴提醒超单 run 上限(%s 个目标),静默丢弃:run=%s target=%s', RUN_PARTNER_NOTICE_CAP, runId, targetId)
|
|
32
|
+
return false
|
|
33
|
+
}
|
|
34
|
+
const todayCount = this.store.get(
|
|
35
|
+
"SELECT COUNT(*) AS n FROM run_events WHERE run_id = ? AND type = 'partner_notice' AND message = ? AND date(created_at) = date('now')",
|
|
36
|
+
runId, `partner_notice:${targetId}`,
|
|
37
|
+
).n
|
|
38
|
+
if (todayCount >= PARTNER_NOTICE_DAILY_CAP) {
|
|
39
|
+
this.logger.warn?.('dsh-team: 伙伴提醒超每日上限(%s 次/日),静默丢弃:run=%s target=%s', PARTNER_NOTICE_DAILY_CAP, runId, targetId)
|
|
40
|
+
return false
|
|
41
|
+
}
|
|
42
|
+
return true
|
|
43
|
+
},
|
|
44
|
+
|
|
45
|
+
// 记一条放行的伙伴提醒事件(配额闸通过后调用;chat 唤醒路径共用同一事件流)。
|
|
46
|
+
recordPartnerNotice(runId, targetId) {
|
|
47
|
+
if (!runId || !targetId) return
|
|
48
|
+
this.insertRunEvent(runId, 'partner_notice', `partner_notice:${targetId}`)
|
|
49
|
+
},
|
|
50
|
+
|
|
13
51
|
// 工具入口:target=member 通知本项目另一成员;target=task 提醒本项目执行中任务。
|
|
14
52
|
// 鉴权:调用者必须是本项目成员(memberContextFor),目标成员/任务都锁在本项目内,
|
|
15
53
|
// 跨项目 ref 一律解析不到(notFound),不泄露他项目是否存在同名目标。
|
|
@@ -32,6 +70,18 @@ export const notifyMethods = {
|
|
|
32
70
|
.find(member => member.name.toLowerCase() === name.toLowerCase())
|
|
33
71
|
if (!target) throw notFound(`项目 ${project.name} 里没有叫 ${memberName} 的成员 Bot`)
|
|
34
72
|
if (target.id === from.id) throw badRequest('不能给自己发通知')
|
|
73
|
+
// 暂停/不可用目标不投递(DSHBOTS-262):通知即唤醒信号,暂停中先恢复再通知。
|
|
74
|
+
if (target.status === 'paused') throw badRequest(`Bot ${target.name} 已暂停,先恢复再通知`)
|
|
75
|
+
if (target.status !== 'active') throw badRequest(`Bot ${target.name} 当前不可用(${target.status})`)
|
|
76
|
+
// DSHBOTS-267 防循环上限:归属发起方当前活跃运行,配额闸不通过即静默丢弃(不落留痕不投递)。
|
|
77
|
+
const activeRun = this.store.get(
|
|
78
|
+
"SELECT id FROM runs WHERE bot_id = ? AND project_id = ? AND status = 'running' ORDER BY started_at DESC LIMIT 1",
|
|
79
|
+
from.id, project.id,
|
|
80
|
+
)
|
|
81
|
+
if (!this.allowPartnerNotice(activeRun?.id, target.id)) {
|
|
82
|
+
return `伙伴提醒已达防循环上限,本次已静默丢弃(详见运行日志)。`
|
|
83
|
+
}
|
|
84
|
+
this.recordPartnerNotice(activeRun?.id, target.id)
|
|
35
85
|
const notice = `【通知】${from.name}:${body}`
|
|
36
86
|
const messageId = this.insertMessage({
|
|
37
87
|
projectId: project.id,
|
|
@@ -72,6 +122,26 @@ export const notifyMethods = {
|
|
|
72
122
|
async notifyTaskFromBot({ project, from, ref, body }) {
|
|
73
123
|
const task = this.resolveBotTask(project, ref)
|
|
74
124
|
const identifier = `${project.key}-${task.number}`
|
|
125
|
+
// 负责人暂停/不可用时提醒不投递(DSHBOTS-262):提醒即唤醒信号,暂停中先恢复再提醒。
|
|
126
|
+
if (task.assignee_bot_id) {
|
|
127
|
+
const assignee = this.store.get('SELECT name, status FROM bots WHERE id = ?', task.assignee_bot_id)
|
|
128
|
+
if (assignee?.status === 'paused') throw badRequest(`任务 ${identifier} 负责人 ${assignee.name} 已暂停,先恢复再提醒`)
|
|
129
|
+
if (!assignee || assignee.status !== 'active') throw badRequest(`任务 ${identifier} 负责人当前不可用`)
|
|
130
|
+
}
|
|
131
|
+
// DSHBOTS-267 单跟进者在途幂等:每张任务同时只允许一个跟进提醒在途。
|
|
132
|
+
// 在途判定=最近一条任务提醒落库后,该任务没有启动过新运行(提醒尚未被任何运行消费);
|
|
133
|
+
// 重复提醒静默吸收(不落新留痕、不注入、不计新),warn 留痕。
|
|
134
|
+
const lastNotice = this.store.get(
|
|
135
|
+
"SELECT created_at FROM messages WHERE task_id = ? AND sender_type = 'bot' AND body LIKE '【任务提醒】%' ORDER BY created_at DESC, rowid DESC LIMIT 1",
|
|
136
|
+
task.id,
|
|
137
|
+
)
|
|
138
|
+
if (lastNotice && !this.store.get(
|
|
139
|
+
'SELECT id FROM runs WHERE task_id = ? AND started_at > ? LIMIT 1',
|
|
140
|
+
task.id, lastNotice.created_at,
|
|
141
|
+
)) {
|
|
142
|
+
this.logger.warn?.('dsh-team: 任务 %s 已有在途跟进提醒,重复提醒静默吸收', identifier)
|
|
143
|
+
return `任务 ${identifier} 已有在途跟进提醒,本次重复未投递(幂等去重,不计数)。`
|
|
144
|
+
}
|
|
75
145
|
const notice = `【任务提醒】${from.name}:${body}`
|
|
76
146
|
const messageId = this.insertMessage({
|
|
77
147
|
projectId: project.id,
|
|
@@ -10,14 +10,48 @@ import { rolloverSummaryKey } from '../config.js'
|
|
|
10
10
|
import {
|
|
11
11
|
isQuotaExhausted,
|
|
12
12
|
isRateLimitError,
|
|
13
|
+
isRetryChainError,
|
|
14
|
+
isTransientNetworkError,
|
|
13
15
|
MAX_RATE_LIMIT_RETRIES,
|
|
14
16
|
needsSynthesizedBackoff,
|
|
17
|
+
needsTransientBackoff,
|
|
15
18
|
rateLimitBackoffMs,
|
|
19
|
+
transientBackoffMs,
|
|
16
20
|
} from './backoff.js'
|
|
17
21
|
import { ensureAttentionSchema, normalizeAttention } from './attention-schema.js'
|
|
18
22
|
import { completeVisionRun, maybeSpawnVisionAfterFailure } from './vision-proxy.js'
|
|
19
23
|
import { chatReply } from '../wechat/format.js'
|
|
20
24
|
|
|
25
|
+
// ── 失败终态白名单分类(DSHBOTS-267)──────────────────────────────────────
|
|
26
|
+
// run 失败终态进入群聊/微信回推前,先按结构化信号归入有限白名单类别,对外只透出
|
|
27
|
+
// 「类别+一句话摘要」;原始错误串、文件路径、请求正文不整段透传(完整信息仍落
|
|
28
|
+
// runs.error_message 与 run_events,供 owner 查询)。分类信号复用 D-266 的
|
|
29
|
+
// isTransientNetworkError(TRANSIENT_PATTERN)与既有限流/额度分类,不重复造。
|
|
30
|
+
// 优先级:配额 > 限流 > 沙箱拒绝 > 超时 > 网络 > 内部错误 > 未知。
|
|
31
|
+
const SANDBOX_DENIED_PATTERN = /(EPERM|EACCES|sandbox|file access denied|access denied)/i
|
|
32
|
+
const TIMEOUT_PATTERN = /timed[\s_-]?out|timeout|超时/i
|
|
33
|
+
const INTERNAL_PATTERN = /(internal error|EINTERNAL|INTERNAL_ERROR)/i
|
|
34
|
+
const FAILURE_CATEGORY_NOTICES = {
|
|
35
|
+
quota: { label: '配额', summary: '模型额度耗尽,已按额度策略切换备用或转受阻。' },
|
|
36
|
+
rate_limited: { label: '限流', summary: '上游限流,已按退避策略自动处理。' },
|
|
37
|
+
sandbox_denied: { label: '沙箱拒绝', summary: '文件或命令操作被沙箱策略拒绝。' },
|
|
38
|
+
timeout: { label: '超时', summary: '上游响应超时。' },
|
|
39
|
+
network: { label: '网络', summary: '网络瞬态失败,已按退避策略自动重试。' },
|
|
40
|
+
internal: { label: '内部错误', summary: '服务内部错误。' },
|
|
41
|
+
unknown: { label: '未知', summary: '运行失败,完整原因见运行日志。' },
|
|
42
|
+
}
|
|
43
|
+
export function classifyFailure(error) {
|
|
44
|
+
const raw = `${String(error?.code ?? '')} ${String(error?.message ?? '')}`
|
|
45
|
+
if (isQuotaExhausted(error)) return FAILURE_CATEGORY_NOTICES.quota
|
|
46
|
+
if (isRateLimitError(error)) return FAILURE_CATEGORY_NOTICES.rate_limited
|
|
47
|
+
if (SANDBOX_DENIED_PATTERN.test(raw)) return FAILURE_CATEGORY_NOTICES.sandbox_denied
|
|
48
|
+
if (TIMEOUT_PATTERN.test(raw)) return FAILURE_CATEGORY_NOTICES.timeout
|
|
49
|
+
if (isTransientNetworkError(error)) return FAILURE_CATEGORY_NOTICES.network
|
|
50
|
+
if (INTERNAL_PATTERN.test(raw)) return FAILURE_CATEGORY_NOTICES.internal
|
|
51
|
+
return FAILURE_CATEGORY_NOTICES.unknown
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
|
|
21
55
|
export const runFinishMethods = {
|
|
22
56
|
// 自动核验委派(DSHBOTS-113):交付迁移 in_review 的同一事务内调用。开关关闭或
|
|
23
57
|
// 迁移未生效(任务已终态)时静默跳过;项目无可用总监属结构性前提缺失,审计留痕后
|
|
@@ -68,7 +102,14 @@ export const runFinishMethods = {
|
|
|
68
102
|
// DSHBOTS-99:额度耗尽(已达到限额)不合成指数退避——额度窗口用尽,退避重打撞同一堵墙。
|
|
69
103
|
const attemptNo = Math.max(1, Number(run.attempt) || 1)
|
|
70
104
|
const quotaExhausted = isQuotaExhausted(error)
|
|
71
|
-
|
|
105
|
+
// DSHBOTS-266:网络瞬态失败(ECONNRESET/5xx/408 等过性错误)同判合成退避,
|
|
106
|
+
// 与限流共用同一 retry_after_ms 落库与 plan_wait 派生链;真 Retry-After 仍优先。
|
|
107
|
+
const transientBackoff = needsTransientBackoff(error)
|
|
108
|
+
const synthesizedMs = needsSynthesizedBackoff(error)
|
|
109
|
+
? rateLimitBackoffMs(attemptNo)
|
|
110
|
+
: transientBackoff
|
|
111
|
+
? transientBackoffMs(attemptNo)
|
|
112
|
+
: null
|
|
72
113
|
// 额度耗尽时 provider 真给的重置时间照样保留(信息不丢,供 UI 显示额度恢复时间),
|
|
73
114
|
// 但它不再触发对主模型的自动重试(见下方失败分支与 scheduler.retryExpiredPlanWaits)。
|
|
74
115
|
const retryAfterMs = Number(error?.retryAfterMs) > 0 ? Number(error.retryAfterMs) : synthesizedMs
|
|
@@ -120,8 +161,10 @@ export const runFinishMethods = {
|
|
|
120
161
|
const suffix = attemptNo < MAX_RATE_LIMIT_RETRIES ? '后重试' : '后转总监处理'
|
|
121
162
|
this.insertRunEvent(
|
|
122
163
|
runId,
|
|
123
|
-
'rate_limit_backoff',
|
|
124
|
-
|
|
164
|
+
transientBackoff ? 'transient_backoff' : 'rate_limit_backoff',
|
|
165
|
+
transientBackoff
|
|
166
|
+
? `网络瞬态失败(${String(error?.code ?? error?.message ?? '未知错误').slice(0, 60)}),第 ${attemptNo} 次退避,约 ${Math.round(synthesizedMs / 1000)} 秒${suffix}`
|
|
167
|
+
: `上游限流(${error?.code})未返回 Retry-After,第 ${attemptNo} 次退避,约 ${Math.round(synthesizedMs / 1000)} 秒${suffix}`,
|
|
125
168
|
)
|
|
126
169
|
}
|
|
127
170
|
// 结构化提问(attention):只在 bot 显式给出时落库,规范超长/超量后写入;
|
|
@@ -213,7 +256,8 @@ export const runFinishMethods = {
|
|
|
213
256
|
senderId: bot.id,
|
|
214
257
|
body: status === 'succeeded'
|
|
215
258
|
? (cleanOutput || `总监已完成${label}。`)
|
|
216
|
-
|
|
259
|
+
// DSHBOTS-267:失败只透出白名单类别+摘要,不透传原始错误串。
|
|
260
|
+
: `总监${label}运行失败(${classifyFailure(error).label}):${classifyFailure(error).summary}`,
|
|
217
261
|
taskId: run.task_id,
|
|
218
262
|
runId,
|
|
219
263
|
})
|
|
@@ -250,7 +294,7 @@ export const runFinishMethods = {
|
|
|
250
294
|
projectId: project.id,
|
|
251
295
|
senderType: 'bot',
|
|
252
296
|
senderId: bot.id,
|
|
253
|
-
body: `总监${label}
|
|
297
|
+
body: `总监${label}运行失败(${classifyFailure(error).label}):${classifyFailure(error).summary}`,
|
|
254
298
|
runId,
|
|
255
299
|
})
|
|
256
300
|
}
|
|
@@ -361,7 +405,9 @@ export const runFinishMethods = {
|
|
|
361
405
|
// 禁止立刻重打上游——不切备用模型(备用可能走同一网关),也不立刻排总监处理运行。
|
|
362
406
|
// DSHBOTS-99:额度耗尽(已达到限额)不走这条路径——不指数退避、无条件先试备用模型;
|
|
363
407
|
// 瞬时限流的等待语义原样保留。
|
|
364
|
-
|
|
408
|
+
// DSHBOTS-266:网络瞬态失败(ECONNRESET/5xx/408 等)进同一退避链——等待期内同样
|
|
409
|
+
// 不切备用不排 handle,plan_retry 到期派生接管重试;attempt≤3,第 4 次照常转 blocked。
|
|
410
|
+
const backoffWaitMs = isRetryChainError(error) && retryAfterMs > 0 ? retryAfterMs : 0
|
|
365
411
|
// 额度耗尽不做自动重试:重置时间只当信息留着,不据此重打主模型。
|
|
366
412
|
const willRetry = !quotaExhausted && backoffWaitMs > 0 && attemptNo < MAX_RATE_LIMIT_RETRIES
|
|
367
413
|
// 主 provider 不可用:临时切备用模型继续跑这条任务。派生成功后任务不置 blocked、
|
|
@@ -401,16 +447,22 @@ export const runFinishMethods = {
|
|
|
401
447
|
const quotaNote = quotaExhausted
|
|
402
448
|
? `\n已达限额(${error?.code ?? '未知错误'}),主备模型均不可用,任务转为受阻${quotaResetNote}。`
|
|
403
449
|
: ''
|
|
450
|
+
// 退避提示按失败类别措辞:瞬态抖动与限流都是「退避后自动重试」,不混用限流文案。
|
|
451
|
+
const backoffKind = isTransientNetworkError(error) ? '网络瞬态失败' : '上游限流'
|
|
452
|
+
const backoffVerb = isTransientNetworkError(error) ? '退避' : '指数退避'
|
|
404
453
|
const backoffNote = backoffWaitMs > 0
|
|
405
454
|
? (willRetry
|
|
406
|
-
? `\n
|
|
407
|
-
: `\n
|
|
455
|
+
? `\n${backoffKind},第 ${attemptNo} 次${backoffVerb},约 ${Math.round(backoffWaitMs / 1000)} 秒后自动重试。`
|
|
456
|
+
: `\n${backoffKind},已达自动重试上限,约 ${Math.round(backoffWaitMs / 1000)} 秒后转总监处理。`)
|
|
408
457
|
: ''
|
|
458
|
+
// DSHBOTS-267:失败只透出白名单类别+摘要;原始错误串不进群聊(完整信息已落
|
|
459
|
+
// runs.error_message 与 run_events)。quotaNote/backoffNote 仅含结构化错误码。
|
|
460
|
+
const failure = classifyFailure(error)
|
|
409
461
|
this.insertMessage({
|
|
410
462
|
projectId: project.id,
|
|
411
463
|
senderType: 'bot',
|
|
412
464
|
senderId: bot.id,
|
|
413
|
-
body:
|
|
465
|
+
body: `任务执行失败(${failure.label}):${failure.summary}\n运行 ID:${runId}${quotaNote}${backoffNote}`,
|
|
414
466
|
taskId: run.task_id,
|
|
415
467
|
runId,
|
|
416
468
|
})
|
|
@@ -428,26 +480,28 @@ export const runFinishMethods = {
|
|
|
428
480
|
})
|
|
429
481
|
}
|
|
430
482
|
} else if (run.kind === 'chat') {
|
|
483
|
+
// DSHBOTS-267:群聊与微信回推共用同一份分类文案,不含原始错误串。
|
|
484
|
+
const failureBody = `运行失败(${classifyFailure(error).label}):${classifyFailure(error).summary}`
|
|
431
485
|
this.insertMessage({
|
|
432
486
|
projectId: project.id,
|
|
433
487
|
senderType: 'bot',
|
|
434
488
|
senderId: bot.id,
|
|
435
|
-
body:
|
|
489
|
+
body: failureBody,
|
|
436
490
|
runId,
|
|
437
491
|
})
|
|
438
|
-
this.notifyWechatChat(bot, run,
|
|
492
|
+
this.notifyWechatChat(bot, run, failureBody, true)
|
|
439
493
|
} else if (run.kind === 'solo_chat') {
|
|
440
|
-
const
|
|
494
|
+
const failureBody = `运行失败(${classifyFailure(error).label}):${classifyFailure(error).summary}`
|
|
441
495
|
this.insertSoloMessage({
|
|
442
496
|
botId: bot.id,
|
|
443
497
|
senderType: 'bot',
|
|
444
498
|
senderId: bot.id,
|
|
445
|
-
body:
|
|
499
|
+
body: failureBody,
|
|
446
500
|
runId,
|
|
447
501
|
})
|
|
448
502
|
// DSHBOTS-258:微信通道的 solo 直达失败提示同样推回(notifyWechatChat 内部按
|
|
449
503
|
// trigger_type='wechat' 门禁,普通 UI 单聊不推)。
|
|
450
|
-
this.notifyWechatChat(bot, run,
|
|
504
|
+
this.notifyWechatChat(bot, run, failureBody, true)
|
|
451
505
|
}
|
|
452
506
|
}
|
|
453
507
|
if (run.project_id && this.rate[run.project_id]) delete this.rate[run.project_id]
|
|
@@ -511,8 +565,11 @@ export const runFinishMethods = {
|
|
|
511
565
|
)
|
|
512
566
|
// 护栏二:链路深度 ≤2——深度达 2 的 bot 回复不再派发。
|
|
513
567
|
if (this.chatMentionChainDepth(run) >= 2) return []
|
|
514
|
-
|
|
568
|
+
// DSHBOTS-267 防循环上限(chat 路径):每次 @ 唤醒即一条伙伴提醒,与 notify 路径
|
|
569
|
+
// 共用同一 run_events 配额流;超限目标静默丢弃(allowPartnerNotice 内 warn 留痕)。
|
|
570
|
+
const dispatched = targets.slice(0, 3).filter(target => this.allowPartnerNotice(run.id, target.id))
|
|
515
571
|
for (const target of dispatched) {
|
|
572
|
+
this.recordPartnerNotice(run.id, target.id)
|
|
516
573
|
this.enqueueOutbox({
|
|
517
574
|
kind: 'chat_run',
|
|
518
575
|
payload: { projectId: project.id, botId: target.id, messageId, origin: null },
|