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.
- package/CHANGELOG.md +74 -0
- package/README.en.md +68 -54
- package/README.md +69 -54
- package/api-routes.js +204 -3
- package/archive-core.js +136 -8
- package/archive-dossiers.js +450 -20
- package/archive-precipitated.js +115 -36
- package/auto-continue.js +13 -5
- package/board-actions.js +120 -10
- package/board-schema.js +331 -6
- package/chief-approvals.js +176 -11
- package/chief-archives.js +158 -30
- package/chief-commands.js +567 -101
- package/chief-compaction.js +1 -0
- package/chief-models.js +264 -27
- package/chief-review.js +455 -107
- package/chief-state.js +918 -33
- package/chief-view.js +20 -14
- package/client.js +13238 -7190
- package/digest.js +22 -10
- package/idle-guard.js +233 -8
- package/locale/en.json +71 -0
- package/locale/zh.json +71 -0
- package/model-tools.js +21 -6
- package/package.json +11 -4
- package/runtime-hooks.js +358 -20
- package/safety/invariant-guards.js +2 -1
- package/subagent-runtime.js +285 -2
package/board-schema.js
CHANGED
|
@@ -8,7 +8,7 @@ import { normalizeMemory } from './memory.js'
|
|
|
8
8
|
|
|
9
9
|
export const API = '/api/dsh-chief'
|
|
10
10
|
|
|
11
|
-
export const BACKEND_BUILD = '1.
|
|
11
|
+
export const BACKEND_BUILD = '1.6.0'
|
|
12
12
|
|
|
13
13
|
export const BOARD_VERSION = 1
|
|
14
14
|
|
|
@@ -252,6 +252,8 @@ export function normalizeTask(raw) {
|
|
|
252
252
|
evidence: str(task.evidence, '').slice(0, 600),
|
|
253
253
|
blockedBy: Array.isArray(task.blockedBy) ? task.blockedBy.map(item => str(item, '')).filter(Boolean) : [],
|
|
254
254
|
parentId: str(task.parentId, '').slice(0, 200),
|
|
255
|
+
planStepId: str(task.planStepId, '').slice(0, 120),
|
|
256
|
+
targetPlanStepId: str(task.targetPlanStepId, '').slice(0, 120),
|
|
255
257
|
assumptions: Array.isArray(task.assumptions) ? task.assumptions.map(a => ({
|
|
256
258
|
text: str(a && a.text, '').slice(0, CAP_ASSUMPTION),
|
|
257
259
|
verified: a && a.verified === true,
|
|
@@ -458,11 +460,15 @@ export function normalizeBoard(raw) {
|
|
|
458
460
|
session: str(board.cursor && board.cursor.session, ''),
|
|
459
461
|
},
|
|
460
462
|
plan: plan
|
|
461
|
-
.map(step =>
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
463
|
+
.map(step => {
|
|
464
|
+
const cleanSummary = str(step && step.summary, '').trim().slice(0, 240)
|
|
465
|
+
return {
|
|
466
|
+
id: str(step && step.id, newId('s')),
|
|
467
|
+
text: str(step && step.text, '').slice(0, 400),
|
|
468
|
+
done: step && step.done === true,
|
|
469
|
+
...(cleanSummary ? { summary: cleanSummary } : {}),
|
|
470
|
+
}
|
|
471
|
+
})
|
|
466
472
|
.filter(step => step.text !== ''),
|
|
467
473
|
tasks: tasks.map(normalizeTask).filter(task => task.title !== ''),
|
|
468
474
|
notes: dedupeNotes(notes.map(normalizeNote).filter(note => note.title !== '' || note.body !== '')),
|
|
@@ -493,6 +499,15 @@ export function guardLogText(verdict, turn, remaining) {
|
|
|
493
499
|
return '空转拦截:第 ' + turn + ' 轮同一个调用连着做了 ' + (loop.count || 0) + ' 次('
|
|
494
500
|
+ String(loop.name || '?').slice(0, 40) + ')结果没变' + tail
|
|
495
501
|
}
|
|
502
|
+
if (verdict && verdict.kind === 'premature-promise') {
|
|
503
|
+
return '空转拦截:第 ' + turn + ' 轮口头预告「'
|
|
504
|
+
+ String(verdict.promiseCue || '').slice(0, 40) + '」却未执行就收工' + tail
|
|
505
|
+
}
|
|
506
|
+
if (verdict && verdict.kind === 'mid-turn-spin') {
|
|
507
|
+
const spin = verdict.spin || verdict.loop || {}
|
|
508
|
+
return '运行中空转急刹:连续空转/报错 ' + (spin.count || 0) + ' 次('
|
|
509
|
+
+ String(spin.name || '?').slice(0, 40) + ')已当场插话纠偏'
|
|
510
|
+
}
|
|
496
511
|
if (verdict && verdict.kind === 'no-goal') {
|
|
497
512
|
return '空转拦截:第 ' + turn + ' 轮板子处在 goal 模式却没有目标' + tail
|
|
498
513
|
}
|
|
@@ -538,6 +553,8 @@ export function summary(board, subagents) {
|
|
|
538
553
|
preset: task.preset || '',
|
|
539
554
|
session: task.session,
|
|
540
555
|
parentId: task.parentId || '',
|
|
556
|
+
planStepId: task.planStepId || '',
|
|
557
|
+
targetPlanStepId: task.targetPlanStepId || '',
|
|
541
558
|
blockedBy: Array.isArray(task.blockedBy) ? task.blockedBy : [],
|
|
542
559
|
intent: task.intent || '',
|
|
543
560
|
evidence: task.evidence || '',
|
|
@@ -578,3 +595,311 @@ export function summary(board, subagents) {
|
|
|
578
595
|
updatedAt: board.updatedAt,
|
|
579
596
|
}
|
|
580
597
|
}
|
|
598
|
+
|
|
599
|
+
/* ------------------------------------------------------ workflow pipeline */
|
|
600
|
+
|
|
601
|
+
/**
|
|
602
|
+
* Resolve which plan step ID is currently active for auto-attaching tasks/subagents.
|
|
603
|
+
* Prefers an open step matching `cursor.step`, then the first incomplete step,
|
|
604
|
+
* and finally the last step when all are done.
|
|
605
|
+
*/
|
|
606
|
+
export function resolveActivePlanStepId(board) {
|
|
607
|
+
const plan = board && Array.isArray(board.plan) ? board.plan.filter(s => s && s.text) : []
|
|
608
|
+
if (!plan.length) return ''
|
|
609
|
+
const cursorStep = str(board && board.cursor && board.cursor.step, '').trim().toLowerCase()
|
|
610
|
+
if (cursorStep) {
|
|
611
|
+
const exact = plan.find(s => !s.done && str(s.text, '').trim().toLowerCase() === cursorStep)
|
|
612
|
+
if (exact) return exact.id
|
|
613
|
+
const prefix = plan.find(s => !s.done && (
|
|
614
|
+
str(s.text, '').trim().toLowerCase().startsWith(cursorStep)
|
|
615
|
+
|| cursorStep.startsWith(str(s.text, '').trim().toLowerCase().slice(0, 24))
|
|
616
|
+
))
|
|
617
|
+
if (prefix) return prefix.id
|
|
618
|
+
}
|
|
619
|
+
const firstOpen = plan.find(s => !s.done)
|
|
620
|
+
return firstOpen ? firstOpen.id : plan[plan.length - 1].id
|
|
621
|
+
}
|
|
622
|
+
|
|
623
|
+
function topoSortRootTasks(roots) {
|
|
624
|
+
if (roots.length <= 1) return roots.slice()
|
|
625
|
+
const byId = new Map(roots.map(t => [t.id, t]))
|
|
626
|
+
const inDeg = new Map(roots.map(t => [t.id, 0]))
|
|
627
|
+
const adj = new Map(roots.map(t => [t.id, []]))
|
|
628
|
+
for (const t of roots) {
|
|
629
|
+
const deps = Array.isArray(t.blockedBy) ? t.blockedBy : []
|
|
630
|
+
for (const depId of deps) {
|
|
631
|
+
if (byId.has(depId) && depId !== t.id) {
|
|
632
|
+
inDeg.set(t.id, (inDeg.get(t.id) || 0) + 1)
|
|
633
|
+
adj.get(depId).push(t.id)
|
|
634
|
+
}
|
|
635
|
+
}
|
|
636
|
+
}
|
|
637
|
+
const ready = roots.filter(t => (inDeg.get(t.id) || 0) === 0).sort((a, b) => (a.createdAt || 0) - (b.createdAt || 0))
|
|
638
|
+
const ordered = []
|
|
639
|
+
const placed = new Set()
|
|
640
|
+
while (ready.length) {
|
|
641
|
+
const cur = ready.shift()
|
|
642
|
+
if (!cur || placed.has(cur.id)) continue
|
|
643
|
+
placed.add(cur.id)
|
|
644
|
+
ordered.push(cur)
|
|
645
|
+
for (const nxtId of adj.get(cur.id) || []) {
|
|
646
|
+
const deg = (inDeg.get(nxtId) || 1) - 1
|
|
647
|
+
inDeg.set(nxtId, deg)
|
|
648
|
+
if (deg <= 0) {
|
|
649
|
+
ready.push(byId.get(nxtId))
|
|
650
|
+
ready.sort((a, b) => (a.createdAt || 0) - (b.createdAt || 0))
|
|
651
|
+
}
|
|
652
|
+
}
|
|
653
|
+
}
|
|
654
|
+
for (const t of roots) {
|
|
655
|
+
if (!placed.has(t.id)) ordered.push(t)
|
|
656
|
+
}
|
|
657
|
+
return ordered
|
|
658
|
+
}
|
|
659
|
+
|
|
660
|
+
function isInternalProbeAgentName(name) {
|
|
661
|
+
const raw = String(name || '').trim().replace(/^@/, '')
|
|
662
|
+
if (!raw) return false
|
|
663
|
+
const leaf = raw.split('/').pop() || raw
|
|
664
|
+
return /^mnemon-(?:review|run)(?:[-_]|$)/i.test(leaf)
|
|
665
|
+
}
|
|
666
|
+
|
|
667
|
+
/**
|
|
668
|
+
* Derive sequential workflow stages (`① -> ② -> ③ ...`), attached tasks,
|
|
669
|
+
* sprouted subagent branches, return-edge targets, and unified 4-state colors.
|
|
670
|
+
*/
|
|
671
|
+
export function derivePlanFlowProgress(board, subagents = []) {
|
|
672
|
+
const safeBoard = board && typeof board === 'object' ? board : {}
|
|
673
|
+
const plan = Array.isArray(safeBoard.plan) ? safeBoard.plan.filter(s => s && s.text) : []
|
|
674
|
+
const tasks = Array.isArray(safeBoard.tasks) ? safeBoard.tasks.filter(t => t && t.title) : []
|
|
675
|
+
const rawAgents = Array.isArray(subagents) ? subagents.filter(a => a && a.name && a.name !== 'lead') : []
|
|
676
|
+
const visibleAgents = rawAgents.filter(a => !(isInternalProbeAgentName(a.name) && a.status !== 'running'))
|
|
677
|
+
|
|
678
|
+
let mode = 'empty'
|
|
679
|
+
let stages = []
|
|
680
|
+
|
|
681
|
+
if (plan.length > 0) {
|
|
682
|
+
mode = 'plan'
|
|
683
|
+
stages = plan.map((s, idx) => ({
|
|
684
|
+
id: str(s.id, `s-${idx + 1}`),
|
|
685
|
+
index: idx + 1,
|
|
686
|
+
text: str(s.text, ''),
|
|
687
|
+
done: s.done === true,
|
|
688
|
+
summary: str(s.summary, ''),
|
|
689
|
+
source: 'plan',
|
|
690
|
+
rootTaskId: '',
|
|
691
|
+
tasks: [],
|
|
692
|
+
agents: [],
|
|
693
|
+
returnEdges: [],
|
|
694
|
+
status: 'todo',
|
|
695
|
+
}))
|
|
696
|
+
} else if (tasks.length > 0) {
|
|
697
|
+
mode = 'task-root'
|
|
698
|
+
const taskIds = new Set(tasks.map(t => t.id))
|
|
699
|
+
const roots = topoSortRootTasks(tasks.filter(t => !t.parentId || !taskIds.has(t.parentId) || t.parentId === t.id))
|
|
700
|
+
stages = roots.map((t, idx) => ({
|
|
701
|
+
id: 's-' + t.id,
|
|
702
|
+
index: idx + 1,
|
|
703
|
+
text: t.title,
|
|
704
|
+
done: t.status === 'done',
|
|
705
|
+
summary: t.evidence || '',
|
|
706
|
+
source: 'task-root',
|
|
707
|
+
rootTaskId: t.id,
|
|
708
|
+
tasks: [],
|
|
709
|
+
agents: [],
|
|
710
|
+
returnEdges: [],
|
|
711
|
+
status: 'todo',
|
|
712
|
+
}))
|
|
713
|
+
} else if (visibleAgents.length > 0 || str(safeBoard.objective, '').trim()) {
|
|
714
|
+
mode = 'synthetic'
|
|
715
|
+
stages = [{
|
|
716
|
+
id: 'stage-main',
|
|
717
|
+
index: 1,
|
|
718
|
+
text: str(safeBoard.objective, '').trim() || '当前主线任务',
|
|
719
|
+
done: false,
|
|
720
|
+
summary: '',
|
|
721
|
+
source: 'synthetic',
|
|
722
|
+
rootTaskId: '',
|
|
723
|
+
tasks: [],
|
|
724
|
+
agents: [],
|
|
725
|
+
returnEdges: [],
|
|
726
|
+
status: 'todo',
|
|
727
|
+
}]
|
|
728
|
+
}
|
|
729
|
+
|
|
730
|
+
if (!stages.length) {
|
|
731
|
+
return {
|
|
732
|
+
mode: 'empty',
|
|
733
|
+
stages: [],
|
|
734
|
+
activeStageId: '',
|
|
735
|
+
orphanTasks: [],
|
|
736
|
+
stats: {
|
|
737
|
+
totalStages: 0,
|
|
738
|
+
doneStages: 0,
|
|
739
|
+
doingStages: 0,
|
|
740
|
+
blockedStages: 0,
|
|
741
|
+
totalAgents: 0,
|
|
742
|
+
runningAgents: 0,
|
|
743
|
+
doneAgents: 0,
|
|
744
|
+
},
|
|
745
|
+
}
|
|
746
|
+
}
|
|
747
|
+
|
|
748
|
+
const stageById = new Map(stages.map(s => [s.id, s]))
|
|
749
|
+
const taskById = new Map(tasks.map(t => [t.id, t]))
|
|
750
|
+
const activeStepId = resolveActivePlanStepId(safeBoard) || (stages.find(s => !s.done)?.id || stages[stages.length - 1].id)
|
|
751
|
+
const doneStagesRev = [...stages].reverse().find(s => s.done)
|
|
752
|
+
const lastDoneStepId = doneStagesRev ? doneStagesRev.id : activeStepId
|
|
753
|
+
|
|
754
|
+
const taskStageCache = new Map()
|
|
755
|
+
const resolveTaskStageId = (task, seen = new Set()) => {
|
|
756
|
+
if (!task) return ''
|
|
757
|
+
if (taskStageCache.has(task.id)) return taskStageCache.get(task.id)
|
|
758
|
+
let hitId = ''
|
|
759
|
+
if (task.planStepId && stageById.has(task.planStepId)) {
|
|
760
|
+
hitId = task.planStepId
|
|
761
|
+
} else if (stageById.has('s-' + task.id)) {
|
|
762
|
+
hitId = 's-' + task.id
|
|
763
|
+
} else {
|
|
764
|
+
const byPrefix = stages.find(s => s.text && task.title && s.text.startsWith(task.title))
|
|
765
|
+
if (byPrefix) hitId = byPrefix.id
|
|
766
|
+
}
|
|
767
|
+
if (!hitId && task.parentId && taskById.has(task.parentId) && !seen.has(task.id)) {
|
|
768
|
+
seen.add(task.id)
|
|
769
|
+
hitId = resolveTaskStageId(taskById.get(task.parentId), seen)
|
|
770
|
+
}
|
|
771
|
+
if (!hitId) {
|
|
772
|
+
if (task.status === 'done') hitId = lastDoneStepId || activeStepId
|
|
773
|
+
else hitId = activeStepId
|
|
774
|
+
}
|
|
775
|
+
taskStageCache.set(task.id, hitId)
|
|
776
|
+
return hitId
|
|
777
|
+
}
|
|
778
|
+
|
|
779
|
+
const orphanTasks = []
|
|
780
|
+
for (const t of tasks) {
|
|
781
|
+
const sid = resolveTaskStageId(t)
|
|
782
|
+
const st = sid ? stageById.get(sid) : null
|
|
783
|
+
if (st) st.tasks.push(t)
|
|
784
|
+
else orphanTasks.push(t)
|
|
785
|
+
}
|
|
786
|
+
|
|
787
|
+
const matchAgentTasks = agentObj => {
|
|
788
|
+
const aName = str(agentObj && agentObj.name, '').trim().toLowerCase()
|
|
789
|
+
const aLeaf = aName.split('/').pop()
|
|
790
|
+
const aId = str(agentObj && agentObj.id, '').trim()
|
|
791
|
+
const aTaskId = str(agentObj && agentObj.taskId, '').trim()
|
|
792
|
+
const aTaskIds = new Set(Array.isArray(agentObj && agentObj.tasks) ? agentObj.tasks.map(x => str(x && x.id, '').trim()).filter(Boolean) : [])
|
|
793
|
+
if (aTaskId) aTaskIds.add(aTaskId)
|
|
794
|
+
return tasks.filter(t => {
|
|
795
|
+
if (aTaskIds.has(t.id)) return true
|
|
796
|
+
if (aId && t.session && String(t.session) === aId) return true
|
|
797
|
+
const ref = str(t.subagent || t.owner, '').trim().toLowerCase()
|
|
798
|
+
if (!ref || ref === 'lead' || ref === 'agent' || ref === 'operator') return false
|
|
799
|
+
return ref === aName || (aLeaf && ref.split('/').pop() === aLeaf)
|
|
800
|
+
})
|
|
801
|
+
}
|
|
802
|
+
|
|
803
|
+
let runningAgents = 0
|
|
804
|
+
let doneAgents = 0
|
|
805
|
+
for (const agentObj of visibleAgents) {
|
|
806
|
+
const agentTasks = matchAgentTasks(agentObj)
|
|
807
|
+
const rawAgentSt = str(agentObj.status, '').trim().toLowerCase()
|
|
808
|
+
const hasBlockedTask = agentTasks.some(t => t.status === 'blocked')
|
|
809
|
+
const hasDoingTask = agentTasks.some(t => t.status === 'doing')
|
|
810
|
+
const hasOpenTask = agentTasks.some(t => t.status === 'todo' || t.status === 'doing' || t.status === 'blocked')
|
|
811
|
+
const allTasksDone = agentTasks.length > 0 && agentTasks.every(t => t.status === 'done')
|
|
812
|
+
const isAgentFailed = /error|fail|crash|blocked/.test(rawAgentSt)
|
|
813
|
+
const isAgentRunning = rawAgentSt === 'running' || rawAgentSt === 'active' || rawAgentSt === 'working'
|
|
814
|
+
const isAgentEnded = rawAgentSt !== '' && !isAgentRunning && !isAgentFailed && rawAgentSt !== 'todo' && rawAgentSt !== 'queued'
|
|
815
|
+
|
|
816
|
+
let agentStatus = 'todo'
|
|
817
|
+
if (hasBlockedTask || isAgentFailed) agentStatus = 'blocked'
|
|
818
|
+
else if (isAgentRunning || hasDoingTask) agentStatus = 'doing'
|
|
819
|
+
else if (allTasksDone || (isAgentEnded && !hasOpenTask)) agentStatus = 'done'
|
|
820
|
+
|
|
821
|
+
if (agentStatus === 'doing') runningAgents += 1
|
|
822
|
+
else if (agentStatus === 'done') doneAgents += 1
|
|
823
|
+
|
|
824
|
+
const primaryTask = agentTasks.find(t => t.status === 'doing' || t.status === 'blocked')
|
|
825
|
+
|| agentTasks.find(t => t.status === 'todo')
|
|
826
|
+
|| agentTasks[0]
|
|
827
|
+
|| null
|
|
828
|
+
|
|
829
|
+
let fromStepId = primaryTask ? resolveTaskStageId(primaryTask) : ''
|
|
830
|
+
if (!fromStepId || !stageById.has(fromStepId)) {
|
|
831
|
+
fromStepId = agentStatus === 'done' ? (lastDoneStepId || activeStepId) : activeStepId
|
|
832
|
+
}
|
|
833
|
+
const rawTargetId = primaryTask && primaryTask.targetPlanStepId ? primaryTask.targetPlanStepId : ''
|
|
834
|
+
const toStepId = (rawTargetId && stageById.has(rawTargetId)) ? rawTargetId : fromStepId
|
|
835
|
+
|
|
836
|
+
const stage = stageById.get(fromStepId) || stages[0]
|
|
837
|
+
const node = {
|
|
838
|
+
name: str(agentObj.name, ''),
|
|
839
|
+
id: str(agentObj.id, ''),
|
|
840
|
+
parentId: str(agentObj.parentId, ''),
|
|
841
|
+
rawStatus: rawAgentSt,
|
|
842
|
+
agentStatus,
|
|
843
|
+
taskIds: agentTasks.map(t => t.id),
|
|
844
|
+
primaryTaskId: primaryTask ? primaryTask.id : '',
|
|
845
|
+
fromStepId: stage.id,
|
|
846
|
+
toStepId,
|
|
847
|
+
crossStep: toStepId !== stage.id,
|
|
848
|
+
}
|
|
849
|
+
stage.agents.push(node)
|
|
850
|
+
stage.returnEdges.push({
|
|
851
|
+
fromAgent: node.name,
|
|
852
|
+
taskId: node.primaryTaskId,
|
|
853
|
+
fromStepId: stage.id,
|
|
854
|
+
toStepId,
|
|
855
|
+
status: agentStatus,
|
|
856
|
+
crossStep: toStepId !== stage.id,
|
|
857
|
+
})
|
|
858
|
+
}
|
|
859
|
+
|
|
860
|
+
const firstOpenStage = stages.find(s => !s.done) || null
|
|
861
|
+
const hasAnyDone = stages.some(s => s.done)
|
|
862
|
+
const hasCursorStep = Boolean(str(safeBoard.cursor && safeBoard.cursor.step, '').trim())
|
|
863
|
+
const hasAnyActivity = hasAnyDone
|
|
864
|
+
|| hasCursorStep
|
|
865
|
+
|| tasks.some(t => t.status !== 'todo')
|
|
866
|
+
|| visibleAgents.length > 0
|
|
867
|
+
|
|
868
|
+
let doneStages = 0
|
|
869
|
+
let doingStages = 0
|
|
870
|
+
let blockedStages = 0
|
|
871
|
+
for (const stage of stages) {
|
|
872
|
+
if (stage.done) {
|
|
873
|
+
stage.status = 'done'
|
|
874
|
+
doneStages += 1
|
|
875
|
+
} else if (stage.tasks.some(t => t.status === 'blocked') || stage.agents.some(a => a.agentStatus === 'blocked')) {
|
|
876
|
+
stage.status = 'blocked'
|
|
877
|
+
blockedStages += 1
|
|
878
|
+
} else if (
|
|
879
|
+
stage.tasks.some(t => t.status === 'doing')
|
|
880
|
+
|| stage.agents.some(a => a.agentStatus === 'doing')
|
|
881
|
+
|| (firstOpenStage && firstOpenStage.id === stage.id && hasAnyActivity)
|
|
882
|
+
) {
|
|
883
|
+
stage.status = 'doing'
|
|
884
|
+
doingStages += 1
|
|
885
|
+
} else {
|
|
886
|
+
stage.status = 'todo'
|
|
887
|
+
}
|
|
888
|
+
}
|
|
889
|
+
|
|
890
|
+
return {
|
|
891
|
+
mode,
|
|
892
|
+
stages,
|
|
893
|
+
activeStageId: activeStepId,
|
|
894
|
+
orphanTasks,
|
|
895
|
+
stats: {
|
|
896
|
+
totalStages: stages.length,
|
|
897
|
+
doneStages,
|
|
898
|
+
doingStages,
|
|
899
|
+
blockedStages,
|
|
900
|
+
totalAgents: visibleAgents.length,
|
|
901
|
+
runningAgents,
|
|
902
|
+
doneAgents,
|
|
903
|
+
},
|
|
904
|
+
}
|
|
905
|
+
}
|
package/chief-approvals.js
CHANGED
|
@@ -42,9 +42,9 @@ import { deliverCrossMessage } from './chief-network.js'
|
|
|
42
42
|
import { getChiefView } from './chief-view.js'
|
|
43
43
|
import { buildChiefArchiveUpsertFn } from './chief-archives.js'
|
|
44
44
|
import { buildChiefCompactFn, recordSmartCompactionChatReceipt } from './chief-compaction.js'
|
|
45
|
-
import { askOrCommandChief } from './chief-commands.js'
|
|
45
|
+
import { askOrCommandChief, executeChiefArchiveInjectionOp } from './chief-commands.js'
|
|
46
46
|
|
|
47
|
-
export async function handleSuggestionDecision(deps, { sessionId, suggestionId, decision, editedText, boundField, pinGlobal, _suppressChatReceipt = false }) {
|
|
47
|
+
export async function handleSuggestionDecision(deps, { sessionId, suggestionId, decision, editedText, boundField, pinGlobal, _suppressChatReceipt = false, _byUserClick = true }) {
|
|
48
48
|
const rootId = deps.resolveRootSessionId(sessionId) || String(sessionId || 'global')
|
|
49
49
|
let found = null
|
|
50
50
|
mutateChiefState((state) => {
|
|
@@ -186,6 +186,60 @@ export async function handleSuggestionDecision(deps, { sessionId, suggestionId,
|
|
|
186
186
|
}
|
|
187
187
|
}
|
|
188
188
|
|
|
189
|
+
if (decision === 'approve' && found.kind === 'wall_removal') {
|
|
190
|
+
const cwdNow = resolveSessionWorkspaceCwd(deps, rootId)
|
|
191
|
+
const spec = (found.wallRemovalSpec && typeof found.wallRemovalSpec === 'object') ? found.wallRemovalSpec : {}
|
|
192
|
+
const targetWallId = String(spec.wallId || '').trim()
|
|
193
|
+
const opType = spec.action === 'remove' ? 'remove' : 'set_scope'
|
|
194
|
+
if (!targetWallId) {
|
|
195
|
+
return { ok: false, error: '拆墙申请缺少有效的目标物理驻墙 ID (wallId)' }
|
|
196
|
+
}
|
|
197
|
+
const wallRes = mutateGuardWalls({
|
|
198
|
+
op: opType,
|
|
199
|
+
wallId: targetWallId,
|
|
200
|
+
...(opType === 'set_scope' ? { scope: 'paused' } : {}),
|
|
201
|
+
cwd: cwdNow,
|
|
202
|
+
callerOrigin: 'human_approval',
|
|
203
|
+
humanConfirmToken: HUMAN_BOUNDS_CONFIRM_TOKEN,
|
|
204
|
+
})
|
|
205
|
+
if (!wallRes || !wallRes.ok) {
|
|
206
|
+
return { ok: false, error: (wallRes && wallRes.error) || '按人工批复调整物理驻墙失败' }
|
|
207
|
+
}
|
|
208
|
+
const wallTitle = spec.wallTitle || (wallRes.wall && wallRes.wall.title) || targetWallId
|
|
209
|
+
const actVerb = opType === 'remove' ? '拆除移除' : '暂挂休眠'
|
|
210
|
+
mutateChiefState((state) => {
|
|
211
|
+
const ws = ensureWorkspace(state, rootId)
|
|
212
|
+
if (!_suppressChatReceipt) {
|
|
213
|
+
ws.chat.push({
|
|
214
|
+
id: nextId('chat'),
|
|
215
|
+
role: 'chief',
|
|
216
|
+
kind: 'receipt',
|
|
217
|
+
text: `- 【🧱 已按人工批示${actVerb}物理驻墙】${wallTitle} (\`${targetWallId}\`)\n- 【代理上报与拆墙依据】${clamp(spec.blockerEvidence || found.reason || '主/子代理撞墙无法绕过,参谋呈报经人类批准后执行', 180)}`,
|
|
218
|
+
actions: [`已${actVerb}驻墙`],
|
|
219
|
+
at: Date.now(),
|
|
220
|
+
})
|
|
221
|
+
if (ws.chat.length > CHAT_CAP) ws.chat = ws.chat.slice(-CHAT_CAP)
|
|
222
|
+
}
|
|
223
|
+
pushAuditToWs(ws, {
|
|
224
|
+
mode: resolveWorkspaceConfig(state, rootId).permissionMode,
|
|
225
|
+
stage: 'guard_wall',
|
|
226
|
+
rule: 'wall_removal_approved',
|
|
227
|
+
decision: 'executed',
|
|
228
|
+
operation: `人工批复${actVerb}物理驻墙`,
|
|
229
|
+
target: `${wallTitle} (${targetWallId})`,
|
|
230
|
+
reason: clamp(spec.reason || found.reason || '代理撞墙无法绕过,经人类显式审批后放行', 180),
|
|
231
|
+
outcome: 'ok',
|
|
232
|
+
by: 'operator',
|
|
233
|
+
}, rootId)
|
|
234
|
+
})
|
|
235
|
+
return {
|
|
236
|
+
ok: true,
|
|
237
|
+
wallRemoved: { wallId: targetWallId, title: wallTitle, action: opType === 'remove' ? 'remove' : 'pause' },
|
|
238
|
+
guardWalls: wallRes.walls,
|
|
239
|
+
view: _suppressChatReceipt ? null : await getChiefView(deps, rootId, { autoSummarize: false }),
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
|
|
189
243
|
if (decision === 'approve' && found.kind === 'goal_approval') {
|
|
190
244
|
const res = deps.runAction
|
|
191
245
|
? deps.runAction({ action: 'goal.approve', session: rootId, by: 'user' })
|
|
@@ -213,6 +267,53 @@ export async function handleSuggestionDecision(deps, { sessionId, suggestionId,
|
|
|
213
267
|
}
|
|
214
268
|
}
|
|
215
269
|
|
|
270
|
+
if (decision === 'approve' && found.kind === 'archive_op') {
|
|
271
|
+
const spec = (found.archiveSpec && typeof found.archiveSpec === 'object')
|
|
272
|
+
? found.archiveSpec
|
|
273
|
+
: ((found.archiveOp && typeof found.archiveOp === 'object') ? found.archiveOp : {})
|
|
274
|
+
const archRes = await executeChiefArchiveInjectionOp(deps, rootId, {
|
|
275
|
+
op: spec.op || 'pin_to_desktop',
|
|
276
|
+
vaultScope: spec.vaultScope || 'session',
|
|
277
|
+
id: spec.id || '',
|
|
278
|
+
title: spec.title || found.title || '',
|
|
279
|
+
target: spec.target || found.target || 'lead',
|
|
280
|
+
instruction: editedText || spec.instruction || found.reason || '',
|
|
281
|
+
}, { byLabel: 'DSH 参谋长 (获批档案调配)' }).catch(e => ({ ok: false, error: (e && e.message) || 'archive-op-failed' }))
|
|
282
|
+
return {
|
|
283
|
+
ok: Boolean(archRes && archRes.ok),
|
|
284
|
+
archiveApplied: archRes,
|
|
285
|
+
error: archRes && !archRes.ok ? archRes.error : '',
|
|
286
|
+
view: _suppressChatReceipt ? null : await getChiefView(deps, rootId, { autoSummarize: false }),
|
|
287
|
+
}
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
if (decision === 'approve' && found.kind === 'model_effort') {
|
|
291
|
+
const spec = (found.effortSpec && typeof found.effortSpec === 'object')
|
|
292
|
+
? found.effortSpec
|
|
293
|
+
: ((found.effortOp && typeof found.effortOp === 'object') ? found.effortOp : {})
|
|
294
|
+
const target = spec.target || found.target || 'lead'
|
|
295
|
+
const eff = spec.effort || 'medium'
|
|
296
|
+
const effReason = spec.reason || found.reason || '按参谋建议调整思考强度'
|
|
297
|
+
const effRes = deps && typeof deps.controlMainAgent === 'function'
|
|
298
|
+
? await deps.controlMainAgent({
|
|
299
|
+
rootId,
|
|
300
|
+
sessionId: rootId,
|
|
301
|
+
op: 'set_effort',
|
|
302
|
+
target,
|
|
303
|
+
reasoningEffort: eff,
|
|
304
|
+
reason: effReason,
|
|
305
|
+
byUser: Boolean(_byUserClick !== false),
|
|
306
|
+
}).catch(e => ({ ok: false, error: (e && e.message) || 'effort-failed' }))
|
|
307
|
+
: { ok: false, error: '当前宿主未绑定会话控制接口' }
|
|
308
|
+
return {
|
|
309
|
+
ok: Boolean(effRes && effRes.ok),
|
|
310
|
+
effortApplied: effRes,
|
|
311
|
+
lockedByUser: Boolean(effRes && effRes.lockedByUser),
|
|
312
|
+
error: effRes && !effRes.ok ? effRes.error : '',
|
|
313
|
+
view: _suppressChatReceipt ? null : await getChiefView(deps, rootId, { autoSummarize: false }),
|
|
314
|
+
}
|
|
315
|
+
}
|
|
316
|
+
|
|
216
317
|
if (decision === 'approve' && (found.kind === 'relay' || found.kind === 'advice')) {
|
|
217
318
|
const body = editedText ? cleanMultiline(editedText, 1200) : found.draftText
|
|
218
319
|
const deliverTarget = found.target || 'lead'
|
|
@@ -250,6 +351,10 @@ export async function executeBatchSuggestions(deps, {
|
|
|
250
351
|
ids = [],
|
|
251
352
|
includeGoalApproval = false,
|
|
252
353
|
pinGlobalDefault = undefined,
|
|
354
|
+
skipGuardWalls = false,
|
|
355
|
+
skipGlobalBounds = false,
|
|
356
|
+
byAutoRunner = false,
|
|
357
|
+
skipChatReceipt = false,
|
|
253
358
|
} = {}) {
|
|
254
359
|
const rootId = (deps && typeof deps.resolveRootSessionId === 'function' ? deps.resolveRootSessionId(sessionId) : '') || String(sessionId || 'global')
|
|
255
360
|
const stateSnap = loadChiefState()
|
|
@@ -276,7 +381,7 @@ export async function executeBatchSuggestions(deps, {
|
|
|
276
381
|
blocked: [],
|
|
277
382
|
skippedGoals: [],
|
|
278
383
|
summaries: ['当前没有待执行的参谋长建议。'],
|
|
279
|
-
view: await getChiefView(deps, rootId, { autoSummarize: false }),
|
|
384
|
+
view: skipChatReceipt ? null : await getChiefView(deps, rootId, { autoSummarize: false }),
|
|
280
385
|
}
|
|
281
386
|
}
|
|
282
387
|
|
|
@@ -291,8 +396,20 @@ export async function executeBatchSuggestions(deps, {
|
|
|
291
396
|
skippedGoals.push({ id: sug.id, title: sug.title })
|
|
292
397
|
continue
|
|
293
398
|
}
|
|
399
|
+
if (skipGuardWalls && (sug.kind === 'guard_wall' || sug.kind === 'wall_removal')) {
|
|
400
|
+
skippedGoals.push({ id: sug.id, title: sug.title })
|
|
401
|
+
continue
|
|
402
|
+
}
|
|
403
|
+
if (sug.kind === 'wall_removal' && byAutoRunner) {
|
|
404
|
+
skippedGoals.push({ id: sug.id, title: sug.title })
|
|
405
|
+
continue
|
|
406
|
+
}
|
|
407
|
+
if (skipGlobalBounds && sug.kind === 'bound' && sug.pinGlobal) {
|
|
408
|
+
skippedGoals.push({ id: sug.id, title: sug.title })
|
|
409
|
+
continue
|
|
410
|
+
}
|
|
294
411
|
|
|
295
|
-
if (sug.kind !== 'bound' && sug.kind !== 'goal_approval' && sug.kind !== 'guard_wall') {
|
|
412
|
+
if (sug.kind !== 'bound' && sug.kind !== 'goal_approval' && sug.kind !== 'guard_wall' && sug.kind !== 'wall_removal') {
|
|
296
413
|
const cmdSample = `${sug.draftText || ''}\n${sug.title || ''}`.trim()
|
|
297
414
|
const guardRes = evaluateSafetyGuardHardRules({
|
|
298
415
|
tool: 'relay',
|
|
@@ -314,7 +431,7 @@ export async function executeBatchSuggestions(deps, {
|
|
|
314
431
|
target: clamp(sug.title, 80),
|
|
315
432
|
reason: guardRes.reason,
|
|
316
433
|
outcome: 'blocked',
|
|
317
|
-
by: 'operator',
|
|
434
|
+
by: byAutoRunner ? 'chief' : 'operator',
|
|
318
435
|
}, rootId)
|
|
319
436
|
})
|
|
320
437
|
blocked.push({ id: sug.id, title: sug.title, reason: guardRes.reason })
|
|
@@ -329,6 +446,7 @@ export async function executeBatchSuggestions(deps, {
|
|
|
329
446
|
decision: 'approve',
|
|
330
447
|
pinGlobal: sug.kind === 'bound' && typeof pinGlobalDefault === 'boolean' ? pinGlobalDefault : undefined,
|
|
331
448
|
_suppressChatReceipt: true,
|
|
449
|
+
_byUserClick: !byAutoRunner,
|
|
332
450
|
})
|
|
333
451
|
|
|
334
452
|
if (res && res.ok) {
|
|
@@ -340,8 +458,15 @@ export async function executeBatchSuggestions(deps, {
|
|
|
340
458
|
const scopeLabel = w.scope === 'workspace' ? '仅本工程' : '🌐全局随身'
|
|
341
459
|
const patPreview = Array.isArray(w.patterns) ? clamp(w.patterns.join(', '), 60) : ''
|
|
342
460
|
summaries.push(`🧱 已落锁物理驻墙「${clamp(w.title, 36)}」(${scopeLabel}${patPreview ? ` · \`${patPreview}\`` : ''})`)
|
|
461
|
+
} else if (res.wallRemoved) {
|
|
462
|
+
const r = res.wallRemoved
|
|
463
|
+
summaries.push(`🧱 已按人工批复${r.action === 'remove' ? '拆除' : '暂挂'}物理驻墙「${clamp(r.title || r.wallId, 36)}」`)
|
|
343
464
|
} else if (res.goalApproved) {
|
|
344
465
|
summaries.push('✅ 已批准并结项当前核心目标')
|
|
466
|
+
} else if (res.archiveApplied && res.archiveApplied.actionText) {
|
|
467
|
+
summaries.push(`✅ ${res.archiveApplied.actionText}`)
|
|
468
|
+
} else if (res.effortApplied && res.effortApplied.reasoningEffort) {
|
|
469
|
+
summaries.push(`✅ 🧠 已调挡 @${res.effortApplied.target || 'lead'} → ${res.effortApplied.reasoningEffort}`)
|
|
345
470
|
} else if (res.delivery && res.delivery.target) {
|
|
346
471
|
summaries.push(`✅ 已采纳并递话给 @${res.delivery.target}:「${clamp(sug.title, 36)}」`)
|
|
347
472
|
} else {
|
|
@@ -350,7 +475,7 @@ export async function executeBatchSuggestions(deps, {
|
|
|
350
475
|
} else {
|
|
351
476
|
const errMsg = (res && res.error) || '执行未完成'
|
|
352
477
|
failed.push({ id: sug.id, title: sug.title, error: errMsg })
|
|
353
|
-
summaries.push(
|
|
478
|
+
summaries.push(`${res && res.lockedByUser ? '🔒' : '⚠️'} ${res && res.lockedByUser ? '用户已锁挡跳过' : '未完成'}「${clamp(sug.title, 36)}」:${errMsg}`)
|
|
354
479
|
}
|
|
355
480
|
}
|
|
356
481
|
|
|
@@ -360,14 +485,14 @@ export async function executeBatchSuggestions(deps, {
|
|
|
360
485
|
mode: permMode,
|
|
361
486
|
stage: 'executor',
|
|
362
487
|
decision: executed.length > 0 ? 'executed' : (blocked.length > 0 ? 'blocked' : 'noop'),
|
|
363
|
-
operation: '统一执行参谋建议',
|
|
488
|
+
operation: byAutoRunner ? 'L3强介入一站式执行建议' : '统一执行参谋建议',
|
|
364
489
|
target: `${executed.length} 项执行 / ${blocked.length} 项拦截 / ${skippedGoals.length} 项跳过`,
|
|
365
490
|
reason: clamp(summaries.slice(0, 3).join(';'), 200) || '批量执行建议',
|
|
366
491
|
outcome: blocked.length > 0 && executed.length === 0 ? 'blocked' : 'ok',
|
|
367
|
-
by: 'operator',
|
|
492
|
+
by: byAutoRunner ? 'chief' : 'operator',
|
|
368
493
|
}, rootId)
|
|
369
|
-
if (executed.length > 0 || blocked.length > 0 || failed.length > 0) {
|
|
370
|
-
const header = `- 【📋 统一执行参谋建议回执】完成 ${executed.length} 项${blocked.length ? ` · 拦截 ${blocked.length} 项` : ''}${failed.length ? ` · 异常 ${failed.length} 项` : ''}${skippedGoals.length ? ` ·
|
|
494
|
+
if (!skipChatReceipt && (executed.length > 0 || blocked.length > 0 || failed.length > 0)) {
|
|
495
|
+
const header = `- 【📋 统一执行参谋建议回执】完成 ${executed.length} 项${blocked.length ? ` · 拦截 ${blocked.length} 项` : ''}${failed.length ? ` · 异常 ${failed.length} 项` : ''}${skippedGoals.length ? ` · 保留需单独拍板项 ${skippedGoals.length} 项` : ''}`
|
|
371
496
|
const actionTags = [`📋 执行建议 ×${executed.length}`]
|
|
372
497
|
if (blocked.length) actionTags.push(`🛡️ 红线拦截 ×${blocked.length}`)
|
|
373
498
|
ws.chat.push({
|
|
@@ -393,7 +518,7 @@ export async function executeBatchSuggestions(deps, {
|
|
|
393
518
|
failed,
|
|
394
519
|
skippedGoals,
|
|
395
520
|
summaries,
|
|
396
|
-
view: await getChiefView(deps, rootId, { autoSummarize: executed.some(e => e.kind === 'bound') }),
|
|
521
|
+
view: skipChatReceipt ? null : await getChiefView(deps, rootId, { autoSummarize: executed.some(e => e.kind === 'bound') }),
|
|
397
522
|
}
|
|
398
523
|
}
|
|
399
524
|
|
|
@@ -648,6 +773,46 @@ export async function handleApprovalDecision(deps, {
|
|
|
648
773
|
: ((execRes && execRes.error) || '压缩未完成')
|
|
649
774
|
appendChiefAuditLog(rootId, { mode: currentMode, stage: 'executor', rule: 'approved_once', decision: 'executed_approved', operation: found.operationLabel, target: found.targetLabel, reason: clamp(resultMsg, 120), outcome: execOk ? 'ok' : 'error', by: 'operator' })
|
|
650
775
|
}
|
|
776
|
+
} else if (found.kind === 'archive_op') {
|
|
777
|
+
const scopeCheck = verifyCapabilityScope(resolvedScope, { kind: 'archive_op', tool: 'archive_op', target: payload.title || payload.id || found.targetLabel || '' })
|
|
778
|
+
if (!scopeCheck.ok) {
|
|
779
|
+
execOk = false
|
|
780
|
+
execRes = { ok: false, deniedByScopeMismatch: true, error: scopeCheck.error }
|
|
781
|
+
resultMsg = scopeCheck.error
|
|
782
|
+
appendChiefAuditLog(rootId, { mode: currentMode, stage: 'scope_guard', rule: scopeCheck.rule, decision: 'rejected', operation: found.operationLabel, target: found.targetLabel, reason: scopeCheck.error, outcome: 'rejected', by: 'operator' })
|
|
783
|
+
} else {
|
|
784
|
+
execRes = await executeChiefArchiveInjectionOp(deps, rootId, payload, { input: found.reason || '', byLabel: 'DSH 参谋 (逐项授权档案调配)' })
|
|
785
|
+
execOk = Boolean(execRes && execRes.ok)
|
|
786
|
+
resultMsg = execOk ? (execRes.actionText || '已完成授权档案调配') : ((execRes && execRes.error) || '档案调配失败')
|
|
787
|
+
appendChiefAuditLog(rootId, { mode: currentMode, stage: 'executor', rule: 'approved_once', decision: 'executed_approved', operation: found.operationLabel, target: found.targetLabel, reason: clamp(resultMsg, 120), outcome: execOk ? 'ok' : 'error', by: 'operator' })
|
|
788
|
+
}
|
|
789
|
+
} else if (found.kind === 'model_effort') {
|
|
790
|
+
const effTarget = payload.target || 'lead'
|
|
791
|
+
const scopeCheck = verifyCapabilityScope(resolvedScope, { kind: 'model_effort', tool: 'model_effort', target: effTarget })
|
|
792
|
+
if (!scopeCheck.ok) {
|
|
793
|
+
execOk = false
|
|
794
|
+
execRes = { ok: false, deniedByScopeMismatch: true, error: scopeCheck.error }
|
|
795
|
+
resultMsg = scopeCheck.error
|
|
796
|
+
appendChiefAuditLog(rootId, { mode: currentMode, stage: 'scope_guard', rule: scopeCheck.rule, decision: 'rejected', operation: found.operationLabel, target: found.targetLabel, reason: scopeCheck.error, outcome: 'rejected', by: 'operator' })
|
|
797
|
+
} else if (deps && typeof deps.controlMainAgent === 'function') {
|
|
798
|
+
execRes = await deps.controlMainAgent({
|
|
799
|
+
rootId,
|
|
800
|
+
sessionId: rootId,
|
|
801
|
+
op: 'set_effort',
|
|
802
|
+
target: effTarget,
|
|
803
|
+
reasoningEffort: payload.reasoningEffort || payload.effort || 'medium',
|
|
804
|
+
reason: payload.reason || found.reason || '',
|
|
805
|
+
byUser: false,
|
|
806
|
+
})
|
|
807
|
+
execOk = Boolean(execRes && execRes.ok)
|
|
808
|
+
resultMsg = execOk
|
|
809
|
+
? `已将 @${effTarget} 思考强度调为 ${execRes.reasoningEffort || payload.reasoningEffort || 'medium'}`
|
|
810
|
+
: ((execRes && execRes.error) || '思考强度调配未成功')
|
|
811
|
+
appendChiefAuditLog(rootId, { mode: currentMode, stage: execRes && execRes.lockedByUser ? 'model_effort_lock' : 'executor', rule: execRes && execRes.lockedByUser ? 'user_effort_lock' : 'approved_once', decision: execOk ? 'executed_approved' : 'blocked', operation: found.operationLabel, target: found.targetLabel, reason: clamp(resultMsg, 120), outcome: execOk ? 'ok' : 'blocked', by: 'operator' })
|
|
812
|
+
} else {
|
|
813
|
+
execOk = false
|
|
814
|
+
resultMsg = '当前宿主未提供代理思考强度控制接口'
|
|
815
|
+
}
|
|
651
816
|
} else if (found.kind === 'auto_continue') {
|
|
652
817
|
if (deps && typeof deps.triggerAutoContinueNow === 'function') {
|
|
653
818
|
execRes = await deps.triggerAutoContinueNow(payload.sessionId || rootId, payload.trigger || 'turn/end:approved', { detail: payload.detail || '' })
|