dsh-chief 1.5.0 → 1.7.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.
package/api-routes.js CHANGED
@@ -33,18 +33,26 @@ import {
33
33
  mutateChiefFact,
34
34
  handleApprovalDecision,
35
35
  handleElevationDecision,
36
+ scheduleChiefTurnReview,
36
37
  } from './chief.js'
37
38
  import {
38
39
  getChiefActiveWork,
39
40
  mutateChiefState,
40
41
  ensureWorkspace,
42
+ activateChiefSession,
41
43
  queueChiefApproval,
42
44
  normalizeSuggestion,
43
45
  hasValidChiefRiskAck,
46
+ summarizeLlmLedger,
47
+ setBubbleHarnessMembership,
48
+ setBubbleManualLock,
49
+ appendChiefAuditLog,
50
+ clamp,
44
51
  SUGGESTIONS_CAP,
45
52
  CHAT_CAP,
46
53
  nextId,
47
54
  } from './chief-state.js'
55
+ import { listPeerDirectory, computeBubbleState, dispatchBubbleHarness, refreshBubbleDiscovery } from './chief-network.js'
48
56
  import {
49
57
  resolveProjectKey,
50
58
  getArchiveMeta,
@@ -155,6 +163,11 @@ export function createApiRoutes(deps) {
155
163
  return (!root || root === 'global') ? '' : root
156
164
  }
157
165
 
166
+ function markSessionActivated(sessionId = '') {
167
+ const root = resolveBoundRootSid(sessionId)
168
+ if (root) activateChiefSession(root, { force: true })
169
+ }
170
+
158
171
  function collectDeskItemsForUi(runtime, rootId, cabinetId = '') {
159
172
  if (!rootId) return []
160
173
  const targetOpt = archiveTarget(runtime, rootId, { cabinetId, vaultScope: 'session' })
@@ -279,9 +292,20 @@ export function createApiRoutes(deps) {
279
292
  chiefMeta: {
280
293
  backendBuild: BACKEND_BUILD,
281
294
  hostCompat,
295
+ sessionEnabled: Boolean(effCfg.sessionEnabled),
296
+ defaultAutoEnableSessions: Boolean(effCfg.defaultAutoEnableSessions),
297
+ globalSafetyGuardEnabled: effCfg.globalSafetyGuardEnabled !== false,
298
+ sessionSafetyGuardEnabled: effCfg.sessionSafetyGuardEnabled !== false,
282
299
  paceMode: effCfg.paceMode || 'manual',
283
300
  runMode: effCfg.runMode || 'off',
284
301
  managedIdleTimeoutMs: effCfg.managedIdleTimeoutMs || 15 * 60 * 1000,
302
+ notifyEnabled: effCfg.notifyEnabled !== false,
303
+ notifySound: Boolean(effCfg.notifySound),
304
+ uiLang: effCfg.uiLang || 'auto',
305
+ managedBudgetMaxCalls: effCfg.managedBudgetMaxCalls ?? 30,
306
+ managedBudgetMaxTokens: effCfg.managedBudgetMaxTokens ?? 300000,
307
+ llmLedgerSummary: summarizeLlmLedger(rootWs && rootWs.llmLedger, effCfg),
308
+ alertEvents: ((rootWs && Array.isArray(rootWs.alertEvents)) ? rootWs.alertEvents : []).slice(-10),
285
309
  thinkingEffort: effCfg.thinkingEffort || 'auto',
286
310
  teamEffortMode: effCfg.teamEffortMode || 'auto',
287
311
  lockedModelEfforts: effCfg.lockedModelEfforts || {},
@@ -473,6 +497,7 @@ export function createApiRoutes(deps) {
473
497
  const body = await readBody(req)
474
498
  const sessionId = body && typeof body.session === 'string' ? body.session : ''
475
499
  const actName = body && typeof body.action === 'string' ? body.action : ''
500
+ if (actName && actName !== 'snapshot') markSessionActivated(sessionId)
476
501
  if (actName === 'bounds.propose') {
477
502
  const propRes = proposeBoundToChief(makeChiefDeps(runtime), {
478
503
  sessionId,
@@ -539,6 +564,7 @@ export function createApiRoutes(deps) {
539
564
  if (body.regenerate === true) regenerate = true
540
565
  if (typeof body.syncHandoff === 'boolean') syncHandoff = body.syncHandoff
541
566
  }
567
+ if (regenerate) markSessionActivated(sessionId)
542
568
  const view = await getChiefView(makeChiefDeps(runtime), sessionId, { regenerate, autoSummarize: !regenerate, syncHandoff })
543
569
  if (view && view.handoffSyncResult && view.handoffSyncResult.synced && sessionId) {
544
570
  sendJson(res, 200, { ...view, board: boardForSession(boardFileFor(sessionId)) })
@@ -557,6 +583,7 @@ export function createApiRoutes(deps) {
557
583
  try {
558
584
  const body = await readBody(req)
559
585
  const sessionId = typeof body.session === 'string' ? body.session : ''
586
+ markSessionActivated(sessionId)
560
587
  const out = await askOrCommandChief(makeChiefDeps(runtime), {
561
588
  sessionId,
562
589
  text: typeof body.text === 'string' ? body.text : '',
@@ -580,6 +607,7 @@ export function createApiRoutes(deps) {
580
607
  const body = await readBody(req)
581
608
  const sessionId = typeof body?.session === 'string' ? body.session : ''
582
609
  const rootSid = resolveRootSessionId(sessionId) || sessionId || 'global'
610
+ markSessionActivated(rootSid)
583
611
  const out = await relayArchivesWithChief(makeChiefDeps(runtime), {
584
612
  sessionId: rootSid,
585
613
  items: Array.isArray(body?.items) ? body.items : [],
@@ -601,6 +629,7 @@ export function createApiRoutes(deps) {
601
629
  try {
602
630
  const body = await readBody(req)
603
631
  const sessionId = typeof body.session === 'string' ? body.session : ''
632
+ markSessionActivated(sessionId)
604
633
  const out = await deliverCrossMessage(makeChiefDeps(runtime), {
605
634
  fromSession: sessionId,
606
635
  fromLabel: typeof body.by === 'string' && body.by ? body.by : 'DSH 参谋长',
@@ -798,6 +827,36 @@ export function createApiRoutes(deps) {
798
827
  }
799
828
  }
800
829
  }
830
+ const cfgActive = updated && updated.config ? updated.config : null
831
+ const switchedToActiveRunOrIntv = Boolean(
832
+ wantsInterventionOn
833
+ || body?.runMode === 'managed'
834
+ || body?.runMode === 'patrol'
835
+ || body?.paceMode === 'auto'
836
+ || body?.paceMode === 'smart'
837
+ || body?.autoPilot === true,
838
+ )
839
+ if (
840
+ updated
841
+ && updated.ok !== false
842
+ && targetSessionForBoard
843
+ && !(immediateKickoff && immediateKickoff.triggered)
844
+ && cfgActive
845
+ && !cfgActive.stopped
846
+ && cfgActive.tacticalModes
847
+ && cfgActive.tacticalModes.intervention === true
848
+ && switchedToActiveRunOrIntv
849
+ ) {
850
+ const agentsSvcNow = runtime && typeof runtime.get === 'function' ? runtime.get('agents') : null
851
+ const leadAgNow = agentsSvcNow && typeof agentsSvcNow.get === 'function' ? agentsSvcNow.get(targetSessionForBoard) : null
852
+ if (leadAgNow && leadAgNow.status !== 'running') {
853
+ scheduleChiefTurnReview(makeChiefDeps(runtime), {
854
+ agent: leadAgNow,
855
+ sessionId: targetSessionForBoard,
856
+ abnormalTrigger: 'config_activated',
857
+ })
858
+ }
859
+ }
801
860
  const view = await getChiefView(makeChiefDeps(runtime), sessionId, { autoSummarize: false })
802
861
  const latestBoard = targetSessionForBoard ? boardForSession(boardFileFor(targetSessionForBoard)) : null
803
862
  sendJson(res, updated && updated.ok === false ? 400 : 200, {
@@ -819,6 +878,7 @@ export function createApiRoutes(deps) {
819
878
  try {
820
879
  const body = await readBody(req)
821
880
  const sessionId = typeof body?.session === 'string' ? body.session : ''
881
+ markSessionActivated(sessionId)
822
882
  const out = await handleApprovalDecision(makeChiefDeps(runtime), {
823
883
  sessionId,
824
884
  approvalId: typeof body?.id === 'string' ? body.id : '',
@@ -838,6 +898,7 @@ export function createApiRoutes(deps) {
838
898
  try {
839
899
  const body = await readBody(req)
840
900
  const sessionId = typeof body?.session === 'string' ? body.session : ''
901
+ markSessionActivated(sessionId)
841
902
  const out = await handleElevationDecision(makeChiefDeps(runtime), {
842
903
  sessionId,
843
904
  elevationId: typeof body?.id === 'string' ? body.id : '',
@@ -859,6 +920,7 @@ export function createApiRoutes(deps) {
859
920
  try {
860
921
  const body = await readBody(req).catch(() => ({}))
861
922
  const sessionId = typeof body?.session === 'string' ? body.session : ''
923
+ markSessionActivated(sessionId)
862
924
  const out = await suggestCoreObjectiveWithChief(makeChiefDeps(runtime), {
863
925
  sessionId,
864
926
  draft: typeof body?.draft === 'string' ? body.draft : '',
@@ -878,6 +940,7 @@ export function createApiRoutes(deps) {
878
940
  try {
879
941
  const body = await readBody(req).catch(() => ({}))
880
942
  const sessionId = typeof body?.session === 'string' ? body.session : ''
943
+ markSessionActivated(sessionId)
881
944
  const out = await recommendAndSetCompactThreshold(makeChiefDeps(runtime), { sessionId })
882
945
  sendJson(res, out && out.ok === false ? 400 : 200, out)
883
946
  } catch (error) {
@@ -892,8 +955,167 @@ export function createApiRoutes(deps) {
892
955
  try {
893
956
  const body = await readBody(req)
894
957
  const sessionId = typeof body.session === 'string' ? body.session : ''
895
- const peerSession = typeof body.peerSession === 'string' ? body.peerSession : ''
896
958
  const rootA = resolveRootSessionId(sessionId) || sessionId
959
+ const op = typeof body.op === 'string' ? body.op.trim() : ''
960
+ const chiefDeps = makeChiefDeps(runtime)
961
+
962
+ if (op === 'bubble_refresh') {
963
+ refreshBubbleDiscovery(runtime, deps.hostState)
964
+ const view = await getChiefView(chiefDeps, rootA, { autoSummarize: false })
965
+ const bNow = (view && view.bubble) || {}
966
+ const totalHarnesses = ((bNow.enrolledHarnesses || []).filter(h => h && h.installed !== false).length)
967
+ + ((bNow.availableHarnesses || []).length)
968
+ const otherList = Array.isArray(view && view.otherSessions) ? view.otherSessions : []
969
+ sendJson(res, 200, {
970
+ ok: true,
971
+ refreshed: {
972
+ harnesses: totalHarnesses,
973
+ otherSessions: otherList.length,
974
+ dormantSessions: otherList.filter(s => s && s.dormant).length,
975
+ },
976
+ view,
977
+ })
978
+ return
979
+ }
980
+
981
+ markSessionActivated(rootA)
982
+ const dirNow = await listPeerDirectory(chiefDeps, rootA).catch(() => ({ targets: [], cluster: [] }))
983
+ const bState = computeBubbleState(chiefDeps, rootA, loadChiefState(), dirNow)
984
+ const LOCK_ERR = '🔒 协同水泡工作中已锁死结构,不允许中途拆解或改动编队;请等待全员收工或点击「⏹ 急停解体」'
985
+
986
+ if (op === 'bubble_emergency_stop') {
987
+ const subagents = runtime && typeof runtime.get === 'function' ? runtime.get('subagents') : null
988
+ let stoppedCount = 0
989
+ if (deps.hostState.activeBubbleRuns instanceof Map) {
990
+ for (const [rKey, run] of [...deps.hostState.activeBubbleRuns.entries()]) {
991
+ if (!run || (run.rootId && run.rootId !== rootA)) continue
992
+ try { run.abortController?.abort() } catch {}
993
+ try { if (typeof run.terminate === 'function') run.terminate() } catch {}
994
+ try {
995
+ if (run.childId && subagents && typeof subagents.interrupt === 'function') {
996
+ subagents.interrupt(run.childId, { kind: 'user' })
997
+ }
998
+ } catch {}
999
+ deps.hostState.activeBubbleRuns.delete(rKey)
1000
+ stoppedCount += 1
1001
+ }
1002
+ }
1003
+ if (subagents && typeof subagents.interrupt === 'function') {
1004
+ for (const peer of (dirNow.targets || [])) {
1005
+ if (peer && peer.status === 'running' && peer.id && (peer.kind === 'subagent' || peer.kind === 'peer-session')) {
1006
+ try {
1007
+ subagents.interrupt(peer.id, { kind: 'user' })
1008
+ stoppedCount += 1
1009
+ } catch {}
1010
+ }
1011
+ }
1012
+ }
1013
+ try {
1014
+ deps.hostState.activeAutoContinue?.cancelSession?.(rootA, 'bubble emergency stop')
1015
+ } catch {}
1016
+ try {
1017
+ updateChiefConfig(chiefDeps, {
1018
+ session: rootA,
1019
+ tacticalModes: { intervention: false },
1020
+ byUser: true,
1021
+ })
1022
+ } catch {}
1023
+ setBubbleManualLock(rootA, { locked: false, reason: 'emergency_stop' })
1024
+ appendChiefAuditLog(rootA, {
1025
+ kind: 'bubble_emergency_stop',
1026
+ decision: 'allowed',
1027
+ tool: 'bubble_emergency_stop',
1028
+ reason: `触发水泡急停解体,终止 ${stoppedCount} 个在途成员并释放结构锁`,
1029
+ })
1030
+ mutateChiefState((st) => {
1031
+ const ws = ensureWorkspace(st, rootA)
1032
+ ws.chat.push({
1033
+ id: nextId('chat'),
1034
+ role: 'chief',
1035
+ kind: 'receipt',
1036
+ text: `- 【⏹ 水泡急停解体】已中断 ${stoppedCount} 个在途执行并解除协同编队锁死。`,
1037
+ actions: ['🔓 结构已解锁'],
1038
+ at: Date.now(),
1039
+ })
1040
+ if (ws.chat.length > CHAT_CAP) ws.chat = ws.chat.slice(-CHAT_CAP)
1041
+ })
1042
+ const view = await getChiefView(chiefDeps, rootA, { autoSummarize: false })
1043
+ sendJson(res, 200, { ok: true, stoppedCount, view })
1044
+ return
1045
+ }
1046
+
1047
+ if (op === 'bubble_lock') {
1048
+ const wantLock = Boolean(body.locked)
1049
+ if (!wantLock && bState.autoLocked) {
1050
+ sendJson(res, 409, {
1051
+ ok: false,
1052
+ locked: true,
1053
+ error: '🔒 仍有成员或外部 Harness 运行中,无法直接解锁;请等待完结或点击「⏹ 急停解体」',
1054
+ bubble: bState,
1055
+ })
1056
+ return
1057
+ }
1058
+ const lockRes = setBubbleManualLock(rootA, {
1059
+ locked: wantLock,
1060
+ reason: typeof body.reason === 'string' ? body.reason : '',
1061
+ })
1062
+ if (!lockRes.ok) {
1063
+ sendJson(res, 400, lockRes)
1064
+ return
1065
+ }
1066
+ const view = await getChiefView(chiefDeps, rootA, { autoSummarize: false })
1067
+ sendJson(res, 200, { ...lockRes, view })
1068
+ return
1069
+ }
1070
+
1071
+ if (op === 'harness_toggle') {
1072
+ if (bState.locked && body.force !== true) {
1073
+ sendJson(res, 409, { ok: false, locked: true, error: LOCK_ERR, bubble: bState })
1074
+ return
1075
+ }
1076
+ const hRes = setBubbleHarnessMembership(rootA, {
1077
+ provider: body.provider,
1078
+ label: body.label,
1079
+ role: body.role,
1080
+ joined: Boolean(body.joined),
1081
+ })
1082
+ if (!hRes.ok) {
1083
+ sendJson(res, 400, hRes)
1084
+ return
1085
+ }
1086
+ const view = await getChiefView(chiefDeps, rootA, { autoSummarize: false })
1087
+ sendJson(res, 200, { ...hRes, view })
1088
+ return
1089
+ }
1090
+
1091
+ if (op === 'harness_dispatch') {
1092
+ const provider = clamp(String(body.provider || '').trim(), 64)
1093
+ const taskText = typeof body.text === 'string' ? body.text.trim() : ''
1094
+ const taskLabel = clamp(String(body.label || taskText || provider).trim(), 48)
1095
+ const disp = await dispatchBubbleHarness(chiefDeps, {
1096
+ rootId: rootA,
1097
+ provider,
1098
+ text: taskText,
1099
+ label: taskLabel,
1100
+ fromLabel: 'DSH 参谋长',
1101
+ force: body.force === true,
1102
+ allowAutoEnroll: true,
1103
+ })
1104
+ if (!disp.ok) {
1105
+ const status = disp.statusCode || (disp.locked ? 409 : 400)
1106
+ sendJson(res, status, disp)
1107
+ return
1108
+ }
1109
+ const view = await getChiefView(chiefDeps, rootA, { autoSummarize: false })
1110
+ sendJson(res, 200, { ok: true, runId: disp.runId, childId: disp.childId, transport: disp.transport, view })
1111
+ return
1112
+ }
1113
+
1114
+ if (bState.locked && body.force !== true) {
1115
+ sendJson(res, 409, { ok: false, locked: true, error: LOCK_ERR, bubble: bState })
1116
+ return
1117
+ }
1118
+ const peerSession = typeof body.peerSession === 'string' ? body.peerSession : ''
897
1119
  const rootB = resolveRootSessionId(peerSession) || peerSession
898
1120
  const linked = Boolean(body.linked)
899
1121
  const out = setPeerLink(rootA, rootB, linked)
@@ -901,7 +1123,7 @@ export function createApiRoutes(deps) {
901
1123
  sendJson(res, 400, out)
902
1124
  return
903
1125
  }
904
- const view = await getChiefView(makeChiefDeps(runtime), rootA, { autoSummarize: false })
1126
+ const view = await getChiefView(chiefDeps, rootA, { autoSummarize: false })
905
1127
  sendJson(res, 200, { ...out, view })
906
1128
  } catch (error) {
907
1129
  sendJson(res, 400, { ok: false, error: String(error && error.message ? error.message : error) })
@@ -915,6 +1137,7 @@ export function createApiRoutes(deps) {
915
1137
  try {
916
1138
  const body = await readBody(req)
917
1139
  const sessionId = typeof body.session === 'string' ? body.session : ''
1140
+ markSessionActivated(sessionId)
918
1141
  const isBatch = body.decision === 'execute_batch' || body.batch === true
919
1142
  const out = isBatch
920
1143
  ? await executeBatchSuggestions(makeChiefDeps(runtime), {
@@ -947,6 +1170,7 @@ export function createApiRoutes(deps) {
947
1170
  try {
948
1171
  const body = await readBody(req)
949
1172
  const sessionId = typeof body.session === 'string' ? body.session : ''
1173
+ markSessionActivated(sessionId)
950
1174
  const out = await consolidateBoundsWithChief(makeChiefDeps(runtime), {
951
1175
  sessionId,
952
1176
  field: typeof body.field === 'string' ? body.field : '',
@@ -967,6 +1191,7 @@ export function createApiRoutes(deps) {
967
1191
  try {
968
1192
  const body = await readBody(req)
969
1193
  const sessionId = typeof body.session === 'string' ? body.session : ''
1194
+ markSessionActivated(sessionId)
970
1195
  const out = mutateChiefFact(makeChiefDeps(runtime), {
971
1196
  sessionId,
972
1197
  op: typeof body.op === 'string' ? body.op : 'add',
@@ -1027,6 +1252,7 @@ export function createApiRoutes(deps) {
1027
1252
  const body = await readBody(req)
1028
1253
  const sessionId = typeof body.session === 'string' ? body.session : ''
1029
1254
  const rootSid = resolveBoundRootSid(sessionId)
1255
+ markSessionActivated(rootSid || sessionId)
1030
1256
  const op = typeof body.op === 'string' ? body.op : 'save'
1031
1257
  const reqScope = resolveArchiveHttpScope(body.vaultScope || body.scope || '')
1032
1258
  const workingScope = reqScope === 'global' ? 'global' : 'session'
@@ -1309,6 +1535,7 @@ export function createApiRoutes(deps) {
1309
1535
  const body = await readBody(req)
1310
1536
  const sessionId = typeof body.session === 'string' ? body.session : ''
1311
1537
  const rootSid = resolveBoundRootSid(sessionId)
1538
+ markSessionActivated(rootSid || sessionId)
1312
1539
  const vaultScope = resolveArchiveHttpScope(body.vaultScope || body.scope) === 'global' ? 'global' : 'session'
1313
1540
  const cabinetId = vaultScope === 'global' ? '' : (typeof body.cabinetId === 'string' ? body.cabinetId : '')
1314
1541
  const targetOpt = archiveTarget(runtime, rootSid, { cabinetId, vaultScope })
@@ -1347,6 +1574,7 @@ export function createApiRoutes(deps) {
1347
1574
  sendJson(res, 400, { ok: false, error: '当前未绑定具体会话,无法从本会话档案柜提炼' })
1348
1575
  return
1349
1576
  }
1577
+ markSessionActivated(rootSid)
1350
1578
  const cabinetId = typeof body.cabinetId === 'string' ? body.cabinetId : ''
1351
1579
  const targetOpt = archiveTarget(runtime, rootSid, { cabinetId, vaultScope: 'session' })
1352
1580
  const out = await distillSessionArchivesToGlobal(makeChiefDeps(runtime), {
@@ -1379,6 +1607,7 @@ export function createApiRoutes(deps) {
1379
1607
  const body = await readBody(req)
1380
1608
  const sessionId = typeof body.session === 'string' ? body.session : ''
1381
1609
  const rootSid = resolveBoundRootSid(sessionId)
1610
+ markSessionActivated(rootSid || sessionId)
1382
1611
  const cabinetId = typeof body.cabinetId === 'string' ? body.cabinetId : ''
1383
1612
  const vaultScope = 'session'
1384
1613
  const targetOpt = archiveTarget(runtime, rootSid, { cabinetId, vaultScope })
@@ -1410,6 +1639,7 @@ export function createApiRoutes(deps) {
1410
1639
  const body = await readBody(req)
1411
1640
  const sessionId = typeof body.session === 'string' ? body.session : ''
1412
1641
  const rootSid = resolveRootSessionId(sessionId) || sessionId
1642
+ markSessionActivated(rootSid)
1413
1643
  const cabinetId = typeof body.cabinetId === 'string' ? body.cabinetId : ''
1414
1644
  const vaultScope = resolveArchiveHttpScope(body.vaultScope || body.scope) === 'global' ? 'global' : 'session'
1415
1645
  if (vaultScope !== 'global') {