@sshirkit/dsh-orchestrator 0.8.5 → 0.8.8

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/lib/gate.js CHANGED
@@ -38,6 +38,41 @@ import { RUN_CODE_TOOL, runCodeRefusal } from './run-code-guard.js'
38
38
 
39
39
  export { DELEGATE_TOOL }
40
40
 
41
+ export const SUPERVISION_TODO_DENIAL = '[dsh-orchestrator] 当前为定时巡检回合,已禁止调用 todo_write。子代理仍在运行,请勿推进主任务清单状态;如需纠偏请直接通过调研或 send_message / interrupt_agent 等方式处理,无需干预请仅回复「巡检:正常」。任务进度请在子代理交付结算(subagent-settled)或正常用户回合再更新。'
42
+ export const SUPERVISION_NORMAL_DENIAL = '[dsh-orchestrator] 本回合已输出「巡检:正常」确认无需干预,已锁定后续工具调用,请立即结束本回合,勿再调用任何工具。'
43
+ const SUPERVISION_WHITELIST_DENIAL = 'orchestrator: The requested tool is not in allowedTools for the current supervision turn. Use currently allowed tools to handle existing workers, or reply "巡检:正常" and end the turn.'
44
+
45
+ /** Bind supervision restrictions to one root agent and one exact turn. */
46
+ export function createSupervisionTurnState() {
47
+ const turns = new Map()
48
+ const isTurnActive = (agentId, turn) => turns.has(agentId) && turns.get(agentId).turn === turn
49
+ return {
50
+ bindTurn(agentId, turn) {
51
+ turns.set(agentId, { turn, normalDeclared: false })
52
+ },
53
+ markNormalDeclared(agentId, turn) {
54
+ if (isTurnActive(agentId, turn)) turns.get(agentId).normalDeclared = true
55
+ },
56
+ isTurnActive,
57
+ clearTurn(agentId, turn) {
58
+ if (isTurnActive(agentId, turn)) turns.delete(agentId)
59
+ },
60
+ clearAgent: (agentId) => turns.delete(agentId),
61
+ checkTool(agentId, currentTurn, toolName, { whitelistMiss = false } = {}) {
62
+ const record = turns.get(agentId)
63
+ if (record === undefined) return undefined
64
+ if (currentTurn !== undefined && record.turn !== currentTurn) {
65
+ turns.delete(agentId)
66
+ return undefined
67
+ }
68
+ if (toolName === 'todo_write') return SUPERVISION_TODO_DENIAL
69
+ if (record.normalDeclared) return SUPERVISION_NORMAL_DENIAL
70
+ if (whitelistMiss) return SUPERVISION_WHITELIST_DENIAL
71
+ return undefined
72
+ },
73
+ }
74
+ }
75
+
41
76
  /**
42
77
  * Whether an agent is a delegated child. Mirrors the host's `delegationDepthOf`
43
78
  * (`Math.max(header.delegationDepth ?? 0, options.subagentDepth ?? 0)`): the
@@ -160,7 +195,7 @@ export function allowGuard(allowed) {
160
195
  * or a `run_code` program that would reach the host. A guard failure is reported, not
161
196
  * thrown: a broken gate must not break agent creation.
162
197
  */
163
- export function installGate({ agent, allowed, enabled = () => true }) {
198
+ export function installGate({ agent, allowed, enabled = () => true, checkSupervision }) {
164
199
  const liveAllowed = typeof allowed === 'function' ? allowed : () => allowed ?? []
165
200
  let guardError
166
201
  let guardDispose
@@ -168,6 +203,10 @@ export function installGate({ agent, allowed, enabled = () => true }) {
168
203
  guardDispose = agent.ctx.tools.guard((exec) => {
169
204
  const tool = exec?.name
170
205
  if (typeof tool !== 'string' || tool === '') return undefined
206
+ const caller = exec?.agent ?? agent
207
+ if (isDelegatedChild(caller)) return undefined
208
+ const supervisionDenial = checkSupervision?.(caller, tool)
209
+ if (typeof supervisionDenial === 'string' && supervisionDenial !== '') return supervisionDenial
171
210
  if (!enabled()) {
172
211
  if (tool === DELEGATE_TOOL || tool === ORCHESTRATE_TOOL) {
173
212
  return disabledToolMessage(tool)
@@ -178,8 +217,9 @@ export function installGate({ agent, allowed, enabled = () => true }) {
178
217
  if (list.includes(tool)) return undefined
179
218
  // `run_code` is the PTC transport: it stays callable unless the panel checks it, and
180
219
  // the arguments that would escape the gate are refused instead.
181
- if (tool === RUN_CODE_TOOL) return runCodeRefusal(exec?.arguments, true)
182
- return refusalFor(tool)
220
+ const refusal = tool === RUN_CODE_TOOL ? runCodeRefusal(exec?.arguments, true) : refusalFor(tool)
221
+ if (refusal === undefined) return undefined
222
+ return checkSupervision?.(caller, tool, { whitelistMiss: true }) ?? refusal
183
223
  })
184
224
  } catch (error) {
185
225
  guardError = error instanceof Error ? error.message : String(error)