dsh-session-guard 4.2.9 → 4.2.11
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.md +381 -363
- package/README.md +12 -1
- package/dsh.plugin.json +1 -1
- package/lib/client.js +3 -2
- package/lib/client.js.map +1 -1
- package/package.json +5 -5
- package/src/bridge.js +62 -8
- package/src/client/pause-button-text.ts +20 -4
- package/src/index.js +121 -7
- package/src/request-guard.js +17 -0
- package/src/step-gate.js +12 -0
- package/src/wiring.js +3 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "dsh-session-guard",
|
|
3
|
-
"version": "4.2.
|
|
3
|
+
"version": "4.2.11",
|
|
4
4
|
"publishConfig": {
|
|
5
5
|
"registry": "https://registry.npmjs.org",
|
|
6
6
|
"tag": "latest"
|
|
@@ -92,10 +92,10 @@
|
|
|
92
92
|
},
|
|
93
93
|
"peerDependencies": {
|
|
94
94
|
"@deepseek-ai/cordis": "^4.0.4",
|
|
95
|
-
"@deepseek-ai/dsh-client-locale": "
|
|
96
|
-
"@deepseek-ai/dsh-client-ui-renderer": "
|
|
97
|
-
"@deepseek-ai/dsh-client-ui-settings": "
|
|
98
|
-
"@deepseek-ai/dsh-client-ui-slots": "
|
|
95
|
+
"@deepseek-ai/dsh-client-locale": "0.1.0-rc.2 || 0.1.0-rc.3 || 0.1.0-rc.6 || 0.1.0-rc.7 || 0.1.0-rc.8 || 0.1.1-rc.1 || 0.1.1-rc.2 || 0.1.2-rc.1 || 0.1.5-rc.1 || 0.1.5-rc.2 || 0.1.5-rc.3 || 0.1.7-rc.1 || 0.1.7-rc.2 || 0.2.0-rc.1 || 0.2.0-rc.2 || 0.2.1-rc.1 || 0.2.1-rc.2 || 0.2.1-rc.3 || 0.2.1-rc.4 || 0.2.1-rc.5 || 0.2.2-rc.1",
|
|
96
|
+
"@deepseek-ai/dsh-client-ui-renderer": "0.1.0-rc.2 || 0.1.0-rc.3 || 0.1.0-rc.6 || 0.1.0-rc.7 || 0.1.0-rc.8 || 0.1.1-rc.1 || 0.1.1-rc.2 || 0.1.2-rc.1 || 0.1.5-rc.1 || 0.1.5-rc.2 || 0.1.5-rc.3 || 0.1.7-rc.1 || 0.1.7-rc.2 || 0.2.0-rc.1 || 0.2.0-rc.2 || 0.2.1-rc.1 || 0.2.1-rc.2 || 0.2.1-rc.3 || 0.2.1-rc.4 || 0.2.1-rc.5 || 0.2.2-rc.1",
|
|
97
|
+
"@deepseek-ai/dsh-client-ui-settings": "0.1.0-rc.2 || 0.1.0-rc.3 || 0.1.0-rc.6 || 0.1.0-rc.7 || 0.1.0-rc.8 || 0.1.1-rc.1 || 0.1.1-rc.2 || 0.1.2-rc.1 || 0.1.5-rc.1 || 0.1.5-rc.2 || 0.1.5-rc.3 || 0.1.7-rc.1 || 0.1.7-rc.2 || 0.2.0-rc.1 || 0.2.0-rc.2 || 0.2.1-rc.1 || 0.2.1-rc.2 || 0.2.1-rc.3 || 0.2.1-rc.4 || 0.2.1-rc.5 || 0.2.2-rc.1",
|
|
98
|
+
"@deepseek-ai/dsh-client-ui-slots": "0.1.0-rc.2 || 0.1.0-rc.3 || 0.1.0-rc.6 || 0.1.0-rc.7 || 0.1.0-rc.8 || 0.1.1-rc.1 || 0.1.1-rc.2 || 0.1.2-rc.1 || 0.1.5-rc.1 || 0.1.5-rc.2 || 0.1.5-rc.3 || 0.1.7-rc.1 || 0.1.7-rc.2 || 0.2.0-rc.1 || 0.2.0-rc.2 || 0.2.1-rc.1 || 0.2.1-rc.2 || 0.2.1-rc.3 || 0.2.1-rc.4 || 0.2.1-rc.5 || 0.2.2-rc.1"
|
|
99
99
|
},
|
|
100
100
|
"peerDependenciesMeta": {
|
|
101
101
|
"@deepseek-ai/cordis": {
|
package/src/bridge.js
CHANGED
|
@@ -18,8 +18,9 @@ import { idleState } from './store.js'
|
|
|
18
18
|
* @param {ReturnType<import('./store.js').createStore>} store
|
|
19
19
|
* @param {ReturnType<import('./pause-gate.js').createPauseGate>} [pauseGate]
|
|
20
20
|
* @param {ReturnType<import('./step-gate.js').createStepGate>} [stepGate] step 级门控(v0.2.0)
|
|
21
|
+
* @param {ReturnType<import('./deferrals.js').createDeferrals>} [deferrals] 请求级 hold 登记表(v4.2.11)
|
|
21
22
|
*/
|
|
22
|
-
export function createBridge(ctx, gate, store, pauseGate, stepGate) {
|
|
23
|
+
export function createBridge(ctx, gate, store, pauseGate, stepGate, deferrals) {
|
|
23
24
|
function taskControlState(sessionId) {
|
|
24
25
|
let tcState = null
|
|
25
26
|
try {
|
|
@@ -76,31 +77,60 @@ export function createBridge(ctx, gate, store, pauseGate, stepGate) {
|
|
|
76
77
|
* 解开 step 门(v0.2.0):放行被挂起的 step。
|
|
77
78
|
* 默认 `bypass=true`(用户手动「继续」)——本高峰窗口内不再拦该会话;
|
|
78
79
|
* 退峰/非官方切换等自动释放请用 `{ bypass: false }`。
|
|
80
|
+
*
|
|
81
|
+
* v4.2.11(issue #3):**请求级 hold 也是「暂停」的一种**,但它在 step 1 就发生、
|
|
82
|
+
* step 门根本没拉过,于是 `stepGate.release()` 返回 `{released:false}`,
|
|
83
|
+
* 连 bypass 都不置——用户点「继续会话」放不掉任何东西(旧行为)。
|
|
84
|
+
* 现在这里同时结算请求级 hold,并且**只要用户显式要继续就置 bypass**,
|
|
85
|
+
* 不论门来自 step 级还是请求级。
|
|
79
86
|
* @param {string} sessionId
|
|
80
87
|
* @param {{bypass?:boolean, reason?:string}} [opts]
|
|
81
88
|
*/
|
|
82
89
|
stepResume(sessionId, opts = {}) {
|
|
90
|
+
const reason = opts.reason ?? 'manual'
|
|
91
|
+
const wantBypass = opts.bypass !== false
|
|
92
|
+
const id = String(sessionId)
|
|
93
|
+
|
|
94
|
+
// ① 请求级 hold:先结算,让挂住的 promise 以原 config resolve(请求照常发出)。
|
|
95
|
+
let requestReleased = false
|
|
96
|
+
if (deferrals !== undefined && typeof deferrals.settle === 'function') {
|
|
97
|
+
try {
|
|
98
|
+
requestReleased = deferrals.settle(id, reason, 'release') === true
|
|
99
|
+
} catch {
|
|
100
|
+
requestReleased = false
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
if (requestReleased && deferrals !== undefined && typeof deferrals.clearDeferred === 'function') {
|
|
104
|
+
try {
|
|
105
|
+
deferrals.clearDeferred(id)
|
|
106
|
+
} catch {
|
|
107
|
+
/* 延后记录清理失败不影响放行 */
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
// ② step 级门:没有门可放时 released=false,但这不代表「什么也没发生」。
|
|
83
112
|
if (!stepGate || typeof stepGate.release !== 'function') {
|
|
84
|
-
return { released:
|
|
113
|
+
return { released: requestReleased, requestReleased, bypass: false, reason: 'no-step-gate' }
|
|
85
114
|
}
|
|
86
|
-
const reason = opts.reason ?? 'manual'
|
|
87
115
|
let r
|
|
88
116
|
try {
|
|
89
|
-
r = stepGate.release(
|
|
117
|
+
r = stepGate.release(id, reason)
|
|
90
118
|
} catch {
|
|
91
|
-
return { released:
|
|
119
|
+
return { released: requestReleased, requestReleased, bypass: false, reason }
|
|
92
120
|
}
|
|
93
121
|
const released = !!(r && r.released === true)
|
|
94
122
|
let bypassed = false
|
|
95
|
-
if (
|
|
123
|
+
if (wantBypass && typeof stepGate.markBypass === 'function') {
|
|
124
|
+
// 请求级 hold 也置 bypass:否则下一个请求立刻被同一峰期再挂一次,
|
|
125
|
+
// 「继续」就变成了「点一下放一条」的假放行。
|
|
96
126
|
try {
|
|
97
|
-
stepGate.markBypass(
|
|
127
|
+
stepGate.markBypass(id)
|
|
98
128
|
bypassed = true
|
|
99
129
|
} catch {
|
|
100
130
|
/* ignore */
|
|
101
131
|
}
|
|
102
132
|
}
|
|
103
|
-
return { released, bypass: bypassed, reason }
|
|
133
|
+
return { released: released || requestReleased, requestReleased, bypass: bypassed, reason }
|
|
104
134
|
},
|
|
105
135
|
/** 读一会话状态。 */
|
|
106
136
|
state(sessionId) {
|
|
@@ -115,6 +145,25 @@ export function createBridge(ctx, gate, store, pauseGate, stepGate) {
|
|
|
115
145
|
stepState = {}
|
|
116
146
|
}
|
|
117
147
|
}
|
|
148
|
+
// v4.2.11(issue #3):请求级 hold 也是一种「暂停」,但它没有 step 门,
|
|
149
|
+
// 所以旧投影里 pausedStep=false → 按钮显示「暂停会话」,用户看不到自己已被挂起。
|
|
150
|
+
let requestHeld = false
|
|
151
|
+
let requestHeldSince = null
|
|
152
|
+
if (deferrals !== undefined && typeof deferrals.has === 'function') {
|
|
153
|
+
try {
|
|
154
|
+
requestHeld = deferrals.has(sessionId) === true
|
|
155
|
+
} catch {
|
|
156
|
+
requestHeld = false
|
|
157
|
+
}
|
|
158
|
+
if (requestHeld && typeof deferrals.list === 'function') {
|
|
159
|
+
try {
|
|
160
|
+
const hit = deferrals.list().find((e) => String(e.sessionId) === String(sessionId))
|
|
161
|
+
requestHeldSince = hit ? (hit.since ?? null) : null
|
|
162
|
+
} catch {
|
|
163
|
+
requestHeldSince = null
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
}
|
|
118
167
|
return {
|
|
119
168
|
sessionId: String(sessionId),
|
|
120
169
|
queueLocked: base.queueLocked === true,
|
|
@@ -127,6 +176,11 @@ export function createBridge(ctx, gate, store, pauseGate, stepGate) {
|
|
|
127
176
|
stepHeldSince: stepState.since ?? null,
|
|
128
177
|
stepBypass: stepState.bypass === true,
|
|
129
178
|
stepManual: stepState.manual === true,
|
|
179
|
+
// 请求级 hold(v4.2.11):与 step 门并列的第二种暂停,按钮文案要认它。
|
|
180
|
+
requestHeld,
|
|
181
|
+
requestHeldSince,
|
|
182
|
+
// 任一来源处于暂停态即为「已暂停」——按钮文案只看这一个字段。
|
|
183
|
+
pausedAny: stepState.held === true || stepState.manual === true || requestHeld,
|
|
130
184
|
taskControlAvailable: gate.taskControlAvailable(),
|
|
131
185
|
taskControl: taskControlState(sessionId),
|
|
132
186
|
updatedAt: base.updatedAt ?? null,
|
|
@@ -38,14 +38,30 @@ export function pauseButtonTitle(state: PauseButtonState): string {
|
|
|
38
38
|
export function readPauseState(body: unknown): PauseButtonState {
|
|
39
39
|
const b = (body ?? {}) as {
|
|
40
40
|
ok?: unknown
|
|
41
|
-
paused?: { step?: unknown; manual?: unknown }
|
|
41
|
+
paused?: { step?: unknown; manual?: unknown; request?: unknown; any?: unknown }
|
|
42
42
|
stepGate?: { since?: unknown; manual?: unknown }
|
|
43
|
-
|
|
43
|
+
requestGate?: { held?: unknown; since?: unknown }
|
|
44
|
+
state?: {
|
|
45
|
+
pausedStep?: unknown
|
|
46
|
+
stepHeldSince?: unknown
|
|
47
|
+
stepManual?: unknown
|
|
48
|
+
requestHeld?: unknown
|
|
49
|
+
requestHeldSince?: unknown
|
|
50
|
+
pausedAny?: unknown
|
|
51
|
+
}
|
|
44
52
|
}
|
|
45
53
|
if (b.ok !== true) return { paused: false, heldSince: null, manual: false }
|
|
46
54
|
const manual = b.paused?.manual === true || b.stepGate?.manual === true || b.state?.stepManual === true
|
|
47
|
-
|
|
48
|
-
const
|
|
55
|
+
// v4.2.11(issue #3):请求级 hold 也是「已暂停」,否则峰期新会话被挂住时按钮仍显示「暂停会话」。
|
|
56
|
+
const requestHeld = b.paused?.request === true || b.requestGate?.held === true || b.state?.requestHeld === true
|
|
57
|
+
const paused =
|
|
58
|
+
b.paused?.any === true ||
|
|
59
|
+
b.state?.pausedAny === true ||
|
|
60
|
+
b.paused?.step === true ||
|
|
61
|
+
b.state?.pausedStep === true ||
|
|
62
|
+
requestHeld ||
|
|
63
|
+
manual
|
|
64
|
+
const raw = b.stepGate?.since ?? b.state?.stepHeldSince ?? b.requestGate?.since ?? b.state?.requestHeldSince ?? null
|
|
49
65
|
const heldSince = typeof raw === 'number' && Number.isFinite(raw) ? raw : null
|
|
50
66
|
return { paused, heldSince, manual }
|
|
51
67
|
}
|
package/src/index.js
CHANGED
|
@@ -33,7 +33,7 @@ import { registerSettingsBridgeRoutes } from './settings-bridge.js'
|
|
|
33
33
|
export const Config = SettingsSchema
|
|
34
34
|
|
|
35
35
|
export const name = 'session-guard'
|
|
36
|
-
export const inject = ['agents', 'webServer', 'timer', '
|
|
36
|
+
export const inject = ['agents', 'webServer', 'timer', 'goals']
|
|
37
37
|
|
|
38
38
|
export { NS, DEFAULT_SETTINGS }
|
|
39
39
|
|
|
@@ -146,6 +146,8 @@ export function apply(ctx, config = {}) {
|
|
|
146
146
|
const pauseStore = createPauseStore()
|
|
147
147
|
let pauseGateRef = null
|
|
148
148
|
let wiringRef = null
|
|
149
|
+
/** 已发过 TUI 门控提示的门控键(`sessionId:since:step`),防止同一门控刷屏。 */
|
|
150
|
+
const notifiedHolds = new Set()
|
|
149
151
|
const stepGate = createStepGate({
|
|
150
152
|
getSettings: readCfg,
|
|
151
153
|
shouldHoldSession: (cfg, id) => (wiringRef ? wiringRef.shouldPauseSession(cfg, id) : true),
|
|
@@ -154,17 +156,50 @@ export function apply(ctx, config = {}) {
|
|
|
154
156
|
pauseGate: { pause: (id, opts) => pauseGateRef.pause(id, opts) },
|
|
155
157
|
logger: ctx.logger,
|
|
156
158
|
onChange: (id, snapshot) => broadcastStep(id, snapshot),
|
|
159
|
+
// TUI 可见反馈:dsh-tui 会把 source.kind !== 'user' 的消息折成一行 `· …`,
|
|
160
|
+
// 所以拉门时向会话追加一条 plugin 源 user/message(同一个门控只发一次)。
|
|
161
|
+
// 失败静默:老宿主没有 session.append 时退回只能 `/guard status` 查。
|
|
162
|
+
onHold: (id, agent, info) => {
|
|
163
|
+
try {
|
|
164
|
+
const session = agent && agent.session
|
|
165
|
+
if (!session || typeof session.append !== 'function') return
|
|
166
|
+
const since = Number(info && info.since) || 0
|
|
167
|
+
const key = `${id}:${since}:${info && info.step}`
|
|
168
|
+
if (notifiedHolds.has(key)) return
|
|
169
|
+
notifiedHolds.add(key)
|
|
170
|
+
if (notifiedHolds.size > 200) notifiedHolds.clear()
|
|
171
|
+
const reason = info && info.manual
|
|
172
|
+
? '手动暂停'
|
|
173
|
+
: (info && info.why === 'peak' ? '高峰拦截' : `门控(${(info && info.why) || 'hold'})`)
|
|
174
|
+
const text = `⏳ session-guard:${reason} — 本回合已在 step 边界挂起`
|
|
175
|
+
+ `(step ${(info && info.step) || '?'})。/guard status 查看,/guard resume 继续。`
|
|
176
|
+
session.append('user/message', {
|
|
177
|
+
id: `guard-${(info && info.since) || Date.now()}`,
|
|
178
|
+
role: 'user',
|
|
179
|
+
content: [{ type: 'text', text }],
|
|
180
|
+
source: { kind: 'plugin', name: 'session-guard' },
|
|
181
|
+
}, { surfaceOp: 'append' })
|
|
182
|
+
} catch (e) {
|
|
183
|
+
ctx.logger?.warn?.(`session-guard: tui notice failed: ${String((e && e.message) || e)}`)
|
|
184
|
+
}
|
|
185
|
+
},
|
|
157
186
|
})
|
|
158
187
|
const pauseGate = createPauseGate({ ctx, pauseStore, stepGate })
|
|
159
188
|
pauseGateRef = pauseGate
|
|
160
189
|
const gate = createGate({ getCtx: () => ctx, getSettings: readCfg, store, pauseGate })
|
|
161
|
-
|
|
190
|
+
// deferrals 在 createWiring 里才构造;先留引用,bridge 用 late-bound 读(构造期循环依赖)。
|
|
191
|
+
let deferralsRef = null
|
|
192
|
+
const bridge = createBridge(ctx, gate, store, pauseGate, stepGate, {
|
|
193
|
+
settle: (id, reason, outcome) => (deferralsRef ? deferralsRef.settle(id, reason, outcome) : false),
|
|
194
|
+
clearDeferred: (id) => (deferralsRef ? deferralsRef.clearDeferred(id) : false),
|
|
195
|
+
})
|
|
162
196
|
|
|
163
197
|
// ── 官方 provider 二维判定接线(目标追踪 / 请求级守卫 / 入峰过滤 / 退峰释放)──
|
|
164
198
|
// 纯逻辑见 provider.js / provider-directory.js / deferrals.js / request-guard.js / targets.js,
|
|
165
199
|
// 接线与编排见 wiring.js;这里只负责装配与生命周期。
|
|
166
200
|
const wiring = createWiring({ ctx, getSettings: readCfg, gate, stepGate, logger: ctx.logger })
|
|
167
201
|
wiringRef = wiring
|
|
202
|
+
deferralsRef = wiring.deferrals
|
|
168
203
|
|
|
169
204
|
// ── 冗余端口:input-traffic 冻结按钮透传接入(D5/D6/D8)──
|
|
170
205
|
ctx.provide('sessionGuard', bridge)
|
|
@@ -194,9 +229,12 @@ export function apply(ctx, config = {}) {
|
|
|
194
229
|
ctx.effect(() => () => wiring.dispose(), 'session-guard: wiring dispose')
|
|
195
230
|
|
|
196
231
|
// ── 手动会话门命令(/pause /resume /cancel,全量移植)──
|
|
197
|
-
|
|
232
|
+
// 门控:命令注册走 inject(['commands']),不写进顶层 inject——否则缺 commands 服务的
|
|
233
|
+
// 老宿主线会把整个插件挂死(兼容闸见 _tools/tui-audit.mjs)。
|
|
234
|
+
const installSessionGuardCommands = (commandCtx) => {
|
|
235
|
+
if (typeof commandCtx?.commands?.register !== 'function') return
|
|
198
236
|
const tokensOf = (rawInput) => String(rawInput ?? '').trim().split(/\s+/).filter(Boolean)
|
|
199
|
-
ctx.effect(() =>
|
|
237
|
+
ctx.effect(() => commandCtx.commands.register({
|
|
200
238
|
name: 'pause',
|
|
201
239
|
description: 'pause the running task (safe: defers to the safe boundary; force: interrupts tools and reasoning now; wait: let reasoning finish; bare /pause follows the pause settings)',
|
|
202
240
|
input: { hint: '[force|safe] [stop|wait]' },
|
|
@@ -211,7 +249,7 @@ export function apply(ctx, config = {}) {
|
|
|
211
249
|
return pauseGate.pause(sid, opts)
|
|
212
250
|
},
|
|
213
251
|
}))
|
|
214
|
-
ctx.effect(() =>
|
|
252
|
+
ctx.effect(() => commandCtx.commands.register({
|
|
215
253
|
name: 'resume',
|
|
216
254
|
description: 'resume the paused task and continue from the pause point (a force-paused task with an interrupted tool needs `confirm`, plus `rerun`/`skip` for the tool)',
|
|
217
255
|
input: { hint: '[confirm] [rerun|skip]' },
|
|
@@ -227,7 +265,7 @@ export function apply(ctx, config = {}) {
|
|
|
227
265
|
})
|
|
228
266
|
},
|
|
229
267
|
}))
|
|
230
|
-
ctx.effect(() =>
|
|
268
|
+
ctx.effect(() => commandCtx.commands.register({
|
|
231
269
|
name: 'cancel',
|
|
232
270
|
description: 'cancel the running task (stops the current turn immediately, keeps the queue)',
|
|
233
271
|
handler: (invocation) => {
|
|
@@ -237,7 +275,71 @@ export function apply(ctx, config = {}) {
|
|
|
237
275
|
return pauseGate.cancel(sid)
|
|
238
276
|
},
|
|
239
277
|
}))
|
|
278
|
+
|
|
279
|
+
// ── dsh-tui 终端面:`/guard` ───────────────────────────────────
|
|
280
|
+
// TUI 的 routeSlash 先命中本地命令表,`/resume` 在那里被 dsh-tui 自己的
|
|
281
|
+
// “恢复会话”吃掉 → 宿主的 resume 在 TUI 里永远不可达。`/guard resume` 是
|
|
282
|
+
// 不依赖那个名字的入口;status 则补上“门到底关没关”这一眼。
|
|
283
|
+
ctx.effect(() => commandCtx.commands.register({
|
|
284
|
+
name: 'guard',
|
|
285
|
+
description: 'session guard console: status / pause / resume / cancel (TUI-safe wrapper; /resume is shadowed by dsh-tui local commands)',
|
|
286
|
+
input: { hint: '[status|pause|resume|cancel] [force|safe] [confirm] [rerun|skip]' },
|
|
287
|
+
handler: (invocation) => {
|
|
288
|
+
const sid = String(invocation?.agent?.id ?? '')
|
|
289
|
+
if (!sid) return { kind: 'error', text: 'guard: no session for this command' }
|
|
290
|
+
const tokens = tokensOf(invocation?.rawInput)
|
|
291
|
+
const wantsJson = tokens.includes('--json')
|
|
292
|
+
const sub = String(tokens.shift() ?? 'status').toLowerCase()
|
|
293
|
+
if (sub === 'status') {
|
|
294
|
+
let state
|
|
295
|
+
try { state = bridge.state(sid) ?? {} } catch { state = {} }
|
|
296
|
+
const primitives = Object.entries(state)
|
|
297
|
+
.filter(([, value]) => ['string', 'number', 'boolean'].includes(typeof value))
|
|
298
|
+
.slice(0, 10)
|
|
299
|
+
.map(([key, value]) => `${key}=${String(value)}`)
|
|
300
|
+
const paused = state.paused === true || state.queueLocked === true
|
|
301
|
+
// 契约:单行 <=100(上限),实际取 80 —— 80 列终端下也不横向溢出。
|
|
302
|
+
const clipLine = (text, width = 80) => {
|
|
303
|
+
const value = String(text ?? '').replace(/[\r\n]+/g, ' ').replace(/\s+$/, '')
|
|
304
|
+
return value.length <= width ? value : `${value.slice(0, width - 1)}…`
|
|
305
|
+
}
|
|
306
|
+
const lines = [
|
|
307
|
+
clipLine(`guard — ${paused ? 'paused/locked' : 'open'}`),
|
|
308
|
+
clipLine(` state ${primitives.join(' ') || '(no live state in this host)'}`),
|
|
309
|
+
clipLine(' map /guard resume [confirm] [rerun|skip] · /guard pause [force|safe] · /guard cancel'),
|
|
310
|
+
]
|
|
311
|
+
if (wantsJson) {
|
|
312
|
+
return {
|
|
313
|
+
kind: 'success',
|
|
314
|
+
text: JSON.stringify({ ok: true, cmd: 'guard', sub: 'status', data: { paused, state: primitives } }),
|
|
315
|
+
}
|
|
316
|
+
}
|
|
317
|
+
return { kind: 'success', text: lines.join('\n') }
|
|
318
|
+
}
|
|
319
|
+
if (sub === 'pause') {
|
|
320
|
+
const opts = {}
|
|
321
|
+
for (const token of tokens) {
|
|
322
|
+
if (token === 'force' || token === 'safe') opts.mode = token
|
|
323
|
+
if (token === 'stop' || token === 'wait') opts.reason = token
|
|
324
|
+
}
|
|
325
|
+
return pauseGate.pause(sid, opts)
|
|
326
|
+
}
|
|
327
|
+
if (sub === 'resume') {
|
|
328
|
+
bridge.stepResume(sid, { bypass: true, reason: 'command' })
|
|
329
|
+
return pauseGate.resume(sid, {
|
|
330
|
+
confirm: tokens.includes('confirm'),
|
|
331
|
+
choice: tokens.includes('skip') ? 'skip' : 'rerun',
|
|
332
|
+
})
|
|
333
|
+
}
|
|
334
|
+
if (sub === 'cancel') {
|
|
335
|
+
bridge.stepResume(sid, { bypass: false, reason: 'command' })
|
|
336
|
+
return pauseGate.cancel(sid)
|
|
337
|
+
}
|
|
338
|
+
return { kind: 'error', text: `guard: unknown subcommand "${sub}"\n next: /guard status` }
|
|
339
|
+
},
|
|
340
|
+
}))
|
|
240
341
|
}
|
|
342
|
+
ctx.inject?.(['commands'], installSessionGuardCommands)
|
|
241
343
|
|
|
242
344
|
// ── 后端自动重试(D9):冻结/门控期间让路,绝不绕过会话门 ──
|
|
243
345
|
createRetry({
|
|
@@ -330,13 +432,25 @@ export function apply(ctx, config = {}) {
|
|
|
330
432
|
state: st,
|
|
331
433
|
// v0.2.0:PRD §6.2 的 `paused: { step, turn }` 形状(顶层,兼容既有 state.paused 布尔)
|
|
332
434
|
// step = 已拉门;manual = 已请求但还没到边界(按钮同样显示「继续会话」)
|
|
333
|
-
|
|
435
|
+
// v4.2.11(issue #3):request = 请求级 hold。它也属于「已暂停」,
|
|
436
|
+
// 旧投影不含这一项 → 峰期新会话被挂住时按钮仍显示「暂停会话」,用户找不到放行入口。
|
|
437
|
+
paused: {
|
|
438
|
+
step: st.pausedStep === true,
|
|
439
|
+
turn: st.paused === true,
|
|
440
|
+
manual: st.stepManual === true,
|
|
441
|
+
request: st.requestHeld === true,
|
|
442
|
+
any: st.pausedAny === true,
|
|
443
|
+
},
|
|
334
444
|
stepGate: {
|
|
335
445
|
held: st.pausedStep === true,
|
|
336
446
|
manual: st.stepManual === true,
|
|
337
447
|
since: st.stepHeldSince,
|
|
338
448
|
bypass: st.stepBypass === true,
|
|
339
449
|
},
|
|
450
|
+
requestGate: {
|
|
451
|
+
held: st.requestHeld === true,
|
|
452
|
+
since: st.requestHeldSince,
|
|
453
|
+
},
|
|
340
454
|
target: wiring.targets.get(sessionId),
|
|
341
455
|
held: wiring.deferrals.has(sessionId),
|
|
342
456
|
deferred: wiring.deferrals.isDeferred(sessionId),
|
package/src/request-guard.js
CHANGED
|
@@ -67,6 +67,8 @@ export function createRequestGuard({
|
|
|
67
67
|
clock = () => new Date(),
|
|
68
68
|
isRoot,
|
|
69
69
|
scheduleRelease,
|
|
70
|
+
/** v4.2.11(issue #3):读「本峰内用户是否已手动放行该会话」;缺省恒 false。 */
|
|
71
|
+
isBypassed = () => false,
|
|
70
72
|
}) {
|
|
71
73
|
const rootCheck = isRoot ?? makeIsRoot(ctx)
|
|
72
74
|
const counters = { pass: 0, hold: 0, error: 0, skippedSubagent: 0, failOpen: 0 }
|
|
@@ -100,6 +102,21 @@ export function createRequestGuard({
|
|
|
100
102
|
const verdict = shouldPause(cfg, clock())
|
|
101
103
|
if (!verdict.pause) return { action: 'pass', why: verdict.reason }
|
|
102
104
|
|
|
105
|
+
// v4.2.11(issue #3):用户已手动「继续」该会话 → 本峰内直接放行。
|
|
106
|
+
// 没有这一条,`stepResume` 结算掉当前 hold 之后,下一个请求会立刻被同一峰期再挂一次,
|
|
107
|
+
// 「继续会话」就退化成「点一下放一条」的假放行。
|
|
108
|
+
let bypassed
|
|
109
|
+
try {
|
|
110
|
+
bypassed = isBypassed(sessionId) === true
|
|
111
|
+
} catch {
|
|
112
|
+
bypassed = false
|
|
113
|
+
}
|
|
114
|
+
if (bypassed) {
|
|
115
|
+
counters.pass += 1
|
|
116
|
+
logger?.info?.(`[session-guard] peak but ${sessionId} was manually continued — pass (bypassed)`)
|
|
117
|
+
return { action: 'pass', why: 'bypassed' }
|
|
118
|
+
}
|
|
119
|
+
|
|
103
120
|
const cls = directory.classify(config && config.provider)
|
|
104
121
|
if (!cls.official) {
|
|
105
122
|
logger?.info?.(`[session-guard] peak but target "${String(config && config.provider)}" is not official (matchedBy=${cls.matchedBy}) — pass`)
|
package/src/step-gate.js
CHANGED
|
@@ -93,6 +93,7 @@ export function decideStepHold({
|
|
|
93
93
|
* @param {typeof clearTimeout} [deps.clearTimer]
|
|
94
94
|
* @param {boolean} [deps.unrefTimers]
|
|
95
95
|
* @param {(sessionId:string, state:object)=>void} [deps.onChange] 门控状态变化回调(SSE 推送用)
|
|
96
|
+
* @param {(sessionId:string, agent:object, info:object)=>void} [deps.onHold] 真正拉门时的回调(TUI 可见反馈用)
|
|
96
97
|
*/
|
|
97
98
|
export function createStepGate({
|
|
98
99
|
getSettings,
|
|
@@ -106,6 +107,7 @@ export function createStepGate({
|
|
|
106
107
|
clearTimer = clearTimeout,
|
|
107
108
|
unrefTimers = true,
|
|
108
109
|
onChange,
|
|
110
|
+
onHold,
|
|
109
111
|
} = {}) {
|
|
110
112
|
/** sessionId -> { sessionId, step, turn, since, resolve, timer, signal, onAbort } */
|
|
111
113
|
const holds = new Map()
|
|
@@ -349,6 +351,16 @@ export function createStepGate({
|
|
|
349
351
|
holds.set(id, entry)
|
|
350
352
|
emitChange(id)
|
|
351
353
|
|
|
354
|
+
// TUI/终端可见反馈:dsh-tui 把 source.kind !== 'user' 的消息折叠成一行 `· …`,
|
|
355
|
+
// 所以拉门时向会话追加一条 plugin 源 user/message(一次门控只发一次,由 onHold 去重)。
|
|
356
|
+
try {
|
|
357
|
+
if (typeof onHold === 'function') {
|
|
358
|
+
onHold(id, agent, { why: decision.why, step: entry.step, turn: entry.turn, manual, since: entry.since })
|
|
359
|
+
}
|
|
360
|
+
} catch (e) {
|
|
361
|
+
warn(`step gate onHold failed: ${String((e && e.message) || e)}`)
|
|
362
|
+
}
|
|
363
|
+
|
|
352
364
|
// abort(agent.cancel / 冻结 / /pause / /cancel)→ 释放,让 loop 的 throwIfAborted 收尾
|
|
353
365
|
if (entry.signal && typeof entry.signal.addEventListener === 'function') {
|
|
354
366
|
entry.onAbort = () => release(id, 'abort')
|
package/src/wiring.js
CHANGED
|
@@ -96,6 +96,9 @@ export function createWiring({
|
|
|
96
96
|
logger,
|
|
97
97
|
clock,
|
|
98
98
|
scheduleRelease,
|
|
99
|
+
// v4.2.11(issue #3):请求级 hold 认 step 门上的「本峰 bypass」。
|
|
100
|
+
// step 门与请求门共用同一个 bypass 集合,用户手动「继续」对两条路径同时生效。
|
|
101
|
+
isBypassed: (id) => (stepGate ? stepGate.state(id).bypass === true : false),
|
|
99
102
|
})
|
|
100
103
|
|
|
101
104
|
/** `session/event` → 目标追踪(形状异常降级 unknown,不抛出)+ 目标转非官方时自动恢复。 */
|