dsh-chief 1.6.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
@@ -39,14 +39,20 @@ import {
39
39
  getChiefActiveWork,
40
40
  mutateChiefState,
41
41
  ensureWorkspace,
42
+ activateChiefSession,
42
43
  queueChiefApproval,
43
44
  normalizeSuggestion,
44
45
  hasValidChiefRiskAck,
45
46
  summarizeLlmLedger,
47
+ setBubbleHarnessMembership,
48
+ setBubbleManualLock,
49
+ appendChiefAuditLog,
50
+ clamp,
46
51
  SUGGESTIONS_CAP,
47
52
  CHAT_CAP,
48
53
  nextId,
49
54
  } from './chief-state.js'
55
+ import { listPeerDirectory, computeBubbleState, dispatchBubbleHarness, refreshBubbleDiscovery } from './chief-network.js'
50
56
  import {
51
57
  resolveProjectKey,
52
58
  getArchiveMeta,
@@ -157,6 +163,11 @@ export function createApiRoutes(deps) {
157
163
  return (!root || root === 'global') ? '' : root
158
164
  }
159
165
 
166
+ function markSessionActivated(sessionId = '') {
167
+ const root = resolveBoundRootSid(sessionId)
168
+ if (root) activateChiefSession(root, { force: true })
169
+ }
170
+
160
171
  function collectDeskItemsForUi(runtime, rootId, cabinetId = '') {
161
172
  if (!rootId) return []
162
173
  const targetOpt = archiveTarget(runtime, rootId, { cabinetId, vaultScope: 'session' })
@@ -281,6 +292,10 @@ export function createApiRoutes(deps) {
281
292
  chiefMeta: {
282
293
  backendBuild: BACKEND_BUILD,
283
294
  hostCompat,
295
+ sessionEnabled: Boolean(effCfg.sessionEnabled),
296
+ defaultAutoEnableSessions: Boolean(effCfg.defaultAutoEnableSessions),
297
+ globalSafetyGuardEnabled: effCfg.globalSafetyGuardEnabled !== false,
298
+ sessionSafetyGuardEnabled: effCfg.sessionSafetyGuardEnabled !== false,
284
299
  paceMode: effCfg.paceMode || 'manual',
285
300
  runMode: effCfg.runMode || 'off',
286
301
  managedIdleTimeoutMs: effCfg.managedIdleTimeoutMs || 15 * 60 * 1000,
@@ -482,6 +497,7 @@ export function createApiRoutes(deps) {
482
497
  const body = await readBody(req)
483
498
  const sessionId = body && typeof body.session === 'string' ? body.session : ''
484
499
  const actName = body && typeof body.action === 'string' ? body.action : ''
500
+ if (actName && actName !== 'snapshot') markSessionActivated(sessionId)
485
501
  if (actName === 'bounds.propose') {
486
502
  const propRes = proposeBoundToChief(makeChiefDeps(runtime), {
487
503
  sessionId,
@@ -548,6 +564,7 @@ export function createApiRoutes(deps) {
548
564
  if (body.regenerate === true) regenerate = true
549
565
  if (typeof body.syncHandoff === 'boolean') syncHandoff = body.syncHandoff
550
566
  }
567
+ if (regenerate) markSessionActivated(sessionId)
551
568
  const view = await getChiefView(makeChiefDeps(runtime), sessionId, { regenerate, autoSummarize: !regenerate, syncHandoff })
552
569
  if (view && view.handoffSyncResult && view.handoffSyncResult.synced && sessionId) {
553
570
  sendJson(res, 200, { ...view, board: boardForSession(boardFileFor(sessionId)) })
@@ -566,6 +583,7 @@ export function createApiRoutes(deps) {
566
583
  try {
567
584
  const body = await readBody(req)
568
585
  const sessionId = typeof body.session === 'string' ? body.session : ''
586
+ markSessionActivated(sessionId)
569
587
  const out = await askOrCommandChief(makeChiefDeps(runtime), {
570
588
  sessionId,
571
589
  text: typeof body.text === 'string' ? body.text : '',
@@ -589,6 +607,7 @@ export function createApiRoutes(deps) {
589
607
  const body = await readBody(req)
590
608
  const sessionId = typeof body?.session === 'string' ? body.session : ''
591
609
  const rootSid = resolveRootSessionId(sessionId) || sessionId || 'global'
610
+ markSessionActivated(rootSid)
592
611
  const out = await relayArchivesWithChief(makeChiefDeps(runtime), {
593
612
  sessionId: rootSid,
594
613
  items: Array.isArray(body?.items) ? body.items : [],
@@ -610,6 +629,7 @@ export function createApiRoutes(deps) {
610
629
  try {
611
630
  const body = await readBody(req)
612
631
  const sessionId = typeof body.session === 'string' ? body.session : ''
632
+ markSessionActivated(sessionId)
613
633
  const out = await deliverCrossMessage(makeChiefDeps(runtime), {
614
634
  fromSession: sessionId,
615
635
  fromLabel: typeof body.by === 'string' && body.by ? body.by : 'DSH 参谋长',
@@ -858,6 +878,7 @@ export function createApiRoutes(deps) {
858
878
  try {
859
879
  const body = await readBody(req)
860
880
  const sessionId = typeof body?.session === 'string' ? body.session : ''
881
+ markSessionActivated(sessionId)
861
882
  const out = await handleApprovalDecision(makeChiefDeps(runtime), {
862
883
  sessionId,
863
884
  approvalId: typeof body?.id === 'string' ? body.id : '',
@@ -877,6 +898,7 @@ export function createApiRoutes(deps) {
877
898
  try {
878
899
  const body = await readBody(req)
879
900
  const sessionId = typeof body?.session === 'string' ? body.session : ''
901
+ markSessionActivated(sessionId)
880
902
  const out = await handleElevationDecision(makeChiefDeps(runtime), {
881
903
  sessionId,
882
904
  elevationId: typeof body?.id === 'string' ? body.id : '',
@@ -898,6 +920,7 @@ export function createApiRoutes(deps) {
898
920
  try {
899
921
  const body = await readBody(req).catch(() => ({}))
900
922
  const sessionId = typeof body?.session === 'string' ? body.session : ''
923
+ markSessionActivated(sessionId)
901
924
  const out = await suggestCoreObjectiveWithChief(makeChiefDeps(runtime), {
902
925
  sessionId,
903
926
  draft: typeof body?.draft === 'string' ? body.draft : '',
@@ -917,6 +940,7 @@ export function createApiRoutes(deps) {
917
940
  try {
918
941
  const body = await readBody(req).catch(() => ({}))
919
942
  const sessionId = typeof body?.session === 'string' ? body.session : ''
943
+ markSessionActivated(sessionId)
920
944
  const out = await recommendAndSetCompactThreshold(makeChiefDeps(runtime), { sessionId })
921
945
  sendJson(res, out && out.ok === false ? 400 : 200, out)
922
946
  } catch (error) {
@@ -931,8 +955,167 @@ export function createApiRoutes(deps) {
931
955
  try {
932
956
  const body = await readBody(req)
933
957
  const sessionId = typeof body.session === 'string' ? body.session : ''
934
- const peerSession = typeof body.peerSession === 'string' ? body.peerSession : ''
935
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 : ''
936
1119
  const rootB = resolveRootSessionId(peerSession) || peerSession
937
1120
  const linked = Boolean(body.linked)
938
1121
  const out = setPeerLink(rootA, rootB, linked)
@@ -940,7 +1123,7 @@ export function createApiRoutes(deps) {
940
1123
  sendJson(res, 400, out)
941
1124
  return
942
1125
  }
943
- const view = await getChiefView(makeChiefDeps(runtime), rootA, { autoSummarize: false })
1126
+ const view = await getChiefView(chiefDeps, rootA, { autoSummarize: false })
944
1127
  sendJson(res, 200, { ...out, view })
945
1128
  } catch (error) {
946
1129
  sendJson(res, 400, { ok: false, error: String(error && error.message ? error.message : error) })
@@ -954,6 +1137,7 @@ export function createApiRoutes(deps) {
954
1137
  try {
955
1138
  const body = await readBody(req)
956
1139
  const sessionId = typeof body.session === 'string' ? body.session : ''
1140
+ markSessionActivated(sessionId)
957
1141
  const isBatch = body.decision === 'execute_batch' || body.batch === true
958
1142
  const out = isBatch
959
1143
  ? await executeBatchSuggestions(makeChiefDeps(runtime), {
@@ -986,6 +1170,7 @@ export function createApiRoutes(deps) {
986
1170
  try {
987
1171
  const body = await readBody(req)
988
1172
  const sessionId = typeof body.session === 'string' ? body.session : ''
1173
+ markSessionActivated(sessionId)
989
1174
  const out = await consolidateBoundsWithChief(makeChiefDeps(runtime), {
990
1175
  sessionId,
991
1176
  field: typeof body.field === 'string' ? body.field : '',
@@ -1006,6 +1191,7 @@ export function createApiRoutes(deps) {
1006
1191
  try {
1007
1192
  const body = await readBody(req)
1008
1193
  const sessionId = typeof body.session === 'string' ? body.session : ''
1194
+ markSessionActivated(sessionId)
1009
1195
  const out = mutateChiefFact(makeChiefDeps(runtime), {
1010
1196
  sessionId,
1011
1197
  op: typeof body.op === 'string' ? body.op : 'add',
@@ -1066,6 +1252,7 @@ export function createApiRoutes(deps) {
1066
1252
  const body = await readBody(req)
1067
1253
  const sessionId = typeof body.session === 'string' ? body.session : ''
1068
1254
  const rootSid = resolveBoundRootSid(sessionId)
1255
+ markSessionActivated(rootSid || sessionId)
1069
1256
  const op = typeof body.op === 'string' ? body.op : 'save'
1070
1257
  const reqScope = resolveArchiveHttpScope(body.vaultScope || body.scope || '')
1071
1258
  const workingScope = reqScope === 'global' ? 'global' : 'session'
@@ -1348,6 +1535,7 @@ export function createApiRoutes(deps) {
1348
1535
  const body = await readBody(req)
1349
1536
  const sessionId = typeof body.session === 'string' ? body.session : ''
1350
1537
  const rootSid = resolveBoundRootSid(sessionId)
1538
+ markSessionActivated(rootSid || sessionId)
1351
1539
  const vaultScope = resolveArchiveHttpScope(body.vaultScope || body.scope) === 'global' ? 'global' : 'session'
1352
1540
  const cabinetId = vaultScope === 'global' ? '' : (typeof body.cabinetId === 'string' ? body.cabinetId : '')
1353
1541
  const targetOpt = archiveTarget(runtime, rootSid, { cabinetId, vaultScope })
@@ -1386,6 +1574,7 @@ export function createApiRoutes(deps) {
1386
1574
  sendJson(res, 400, { ok: false, error: '当前未绑定具体会话,无法从本会话档案柜提炼' })
1387
1575
  return
1388
1576
  }
1577
+ markSessionActivated(rootSid)
1389
1578
  const cabinetId = typeof body.cabinetId === 'string' ? body.cabinetId : ''
1390
1579
  const targetOpt = archiveTarget(runtime, rootSid, { cabinetId, vaultScope: 'session' })
1391
1580
  const out = await distillSessionArchivesToGlobal(makeChiefDeps(runtime), {
@@ -1418,6 +1607,7 @@ export function createApiRoutes(deps) {
1418
1607
  const body = await readBody(req)
1419
1608
  const sessionId = typeof body.session === 'string' ? body.session : ''
1420
1609
  const rootSid = resolveBoundRootSid(sessionId)
1610
+ markSessionActivated(rootSid || sessionId)
1421
1611
  const cabinetId = typeof body.cabinetId === 'string' ? body.cabinetId : ''
1422
1612
  const vaultScope = 'session'
1423
1613
  const targetOpt = archiveTarget(runtime, rootSid, { cabinetId, vaultScope })
@@ -1449,6 +1639,7 @@ export function createApiRoutes(deps) {
1449
1639
  const body = await readBody(req)
1450
1640
  const sessionId = typeof body.session === 'string' ? body.session : ''
1451
1641
  const rootSid = resolveRootSessionId(sessionId) || sessionId
1642
+ markSessionActivated(rootSid)
1452
1643
  const cabinetId = typeof body.cabinetId === 'string' ? body.cabinetId : ''
1453
1644
  const vaultScope = resolveArchiveHttpScope(body.vaultScope || body.scope) === 'global' ? 'global' : 'session'
1454
1645
  if (vaultScope !== 'global') {