dsh-session-guard 0.1.2 → 0.2.0-beta.1
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/CHANGELOG.ja.md +75 -29
- package/CHANGELOG.ko.md +75 -29
- package/CHANGELOG.md +146 -29
- package/INSTALL.ja.md +83 -42
- package/INSTALL.ko.md +83 -42
- package/INSTALL.md +86 -44
- package/INSTALL.zh.md +82 -42
- package/README.en.md +303 -170
- package/README.ja.md +177 -106
- package/README.ko.md +177 -106
- package/README.md +159 -20
- package/dsh.plugin.json +13 -0
- package/lib/client.js +397 -11
- package/lib/client.js.map +1 -1
- package/package.json +101 -78
- package/src/bridge.js +61 -1
- package/src/client/badge-text.ts +49 -0
- package/src/client/index.ts +11 -1
- package/src/client/locales.ts +163 -95
- package/src/client/pause-button-text.ts +60 -0
- package/src/client/pause-button.tsx +129 -0
- package/src/client/settings-card.tsx +391 -191
- package/src/client/status-badge.tsx +59 -66
- package/src/client/styles.ts +30 -0
- package/src/deferrals.js +197 -0
- package/src/index.js +405 -293
- package/src/pause-gate.js +472 -416
- package/src/provider-directory.js +101 -0
- package/src/provider.js +139 -0
- package/src/request-guard.js +191 -0
- package/src/retry.js +227 -202
- package/src/settings.js +118 -97
- package/src/step-gate.js +399 -0
- package/src/targets.js +70 -0
- package/src/time.js +195 -104
- package/src/tool-call-id.js +44 -0
- package/src/wiring.js +305 -0
package/src/retry.js
CHANGED
|
@@ -1,202 +1,227 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* dsh-session-guard — 后端自动重试(host side,D9)。
|
|
3
|
-
*
|
|
4
|
-
* 监听 session/event:turn/end 以 error / interrupted / max-tokens 结束时,
|
|
5
|
-
* 分类瞬时/永久失败,自适应退避后自动 `agent.followup(retryText)` 续跑。
|
|
6
|
-
*
|
|
7
|
-
* 关键纪律:
|
|
8
|
-
* - **冻结让路**:会话被本插件门控(高峰暂停 / 队列锁)时不触发重试——
|
|
9
|
-
* 与 input-traffic「freeze 是一等公民」一致,自动重试不得绕过会话门。
|
|
10
|
-
* - 用户手动介入(user/message)与成功回合重置连续计数。
|
|
11
|
-
* - 子代理会话不重试(由父代理处理)。
|
|
12
|
-
* - 永久失败(鉴权/余额/模型不存在/上下文超限等)重试无益 → 停止并告警。
|
|
13
|
-
*
|
|
14
|
-
* 纯决策逻辑(classifyTurnEnd / isTransientFailure / effectiveCooldown /
|
|
15
|
-
* shouldRetry)零依赖可单测;事件接线在 createRetry。
|
|
16
|
-
*/
|
|
17
|
-
|
|
18
|
-
/** 默认重试配置。 */
|
|
19
|
-
export const DEFAULT_RETRY = Object.freeze({
|
|
20
|
-
retryEnabled: false, // ← 自动重试开关(默认关,保守)
|
|
21
|
-
retryText: '继续(自动重试)',
|
|
22
|
-
retryGraceMs: 3000, // 失败后等待多久再发
|
|
23
|
-
retryCooldownMs: 20000, // 同一会话两次重试最小间隔
|
|
24
|
-
retryBackoffFactor: 2,
|
|
25
|
-
retryBackoffMaxMs: 300000,
|
|
26
|
-
retryMaxConsecutive: 3, // 连续重试上限,超过停止
|
|
27
|
-
})
|
|
28
|
-
|
|
29
|
-
/**
|
|
30
|
-
export
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
if (
|
|
53
|
-
|
|
54
|
-
return false
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
*
|
|
66
|
-
*
|
|
67
|
-
*
|
|
68
|
-
*
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
if (
|
|
74
|
-
if (
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
*
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
*
|
|
90
|
-
* @param {
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
s
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
}
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
1
|
+
/**
|
|
2
|
+
* dsh-session-guard — 后端自动重试(host side,D9)。
|
|
3
|
+
*
|
|
4
|
+
* 监听 session/event:turn/end 以 error / interrupted / max-tokens 结束时,
|
|
5
|
+
* 分类瞬时/永久失败,自适应退避后自动 `agent.followup(retryText)` 续跑。
|
|
6
|
+
*
|
|
7
|
+
* 关键纪律:
|
|
8
|
+
* - **冻结让路**:会话被本插件门控(高峰暂停 / 队列锁)时不触发重试——
|
|
9
|
+
* 与 input-traffic「freeze 是一等公民」一致,自动重试不得绕过会话门。
|
|
10
|
+
* - 用户手动介入(user/message)与成功回合重置连续计数。
|
|
11
|
+
* - 子代理会话不重试(由父代理处理)。
|
|
12
|
+
* - 永久失败(鉴权/余额/模型不存在/上下文超限等)重试无益 → 停止并告警。
|
|
13
|
+
*
|
|
14
|
+
* 纯决策逻辑(classifyTurnEnd / isTransientFailure / effectiveCooldown /
|
|
15
|
+
* shouldRetry)零依赖可单测;事件接线在 createRetry。
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
/** 默认重试配置。 */
|
|
19
|
+
export const DEFAULT_RETRY = Object.freeze({
|
|
20
|
+
retryEnabled: false, // ← 自动重试开关(默认关,保守)
|
|
21
|
+
retryText: '继续(自动重试)',
|
|
22
|
+
retryGraceMs: 3000, // 失败后等待多久再发
|
|
23
|
+
retryCooldownMs: 20000, // 同一会话两次重试最小间隔
|
|
24
|
+
retryBackoffFactor: 2,
|
|
25
|
+
retryBackoffMaxMs: 300000,
|
|
26
|
+
retryMaxConsecutive: 3, // 连续重试上限,超过停止
|
|
27
|
+
})
|
|
28
|
+
|
|
29
|
+
/** 本插件自己的延后失败哨兵(与 deferrals.js 的 PEAK_DEFERRED_CODE 同一个值)。 */
|
|
30
|
+
export const PEAK_DEFERRED_CODE = 'PEAK_DEFERRED'
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* 是否本插件的「高峰延后」失败(**精确匹配,唯一短路**)。
|
|
34
|
+
*
|
|
35
|
+
* 两条路径都必须认:
|
|
36
|
+
* - `code === 'PEAK_DEFERRED'`:结构化路径(若上层保留了 failure.code);
|
|
37
|
+
* - `message` 以 `PEAK_DEFERRED:` 开头:DSH 回合循环只对 `error instanceof LlmError`
|
|
38
|
+
* 保留结构化 failure,其它一律压成 `{ message: errorChain(error), code: 'UNKNOWN' }`
|
|
39
|
+
* (见 findings 8.3),本插件不能 value-import `@deepseek-ai/dsh-llm`。
|
|
40
|
+
*
|
|
41
|
+
* **严禁**在这里加 `高峰` / `已延后` / `拦截` 之类宽泛关键词——那会误伤真实错误,
|
|
42
|
+
* 也会破坏 429 / 传输层的重试语义。
|
|
43
|
+
*/
|
|
44
|
+
export function isPeakDeferredFailure({ code, message } = {}) {
|
|
45
|
+
if (code === PEAK_DEFERRED_CODE) return true
|
|
46
|
+
return typeof message === 'string' && message.startsWith(`${PEAK_DEFERRED_CODE}:`)
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/** 瞬时 vs 永久失败分类:瞬时值得重试,永久重试无益。 */
|
|
50
|
+
export function isTransientFailure({ code, message, status } = {}) {
|
|
51
|
+
// 本插件自己的延后失败是「永久」——重试只会再被拦一次,形成放大。
|
|
52
|
+
if (isPeakDeferredFailure({ code, message })) return false
|
|
53
|
+
const haystack = `${code ?? ''} ${message ?? ''}`.toLowerCase()
|
|
54
|
+
if (status !== undefined && (status === 401 || status === 403)) return false
|
|
55
|
+
const permanent =
|
|
56
|
+
/auth|unauthor|forbidden|credential|api\s*[_-]?\s*key|permission/i.test(haystack) ||
|
|
57
|
+
/insufficient.*(balance|quota)|billing|payment/i.test(haystack) ||
|
|
58
|
+
/model[^a-z]*not[^a-z]*found|unknown[_-]?model|not.*support.*model/i.test(haystack) ||
|
|
59
|
+
/context.*(length|limit|overflow|exceed)|token.*limit|max.*context/i.test(haystack) ||
|
|
60
|
+
/invalid[_-]?request|bad[_-]?request/i.test(haystack)
|
|
61
|
+
return !permanent
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* turn/end reason → 是否可自动重试。
|
|
66
|
+
* - completed / aborted(用户停)/ blocked(策略拒)→ 否
|
|
67
|
+
* - error → 先按精确码短路 `PEAK_DEFERRED`(永久),再按 isTransientFailure 分类
|
|
68
|
+
* - interrupted(崩溃修复)→ 可重试
|
|
69
|
+
* - max-tokens → 可重试
|
|
70
|
+
*/
|
|
71
|
+
export function classifyTurnEnd(reason, failure) {
|
|
72
|
+
const kind = reason && reason.kind
|
|
73
|
+
if (kind === 'completed' || kind === 'aborted' || kind === 'blocked') return false
|
|
74
|
+
if (kind === 'error') {
|
|
75
|
+
if (isPeakDeferredFailure(failure)) return false
|
|
76
|
+
return isTransientFailure(failure)
|
|
77
|
+
}
|
|
78
|
+
if (kind === 'interrupted' || kind === 'max-tokens') return true
|
|
79
|
+
return false
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/** 自适应退避:consecutive 次连续后 cooldown * factor^n,封顶 max。 */
|
|
83
|
+
export function effectiveCooldown(consecutive, base, factor, max) {
|
|
84
|
+
const mult = Math.pow(factor, Math.max(0, consecutive))
|
|
85
|
+
return Math.min(Math.max(base, base * mult), Math.max(base, max))
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* 纯决策:此刻是否应触发重试。
|
|
90
|
+
* @param {object} s 会话状态 { consecutive, lastAttemptAt, pending }
|
|
91
|
+
* @param {object} cfg 重试配置
|
|
92
|
+
* @param {boolean} frozen 会话是否被门控(高峰暂停/队列锁)
|
|
93
|
+
* @param {number} now
|
|
94
|
+
*/
|
|
95
|
+
export function shouldRetry(s, cfg, frozen, now = Date.now()) {
|
|
96
|
+
if (!cfg.retryEnabled) return false
|
|
97
|
+
if (frozen) return false // 冻结让路(D9)
|
|
98
|
+
if (s.pending) return false // 已有排队重试
|
|
99
|
+
if (s.consecutive >= cfg.retryMaxConsecutive) return false
|
|
100
|
+
if (now - s.lastAttemptAt < effectiveCooldown(s.consecutive, cfg.retryCooldownMs, cfg.retryBackoffFactor, cfg.retryBackoffMaxMs)) return false
|
|
101
|
+
return true
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/** 会话状态工厂。 */
|
|
105
|
+
export function freshRetryState() {
|
|
106
|
+
return { consecutive: 0, lastAttemptAt: 0, pending: false }
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/**
|
|
110
|
+
* 事件接线(host):
|
|
111
|
+
* @param {object} deps
|
|
112
|
+
* @param {object} deps.ctx host context
|
|
113
|
+
* @param {()=>object} deps.getSettings 读实时配置
|
|
114
|
+
* @param {(sessionId:string)=>boolean} deps.isFrozen 会话是否被门控
|
|
115
|
+
* @param {(sessionId:string, text:string)=>void} [deps.send] 发送函数(默认 agent.followup)
|
|
116
|
+
*/
|
|
117
|
+
export function createRetry({ ctx, getSettings, isFrozen, send }) {
|
|
118
|
+
const states = new Map()
|
|
119
|
+
|
|
120
|
+
function state(sessionId) {
|
|
121
|
+
let s = states.get(sessionId)
|
|
122
|
+
if (!s) {
|
|
123
|
+
s = freshRetryState()
|
|
124
|
+
states.set(sessionId, s)
|
|
125
|
+
}
|
|
126
|
+
return s
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
/** 从 turn/end reason 提取失败事实。 */
|
|
130
|
+
function failureFacts(reason) {
|
|
131
|
+
const error = reason && reason.error
|
|
132
|
+
return {
|
|
133
|
+
code: error && typeof error.code === 'string' ? error.code : 'UNKNOWN',
|
|
134
|
+
message: error && typeof error.message === 'string' ? error.message : '',
|
|
135
|
+
status: error && typeof error.status === 'number' ? error.status : undefined,
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
function schedule(sessionId, reason) {
|
|
140
|
+
const s = state(sessionId)
|
|
141
|
+
const cfg = getSettings()
|
|
142
|
+
if (s.pending) return
|
|
143
|
+
const frozen = isFrozen(sessionId)
|
|
144
|
+
if (!shouldRetry({ ...s, pending: true }, cfg, frozen)) return
|
|
145
|
+
s.pending = true
|
|
146
|
+
const grace = cfg.retryGraceMs ?? DEFAULT_RETRY.retryGraceMs
|
|
147
|
+
const timer = setTimeout(() => {
|
|
148
|
+
s.pending = false
|
|
149
|
+
// 到点再复核:门控/上限变化后放弃。
|
|
150
|
+
if (!shouldRetry(s, cfg, isFrozen(sessionId))) return
|
|
151
|
+
const agent = ctx.agents.get(sessionId)
|
|
152
|
+
if (!agent || agent.status !== 'idle') return
|
|
153
|
+
const text = cfg.retryText ?? DEFAULT_RETRY.retryText
|
|
154
|
+
try {
|
|
155
|
+
if (send) {
|
|
156
|
+
send(sessionId, text)
|
|
157
|
+
} else {
|
|
158
|
+
// 与 autoresume 同构:直接构造 plugin-source 消息,避免运行时依赖。
|
|
159
|
+
agent.followup({
|
|
160
|
+
id: `session-guard-retry-${Date.now()}-${Math.random().toString(36).slice(2, 8)}`,
|
|
161
|
+
role: 'user',
|
|
162
|
+
content: [{ type: 'text', text }],
|
|
163
|
+
source: { kind: 'plugin', plugin: 'session-guard', form: 'notice' },
|
|
164
|
+
})
|
|
165
|
+
}
|
|
166
|
+
s.lastAttemptAt = Date.now()
|
|
167
|
+
s.consecutive += 1
|
|
168
|
+
ctx.logger?.info?.(`[session-guard] auto-retry ${sessionId} (${String(reason && reason.kind)}), #${s.consecutive}`)
|
|
169
|
+
} catch (e) {
|
|
170
|
+
ctx.logger?.warn?.(`[session-guard] auto-retry send failed: ${String(e && e.message || e)}`)
|
|
171
|
+
}
|
|
172
|
+
}, grace)
|
|
173
|
+
// 会话对象上的清理钩子(若宿主提供)。
|
|
174
|
+
const dispose = () => clearTimeout(timer)
|
|
175
|
+
return dispose
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
function onEvent(session, event) {
|
|
179
|
+
const sessionId = session && session.id
|
|
180
|
+
if (typeof sessionId !== 'string') return
|
|
181
|
+
const s = state(sessionId)
|
|
182
|
+
switch (event.type) {
|
|
183
|
+
case 'turn/end': {
|
|
184
|
+
const reason = event.data && event.data.reason
|
|
185
|
+
if (reason && reason.kind === 'completed') {
|
|
186
|
+
s.consecutive = 0 // 成功回合重置
|
|
187
|
+
s.pending = false
|
|
188
|
+
return
|
|
189
|
+
}
|
|
190
|
+
if (reason && reason.kind === 'aborted') {
|
|
191
|
+
// 用户主动停止:不重试,重置。
|
|
192
|
+
s.consecutive = 0
|
|
193
|
+
s.pending = false
|
|
194
|
+
return
|
|
195
|
+
}
|
|
196
|
+
if (reason && classifyTurnEnd(reason, failureFacts(reason))) {
|
|
197
|
+
schedule(sessionId, reason)
|
|
198
|
+
}
|
|
199
|
+
return
|
|
200
|
+
}
|
|
201
|
+
case 'user/message': {
|
|
202
|
+
// 用户手动介入:重置(无论是否我们的回显都保守重置)。
|
|
203
|
+
if (event.data && event.data.source && event.data.source.kind === 'user') {
|
|
204
|
+
s.consecutive = 0
|
|
205
|
+
s.pending = false
|
|
206
|
+
}
|
|
207
|
+
return
|
|
208
|
+
}
|
|
209
|
+
default:
|
|
210
|
+
return
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
ctx.on('session/event', (session, event) => {
|
|
215
|
+
try {
|
|
216
|
+
onEvent(session, event)
|
|
217
|
+
} catch (e) {
|
|
218
|
+
ctx.logger?.warn?.(`[session-guard] retry event failed: ${String(e && e.message || e)}`)
|
|
219
|
+
}
|
|
220
|
+
})
|
|
221
|
+
|
|
222
|
+
return {
|
|
223
|
+
onEvent,
|
|
224
|
+
state,
|
|
225
|
+
states,
|
|
226
|
+
}
|
|
227
|
+
}
|