dsh-chief 1.4.0 → 1.6.0

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.
@@ -10,10 +10,13 @@ import os from 'node:os'
10
10
  import { answerMemory } from './memory.js'
11
11
  import {
12
12
  loadChiefState,
13
+ resolveWorkspaceConfig,
14
+ appendChiefAuditLog,
13
15
  getLinkedCluster,
14
16
  collectClusterPeerProgress,
15
17
  deliverCrossMessage,
16
18
  } from './chief.js'
19
+ import { resolveAlignedModelEffort } from './chief-state.js'
17
20
  import {
18
21
  API,
19
22
  newId,
@@ -23,6 +26,7 @@ import {
23
26
  CAP_TASK_INTENT,
24
27
  asBool,
25
28
  normalizeTask,
29
+ resolveActivePlanStepId,
26
30
  pushLog,
27
31
  safe,
28
32
  } from './board-schema.js'
@@ -156,6 +160,25 @@ export function createSubagentRuntime(deps) {
156
160
  return getSubagentRoster(rt, rootId, board)
157
161
  }
158
162
 
163
+ function readAgentReasoningEffort(ag) {
164
+ if (!ag || typeof ag !== 'object') return ''
165
+ try {
166
+ if (ag.model && typeof ag.model.current === 'function') {
167
+ const cur = ag.model.current()
168
+ if (cur && typeof cur.reasoningEffort === 'string' && cur.reasoningEffort) return cur.reasoningEffort
169
+ }
170
+ if (ag.session && typeof ag.session.requestHeader === 'function') {
171
+ const h = ag.session.requestHeader()
172
+ if (h && h.config && typeof h.config.reasoningEffort === 'string') return h.config.reasoningEffort
173
+ }
174
+ if (ag.session && ag.session.modelSelection && typeof ag.session.modelSelection.reasoningEffort === 'string') {
175
+ return ag.session.modelSelection.reasoningEffort
176
+ }
177
+ if (ag.config && typeof ag.config.reasoningEffort === 'string') return ag.config.reasoningEffort
178
+ } catch {}
179
+ return ''
180
+ }
181
+
159
182
  /** Synchronous roster combining cached subagent tree + live agent status + board task assignments. */
160
183
  function getSubagentRoster(runtime, sessionId, board) {
161
184
  const rt = runtime || deps.hostState.activeRuntime
@@ -163,6 +186,11 @@ export function createSubagentRuntime(deps) {
163
186
  const agents = rt ? rt.get('agents') : null
164
187
  const cached = deps.hostState.subagentTreeCache.get(rootId) || []
165
188
  const byName = new Map()
189
+ let locks = {}
190
+ try {
191
+ const cfg = resolveWorkspaceConfig(loadChiefState(), rootId)
192
+ locks = (cfg && cfg.lockedModelEfforts) || {}
193
+ } catch {}
166
194
 
167
195
  const liveStatus = (id, fallback) => {
168
196
  try {
@@ -173,11 +201,22 @@ export function createSubagentRuntime(deps) {
173
201
  } catch {}
174
202
  return fallback || 'inactive'
175
203
  }
204
+ const liveEffort = (id) => {
205
+ try {
206
+ if (agents && typeof agents.get === 'function' && id) {
207
+ return readAgentReasoningEffort(agents.get(id))
208
+ }
209
+ } catch {}
210
+ return ''
211
+ }
176
212
 
177
213
  for (const row of cached) {
214
+ const short = String(row.name || '').split('/').pop()
178
215
  byName.set(row.name, {
179
216
  ...row,
180
217
  status: liveStatus(row.id, row.status),
218
+ reasoningEffort: liveEffort(row.id) || row.reasoningEffort || '',
219
+ effortLocked: Boolean(locks[row.name] || locks[short] || locks[row.id]),
181
220
  tasks: [],
182
221
  })
183
222
  }
@@ -187,6 +226,8 @@ export function createSubagentRuntime(deps) {
187
226
  id: rootId,
188
227
  name: 'lead',
189
228
  status: liveStatus(rootId, 'inactive'),
229
+ reasoningEffort: liveEffort(rootId),
230
+ effortLocked: Boolean(locks.lead || locks[rootId]),
190
231
  description: '主代理 (Root Coordinator)',
191
232
  mode: 'continuable',
192
233
  tasks: [],
@@ -198,10 +239,13 @@ export function createSubagentRuntime(deps) {
198
239
  const ownerRaw = String(t.subagent || t.owner || '').trim()
199
240
  if (!ownerRaw || ownerRaw === 'agent' || ownerRaw === 'operator' || ownerRaw === 'user') continue
200
241
  if (!byName.has(ownerRaw)) {
242
+ const short = ownerRaw.split('/').pop()
201
243
  byName.set(ownerRaw, {
202
244
  id: String(t.session || ''),
203
245
  name: ownerRaw,
204
246
  status: t.status === 'doing' ? liveStatus(t.session, 'running') : 'inactive',
247
+ reasoningEffort: liveEffort(t.session),
248
+ effortLocked: Boolean(locks[ownerRaw] || locks[short] || (t.session && locks[t.session])),
205
249
  description: t.title || '',
206
250
  mode: 'continuable',
207
251
  tasks: [],
@@ -471,6 +515,17 @@ export function createSubagentRuntime(deps) {
471
515
  '- **任务标题**: ' + task.title,
472
516
  ]
473
517
  if (board.objective) lines.push('- **全局目标**: ' + board.objective.slice(0, 400))
518
+ const planSteps = Array.isArray(board && board.plan) ? board.plan : []
519
+ const stepIdx = task.planStepId ? planSteps.findIndex(s => s && s.id === task.planStepId) : -1
520
+ if (stepIdx >= 0) {
521
+ const targetIdx = task.targetPlanStepId && task.targetPlanStepId !== task.planStepId
522
+ ? planSteps.findIndex(s => s && s.id === task.targetPlanStepId)
523
+ : -1
524
+ const returnHint = targetIdx >= 0
525
+ ? ';完成后成果回指第 ' + (targetIdx + 1) + ' 步「' + planSteps[targetIdx].text + '」'
526
+ : ''
527
+ lines.push('- **所属推进步骤**: 第 ' + (stepIdx + 1) + ' 步「' + planSteps[stepIdx].text + '」' + returnHint)
528
+ }
474
529
  if (task.intent) lines.push('- **任务意图 (Intent)**: ' + task.intent)
475
530
  if (task.evidence) lines.push('- **验收标准 (Expected Evidence)**: ' + task.evidence)
476
531
  if (task.note) lines.push('- **补充说明 (Note)**: ' + task.note)
@@ -761,6 +816,11 @@ export function createSubagentRuntime(deps) {
761
816
  const otherTokens = Math.max(0, usedTokens - overheadTokens - messageTokens)
762
817
  const pct = contextWindow > 0 ? Math.min(100, Math.round((usedTokens / contextWindow) * 100)) : 0
763
818
 
819
+ let effortLocked = false
820
+ try {
821
+ const cfg = resolveWorkspaceConfig(loadChiefState(), rootSid || sid)
822
+ effortLocked = Boolean(cfg && cfg.lockedModelEfforts && cfg.lockedModelEfforts.lead)
823
+ } catch {}
764
824
  return {
765
825
  totalTokens: usedTokens,
766
826
  usedTokens,
@@ -772,6 +832,8 @@ export function createSubagentRuntime(deps) {
772
832
  otherTokens,
773
833
  overheadTokens,
774
834
  modelRef,
835
+ reasoningEffort: readAgentReasoningEffort(ag),
836
+ effortLocked,
775
837
  seq,
776
838
  source,
777
839
  }
@@ -907,12 +969,195 @@ export function createSubagentRuntime(deps) {
907
969
  })
908
970
  },
909
971
  executeSubagentAction: (input, callerAgent, signal) => executeSubagentAction(rt, input, callerAgent, signal),
910
- async controlMainAgent({ rootId = '', sessionId = '', op = 'steer', modelRef = '', reasoningEffort = '', message = '', reason = '' } = {}) {
972
+ async controlMainAgent({ rootId = '', sessionId = '', op = 'steer', target = '', modelRef = '', reasoningEffort = '', message = '', reason = '', byUser = false } = {}) {
911
973
  const targetSid = String(sessionId || rootId || '').trim()
912
974
  if (!targetSid || targetSid === 'global') {
913
975
  return { ok: false, error: '当前为空白未绑定会话,无法直接控制主对话代理' }
914
976
  }
915
977
  const ctrl = rt ? rt.get('sessionController') : null
978
+ if (op === 'set_effort') {
979
+ const wantRaw = String(reasoningEffort || '').trim().toLowerCase()
980
+ const validEff = ['off', 'low', 'medium', 'high', 'xhigh'].includes(wantRaw) ? wantRaw : 'medium'
981
+ const fallbackTarget = !target && modelRef && !String(modelRef).includes('/') ? String(modelRef) : ''
982
+ const rawTarget = String(target || fallbackTarget || '').trim().replace(/^@/, '')
983
+ const isLead = !rawTarget || rawTarget === 'lead' || rawTarget === 'main' || rawTarget === targetSid
984
+ const targetKey = isLead ? 'lead' : rawTarget
985
+ const rootKey = String(rootId || resolveRootSessionId(targetSid) || targetSid).trim()
986
+ const effCfg = resolveWorkspaceConfig(loadChiefState(), rootKey)
987
+ const locks = (effCfg && effCfg.lockedModelEfforts) || {}
988
+
989
+ const agents = rt ? rt.get('agents') : null
990
+ const subagents = rt ? rt.get('subagents') : null
991
+ let resolvedAgentId = isLead ? targetSid : ''
992
+ let targetAgent = isLead && agents && typeof agents.get === 'function' ? agents.get(targetSid) : null
993
+
994
+ if (!isLead) {
995
+ const leadAg = agents && typeof agents.get === 'function' ? agents.get(targetSid) : null
996
+ if (subagents && typeof subagents.resolveName === 'function' && leadAg) {
997
+ try {
998
+ resolvedAgentId = await subagents.resolveName(leadAg, targetKey, new AbortController().signal)
999
+ } catch {}
1000
+ }
1001
+ if (!resolvedAgentId) {
1002
+ const board = loadBoard(boardFileFor(rootKey))
1003
+ const roster = getSubagentRoster(rt, rootKey, board)
1004
+ const matched = roster.find(r => r.name === targetKey || r.name.endsWith('/' + targetKey) || r.id === targetKey)
1005
+ if (matched && matched.id) resolvedAgentId = matched.id
1006
+ }
1007
+ if (!resolvedAgentId && agents && typeof agents.get === 'function' && agents.get(targetKey)) {
1008
+ resolvedAgentId = targetKey
1009
+ }
1010
+ if (resolvedAgentId && agents && typeof agents.get === 'function') {
1011
+ targetAgent = agents.get(resolvedAgentId)
1012
+ }
1013
+ }
1014
+
1015
+ if (!targetAgent && resolvedAgentId && ctrl && typeof ctrl.resolveAgent === 'function') {
1016
+ try {
1017
+ const resolved = await ctrl.resolveAgent(resolvedAgentId)
1018
+ if (resolved && resolved.agent) targetAgent = resolved.agent
1019
+ } catch {}
1020
+ }
1021
+
1022
+ const shortTarget = targetKey.split('/').pop()
1023
+ const isManualTeamEffort = Boolean(effCfg && effCfg.teamEffortMode === 'manual')
1024
+ if (!byUser && isManualTeamEffort) {
1025
+ appendChiefAuditLog(rootKey, {
1026
+ mode: effCfg.permissionMode,
1027
+ stage: 'model_effort_lock',
1028
+ decision: 'blocked',
1029
+ operation: '调节模型思考强度',
1030
+ target: targetKey,
1031
+ reason: `团队思考强度处于「手动」模式,阻止参谋将 ${targetKey} 调为 ${validEff}`,
1032
+ outcome: 'blocked',
1033
+ by: 'chief',
1034
+ })
1035
+ return {
1036
+ ok: false,
1037
+ lockedByUser: true,
1038
+ manualMode: true,
1039
+ target: targetKey,
1040
+ error: '当前团队思考强度已设为「手动」模式,参谋不会自动调节主/子代理思考强度',
1041
+ badge: '✋ 团队调挡已设为手动',
1042
+ }
1043
+ }
1044
+ const isLocked = Boolean(
1045
+ locks[targetKey] === true
1046
+ || locks[shortTarget] === true
1047
+ || (resolvedAgentId && locks[resolvedAgentId] === true)
1048
+ )
1049
+ if (!byUser && isLocked) {
1050
+ appendChiefAuditLog(rootKey, {
1051
+ mode: effCfg.permissionMode,
1052
+ stage: 'model_effort_lock',
1053
+ decision: 'blocked',
1054
+ operation: '调节模型思考强度',
1055
+ target: targetKey,
1056
+ reason: `用户已开启「🔒 锁挡」,阻止参谋将 ${targetKey} 调为 ${validEff}`,
1057
+ outcome: 'blocked',
1058
+ by: 'chief',
1059
+ })
1060
+ return {
1061
+ ok: false,
1062
+ lockedByUser: true,
1063
+ target: targetKey,
1064
+ error: `用户已锁定 ${isLead ? '主对话 (@lead)' : '@' + targetKey} 的思考强度(🔒),参谋无权覆盖`,
1065
+ badge: '🔒 用户已锁挡',
1066
+ }
1067
+ }
1068
+
1069
+ let curModel = null
1070
+ try {
1071
+ if (targetAgent && targetAgent.model && typeof targetAgent.model.current === 'function') {
1072
+ curModel = targetAgent.model.current()
1073
+ }
1074
+ } catch {}
1075
+ let provider = String((curModel && curModel.provider) || '').trim()
1076
+ let model = String((curModel && curModel.model) || '').trim()
1077
+ if ((!provider || !model) && modelRef && modelRef.includes('/')) {
1078
+ const idx = modelRef.indexOf('/')
1079
+ provider = modelRef.slice(0, idx).trim()
1080
+ model = modelRef.slice(idx + 1).trim()
1081
+ }
1082
+ if ((!provider || !model) && isLead) {
1083
+ const bd = getSessionContextBreakdown(rt, targetSid, targetAgent)
1084
+ if (bd && bd.modelRef && bd.modelRef.includes('/')) {
1085
+ const idx = bd.modelRef.indexOf('/')
1086
+ provider = bd.modelRef.slice(0, idx).trim()
1087
+ model = bd.modelRef.slice(idx + 1).trim()
1088
+ }
1089
+ }
1090
+
1091
+ let finalEffort = validEff
1092
+ const llm = deps.hostState.activeLlm || (rt ? rt.get('llm') : null)
1093
+ if (llm && typeof llm.resolveModelInfo === 'function' && provider && model) {
1094
+ try {
1095
+ const info = await llm.resolveModelInfo(provider, model)
1096
+ if (info && info.reasoning && Array.isArray(info.reasoning.efforts)) {
1097
+ if (info.reasoning.efforts.length === 0) {
1098
+ return {
1099
+ ok: false,
1100
+ nonReasoningModel: true,
1101
+ error: `当前模型 ${provider}/${model} 不支持思考强度调节`,
1102
+ }
1103
+ }
1104
+ finalEffort = resolveAlignedModelEffort(validEff, info.reasoning.efforts) || validEff
1105
+ }
1106
+ } catch {}
1107
+ }
1108
+
1109
+ let applied = false
1110
+ try {
1111
+ if (targetAgent && targetAgent.model && typeof targetAgent.model.select === 'function') {
1112
+ await targetAgent.model.select({
1113
+ ...(curModel && typeof curModel === 'object' ? curModel : {}),
1114
+ ...(provider ? { provider } : {}),
1115
+ ...(model ? { model } : {}),
1116
+ reasoningEffort: finalEffort,
1117
+ })
1118
+ applied = true
1119
+ } else if (isLead && ctrl && typeof ctrl.selectModel === 'function' && model) {
1120
+ await ctrl.selectModel({
1121
+ sessionId: resolvedAgentId,
1122
+ ...(provider ? { provider } : {}),
1123
+ model,
1124
+ reasoningEffort: finalEffort,
1125
+ })
1126
+ applied = true
1127
+ } else if (targetAgent && typeof targetAgent.setModel === 'function') {
1128
+ targetAgent.setModel({
1129
+ ...(provider ? { provider } : {}),
1130
+ ...(model ? { model } : {}),
1131
+ reasoningEffort: finalEffort,
1132
+ })
1133
+ applied = true
1134
+ }
1135
+ } catch (err) {
1136
+ return { ok: false, error: '调节思考强度失败:' + ((err && err.message) || String(err || 'unknown')) }
1137
+ }
1138
+
1139
+ if (!applied) {
1140
+ return { ok: false, error: `未找到在线代理 ${targetKey} 或当前宿主未提供热调挡接口` }
1141
+ }
1142
+
1143
+ appendChiefAuditLog(rootKey, {
1144
+ mode: effCfg.permissionMode,
1145
+ stage: 'model_effort',
1146
+ decision: byUser ? 'user_changed' : 'executed',
1147
+ operation: '调节模型思考强度',
1148
+ target: targetKey,
1149
+ reason: reason || `${byUser ? '人类用户' : '参谋长'}将 ${targetKey} 思考强度设为 ${finalEffort}`,
1150
+ outcome: 'ok',
1151
+ by: byUser ? 'user' : 'chief',
1152
+ })
1153
+ return {
1154
+ ok: true,
1155
+ target: targetKey,
1156
+ targetId: resolvedAgentId,
1157
+ reasoningEffort: finalEffort,
1158
+ badge: `🧠 ${isLead ? '主对话' : '@' + targetKey} 思考强度 → ${finalEffort}`,
1159
+ }
1160
+ }
916
1161
  if (op === 'switch_model') {
917
1162
  const rawRef = String(modelRef || '').trim()
918
1163
  if (!rawRef) return { ok: false, error: 'switch_model 需要 modelRef 参数(如 provider/model)' }
@@ -1054,7 +1299,16 @@ export function createSubagentRuntime(deps) {
1054
1299
  }
1055
1300
 
1056
1301
  const requestedTarget = str(args.agent || args.subagent || args.target || targetTask.subagent || targetTask.owner, '').trim()
1057
- const brief = buildTaskDispatchBrief(boardSnapshot, targetTask, args.prompt || args.message || args.text)
1302
+ const explicitPlanStepId = typeof args.planStepId === 'string' ? args.planStepId.trim().slice(0, 120) : ''
1303
+ const explicitTargetStepId = typeof args.targetPlanStepId === 'string' ? args.targetPlanStepId.trim().slice(0, 120) : ''
1304
+ const effectivePlanStepId = explicitPlanStepId || targetTask.planStepId || resolveActivePlanStepId(boardSnapshot)
1305
+ const effectiveTargetStepId = explicitTargetStepId || targetTask.targetPlanStepId || ''
1306
+ const briefTask = {
1307
+ ...targetTask,
1308
+ planStepId: effectivePlanStepId,
1309
+ targetPlanStepId: effectiveTargetStepId,
1310
+ }
1311
+ const brief = buildTaskDispatchBrief(boardSnapshot, briefTask, args.prompt || args.message || args.text)
1058
1312
  let dispatchedName = requestedTarget
1059
1313
  let childSessionId = ''
1060
1314
  let dispatchMode = 'assigned'
@@ -1079,6 +1333,18 @@ export function createSubagentRuntime(deps) {
1079
1333
  const providerName = str(args.provider, targetTask.preset === 'fork' ? 'fork' : 'spawn') || 'spawn'
1080
1334
  const preset = str(args.preset, targetTask.preset && targetTask.preset !== 'fork' && targetTask.preset !== 'spawn' ? targetTask.preset : '')
1081
1335
  const candidateName = sanitizeSubagentName(requestedTarget, targetTask.id)
1336
+ const reqEffortRaw = str(args.reasoningEffort || args.effort, '').trim().toLowerCase()
1337
+ let allowedEffort = ['off', 'low', 'medium', 'high', 'xhigh'].includes(reqEffortRaw) ? reqEffortRaw : ''
1338
+ if (allowedEffort) {
1339
+ try {
1340
+ const effCfg = resolveWorkspaceConfig(loadChiefState(), rootId)
1341
+ const locks = (effCfg && effCfg.lockedModelEfforts) || {}
1342
+ const isHuman = by === 'operator' || by === 'user'
1343
+ if (!isHuman && (locks[candidateName] || locks[requestedTarget])) {
1344
+ allowedEffort = ''
1345
+ }
1346
+ } catch {}
1347
+ }
1082
1348
 
1083
1349
  let existingTargetId = ''
1084
1350
  if (requestedTarget && requestedTarget !== 'agent' && requestedTarget !== 'operator' && requestedTarget !== 'lead') {
@@ -1092,6 +1358,15 @@ export function createSubagentRuntime(deps) {
1092
1358
  }
1093
1359
 
1094
1360
  if (existingTargetId && existingTargetId !== String(liveCaller.id)) {
1361
+ if (allowedEffort && agents && typeof agents.get === 'function') {
1362
+ try {
1363
+ const exAg = agents.get(existingTargetId)
1364
+ if (exAg && exAg.model && typeof exAg.model.select === 'function') {
1365
+ const cur = typeof exAg.model.current === 'function' ? exAg.model.current() : {}
1366
+ await exAg.model.select({ ...(cur || {}), reasoningEffort: allowedEffort })
1367
+ }
1368
+ } catch {}
1369
+ }
1095
1370
  await subagents.sendMessage(liveCaller, existingTargetId, [{ type: 'text', text: brief }], { signal: sig })
1096
1371
  dispatchedName = requestedTarget || candidateName
1097
1372
  childSessionId = String(existingTargetId)
@@ -1107,6 +1382,7 @@ export function createSubagentRuntime(deps) {
1107
1382
  prompt: [{ type: 'text', text: brief }],
1108
1383
  parent: liveCaller,
1109
1384
  ...(preset ? { agentPreset: preset } : {}),
1385
+ ...(allowedEffort ? { agentReasoningEffort: allowedEffort, reasoningEffort: allowedEffort } : {}),
1110
1386
  signal: sig,
1111
1387
  delivery: 'parent',
1112
1388
  })
@@ -1122,6 +1398,7 @@ export function createSubagentRuntime(deps) {
1122
1398
  prompt: [{ type: 'text', text: brief }],
1123
1399
  parent: liveCaller,
1124
1400
  ...(preset ? { agentPreset: preset } : {}),
1401
+ ...(allowedEffort ? { agentReasoningEffort: allowedEffort, reasoningEffort: allowedEffort } : {}),
1125
1402
  signal: sig,
1126
1403
  delivery: 'parent',
1127
1404
  })
@@ -1148,6 +1425,12 @@ export function createSubagentRuntime(deps) {
1148
1425
  task.subagent = dispatchedName
1149
1426
  if (childSessionId) task.session = childSessionId
1150
1427
  if (args.preset) task.preset = str(args.preset, '').slice(0, 80)
1428
+ if (explicitPlanStepId) task.planStepId = explicitPlanStepId
1429
+ else if (!task.planStepId) {
1430
+ const autoStep = resolveActivePlanStepId(board)
1431
+ if (autoStep) task.planStepId = autoStep
1432
+ }
1433
+ if (explicitTargetStepId) task.targetPlanStepId = explicitTargetStepId
1151
1434
  task.dispatchedAt = nowMs()
1152
1435
  task.updatedAt = nowMs()
1153
1436
  board.cursor = {