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/CHANGELOG.md +63 -0
- package/README.en.md +281 -129
- package/README.md +286 -134
- package/api-routes.js +232 -2
- package/board-actions.js +1452 -2005
- package/board-handoff.js +709 -0
- package/board-schema.js +365 -7
- package/chief-approvals.js +6 -2
- package/chief-archives.js +1 -0
- package/chief-commands.js +1884 -3484
- package/chief-compaction.js +1 -0
- package/chief-executor.js +6 -1
- package/chief-models.js +112 -5
- package/chief-network.js +1098 -869
- package/chief-review.js +448 -51
- package/chief-state.js +1996 -2523
- package/chief-view.js +10 -4
- package/client.js +20318 -11591
- package/command-helpers.js +568 -0
- package/command-ops.js +1226 -0
- package/digest.js +29 -4
- package/idle-guard.js +233 -8
- package/index.js +6 -0
- package/locale/en.json +72 -1
- package/locale/zh.json +72 -1
- package/model-tools.js +46 -3
- package/network-harness.js +1047 -0
- package/package.json +13 -5
- package/runtime-hooks.js +316 -14
- package/safety/guard-walls.js +5 -0
- package/safety/invariant-guards.js +126 -6
- package/safety/tool-gate.js +2 -0
- package/state-media.js +173 -0
- package/state-schema.js +1505 -0
- package/subagent-runtime.js +85 -4
package/chief-compaction.js
CHANGED
|
@@ -546,6 +546,7 @@ export async function recommendAndSetCompactThreshold(deps, { sessionId = '' } =
|
|
|
546
546
|
|
|
547
547
|
const llmRes = await callChiefLlm(deps, {
|
|
548
548
|
sessionId: rootId,
|
|
549
|
+
reasonKind: 'recommend_compact',
|
|
549
550
|
system: COMPACT_ADVISOR_SYSTEM,
|
|
550
551
|
prompt: advisorPrompt,
|
|
551
552
|
maxTokens: 320,
|
package/chief-executor.js
CHANGED
|
@@ -521,6 +521,7 @@ export async function executeChiefToolCall(deps, call, execCtx = {}) {
|
|
|
521
521
|
const rootId = String(execCtx.rootId || '')
|
|
522
522
|
const board = execCtx.board || {}
|
|
523
523
|
const readOnlyToolsOnly = Boolean(execCtx.readOnlyToolsOnly)
|
|
524
|
+
const safetyGuardEnabled = execCtx.safetyGuardEnabled !== false
|
|
524
525
|
const approvedOnce = Boolean(execCtx.approvedOnce)
|
|
525
526
|
const capabilityScope = execCtx.capabilityScope || null
|
|
526
527
|
|
|
@@ -563,7 +564,7 @@ export async function executeChiefToolCall(deps, call, execCtx = {}) {
|
|
|
563
564
|
const resolvedAbsTarget = rawTargetPath ? path.resolve(cwd, rawTargetPath) : ''
|
|
564
565
|
const rawCommand = tool === 'run_command' ? String(args.command || args.cmd || '').trim() : ''
|
|
565
566
|
|
|
566
|
-
// Stage 3:
|
|
567
|
+
// Stage 3: 安全卫兵硬性规则(优先于任何权限等级、策略插件与逐项批准;受人类可控 safetyGuardEnabled 开关控制)
|
|
567
568
|
const guardRes = evaluateSafetyGuardHardRules({
|
|
568
569
|
tool,
|
|
569
570
|
opClass,
|
|
@@ -572,6 +573,7 @@ export async function executeChiefToolCall(deps, call, execCtx = {}) {
|
|
|
572
573
|
args,
|
|
573
574
|
cwd,
|
|
574
575
|
board,
|
|
576
|
+
safetyGuardEnabled,
|
|
575
577
|
})
|
|
576
578
|
if (guardRes.blocked) {
|
|
577
579
|
writeAudit({
|
|
@@ -1093,6 +1095,7 @@ export async function runChiefAgenticLoop(deps, {
|
|
|
1093
1095
|
board = null,
|
|
1094
1096
|
maxSteps = 4,
|
|
1095
1097
|
readOnlyToolsOnly = false,
|
|
1098
|
+
safetyGuardEnabled = true,
|
|
1096
1099
|
disableTools = false,
|
|
1097
1100
|
lazyDeferWorkspaceWritesToLead = false,
|
|
1098
1101
|
callLlmFn,
|
|
@@ -1182,6 +1185,7 @@ export async function runChiefAgenticLoop(deps, {
|
|
|
1182
1185
|
args: (c && c.args && typeof c.args === 'object') ? c.args : {},
|
|
1183
1186
|
cwd,
|
|
1184
1187
|
board,
|
|
1188
|
+
safetyGuardEnabled,
|
|
1185
1189
|
})
|
|
1186
1190
|
if (!preDef || preDef.decision !== 'deny') {
|
|
1187
1191
|
if (typeof recordAuditFn === 'function') {
|
|
@@ -1222,6 +1226,7 @@ export async function runChiefAgenticLoop(deps, {
|
|
|
1222
1226
|
policyPlugins,
|
|
1223
1227
|
board,
|
|
1224
1228
|
readOnlyToolsOnly,
|
|
1229
|
+
safetyGuardEnabled,
|
|
1225
1230
|
upsertArchiveFn,
|
|
1226
1231
|
compactNowFn,
|
|
1227
1232
|
queueApprovalFn,
|
package/chief-models.js
CHANGED
|
@@ -15,6 +15,8 @@ import {
|
|
|
15
15
|
resolveWorkspaceConfig,
|
|
16
16
|
loadChiefState,
|
|
17
17
|
mutateChiefState,
|
|
18
|
+
checkManagedBudgetBeforeCall,
|
|
19
|
+
recordChiefLlmUsage,
|
|
18
20
|
} from './chief-state.js'
|
|
19
21
|
|
|
20
22
|
let dshLlmModulePromise = null
|
|
@@ -480,6 +482,36 @@ function pickRouteFromItems(items, selectedRef, followRef, deps, sessionId, regi
|
|
|
480
482
|
return queue[0] || null
|
|
481
483
|
}
|
|
482
484
|
|
|
485
|
+
export function estimateTokensRough(text = '') {
|
|
486
|
+
const s = String(text || '')
|
|
487
|
+
if (!s) return 0
|
|
488
|
+
let cjk = 0
|
|
489
|
+
for (let i = 0; i < s.length; i += 1) {
|
|
490
|
+
const code = s.charCodeAt(i)
|
|
491
|
+
if (code >= 0x4E00 && code <= 0x9FFF) cjk += 1
|
|
492
|
+
}
|
|
493
|
+
const rest = s.length - cjk
|
|
494
|
+
return Math.max(1, Math.round(cjk * 0.72 + rest * 0.28))
|
|
495
|
+
}
|
|
496
|
+
|
|
497
|
+
function extractUsageMetrics(rawUsage, promptText = '', outputText = '') {
|
|
498
|
+
const u = rawUsage && typeof rawUsage === 'object' ? rawUsage : null
|
|
499
|
+
const inRaw = u ? Number(u.inputTokens ?? u.promptTokens ?? u.prompt_tokens ?? u.input_tokens ?? 0) : 0
|
|
500
|
+
const outRaw = u ? Number(u.outputTokens ?? u.completionTokens ?? u.completion_tokens ?? u.output_tokens ?? 0) : 0
|
|
501
|
+
if (Number.isFinite(inRaw) && Number.isFinite(outRaw) && (inRaw > 0 || outRaw > 0)) {
|
|
502
|
+
return {
|
|
503
|
+
inputTokens: Math.max(0, Math.round(inRaw)),
|
|
504
|
+
outputTokens: Math.max(0, Math.round(outRaw)),
|
|
505
|
+
estimated: false,
|
|
506
|
+
}
|
|
507
|
+
}
|
|
508
|
+
return {
|
|
509
|
+
inputTokens: estimateTokensRough(promptText),
|
|
510
|
+
outputTokens: estimateTokensRough(outputText),
|
|
511
|
+
estimated: true,
|
|
512
|
+
}
|
|
513
|
+
}
|
|
514
|
+
|
|
483
515
|
async function callOneRoute(llm, route, system, userText, maxTokens = SUMMARY_MAX_TOKENS, sessionId = '', imageBlocks = [], desiredEffort = 'medium') {
|
|
484
516
|
const caps = await resolveRouteCapabilities(llm, route.provider, route.model)
|
|
485
517
|
const modelDefaultMaxTokens = caps.defaultMaxTokens || 0
|
|
@@ -491,6 +523,7 @@ async function callOneRoute(llm, route, system, userText, maxTokens = SUMMARY_MA
|
|
|
491
523
|
const retryBudget = firstBudget < targetBudget ? targetBudget : SUMMARY_RETRY_MAX_TOKENS
|
|
492
524
|
const budgets = retryBudget > firstBudget ? [firstBudget, retryBudget] : [firstBudget]
|
|
493
525
|
const safeImages = Array.isArray(imageBlocks) ? imageBlocks : []
|
|
526
|
+
const promptBasis = `${system || ''}\n${userText || ''}`
|
|
494
527
|
|
|
495
528
|
const runOnce = async (budget) => {
|
|
496
529
|
const userContent = [...safeImages, { type: 'text', text: userText || '请查看附图并回答。' }]
|
|
@@ -508,8 +541,12 @@ async function callOneRoute(llm, route, system, userText, maxTokens = SUMMARY_MA
|
|
|
508
541
|
const dshLlm = await loadDshLlm()
|
|
509
542
|
if (dshLlm && typeof dshLlm.BlockAssembler === 'function') {
|
|
510
543
|
const assembler = new dshLlm.BlockAssembler()
|
|
544
|
+
let streamUsage = null
|
|
511
545
|
for await (const chunk of llm.stream(request)) {
|
|
512
546
|
assembler.push(chunk)
|
|
547
|
+
if (chunk && typeof chunk === 'object' && (chunk.usage || chunk.type === 'usage')) {
|
|
548
|
+
streamUsage = chunk.usage || chunk
|
|
549
|
+
}
|
|
513
550
|
}
|
|
514
551
|
const finish = assembler.finish
|
|
515
552
|
if (finish && (finish.kind === 'error' || finish.kind === 'aborted')) {
|
|
@@ -523,14 +560,19 @@ async function callOneRoute(llm, route, system, userText, maxTokens = SUMMARY_MA
|
|
|
523
560
|
.map(b => b.text)
|
|
524
561
|
.join('\n')
|
|
525
562
|
.trim()
|
|
526
|
-
|
|
563
|
+
const usage = extractUsageMetrics(assembler.usage || finish?.usage || finish?.reason?.usage || streamUsage, promptBasis, text)
|
|
564
|
+
return { text, finishKind: finish?.kind || '', modelRef: route.ref, thinkingEffort: effort || 'none', usage }
|
|
527
565
|
}
|
|
528
566
|
|
|
529
567
|
// Pure standalone fallback stream reader (for unit tests / lightweight hosts)
|
|
530
568
|
const parts = []
|
|
531
569
|
let finishKind = ''
|
|
570
|
+
let streamUsage = null
|
|
532
571
|
for await (const chunk of llm.stream(request)) {
|
|
533
572
|
if (!chunk || typeof chunk !== 'object') continue
|
|
573
|
+
if (chunk.usage || chunk.type === 'usage') {
|
|
574
|
+
streamUsage = chunk.usage || chunk
|
|
575
|
+
}
|
|
534
576
|
if (chunk.type === 'text-delta' || chunk.type === 'text_delta') {
|
|
535
577
|
if (typeof chunk.text === 'string') parts.push(chunk.text)
|
|
536
578
|
else if (typeof chunk.delta === 'string') parts.push(chunk.delta)
|
|
@@ -538,6 +580,7 @@ async function callOneRoute(llm, route, system, userText, maxTokens = SUMMARY_MA
|
|
|
538
580
|
else if (typeof chunk.content === 'string') parts.push(chunk.content)
|
|
539
581
|
else if (chunk.type === 'finish') {
|
|
540
582
|
finishKind = chunk.reason?.kind || chunk.finishReason || ''
|
|
583
|
+
if (chunk.reason?.usage) streamUsage = chunk.reason.usage
|
|
541
584
|
if (finishKind === 'error') {
|
|
542
585
|
const err = new Error(chunk.reason?.failure?.message || 'LLM stream error')
|
|
543
586
|
err.failure = chunk.reason?.failure
|
|
@@ -545,18 +588,26 @@ async function callOneRoute(llm, route, system, userText, maxTokens = SUMMARY_MA
|
|
|
545
588
|
}
|
|
546
589
|
}
|
|
547
590
|
}
|
|
548
|
-
|
|
591
|
+
const text = parts.join('').trim()
|
|
592
|
+
const usage = extractUsageMetrics(streamUsage, promptBasis, text)
|
|
593
|
+
return { text, finishKind, modelRef: route.ref, thinkingEffort: effort || 'none', usage }
|
|
549
594
|
}
|
|
550
595
|
|
|
551
|
-
let lastRes = {
|
|
596
|
+
let lastRes = {
|
|
597
|
+
text: '',
|
|
598
|
+
finishKind: '',
|
|
599
|
+
modelRef: route.ref,
|
|
600
|
+
thinkingEffort: effort || 'none',
|
|
601
|
+
usage: extractUsageMetrics(null, promptBasis, ''),
|
|
602
|
+
}
|
|
552
603
|
for (let i = 0; i < budgets.length; i += 1) {
|
|
553
604
|
const res = await runOnce(budgets[i])
|
|
554
605
|
lastRes = res
|
|
555
606
|
if (res.text && res.finishKind !== 'max-tokens') {
|
|
556
|
-
return { text: res.text, modelRef: res.modelRef, thinkingEffort: res.thinkingEffort }
|
|
607
|
+
return { text: res.text, modelRef: res.modelRef, thinkingEffort: res.thinkingEffort, usage: res.usage }
|
|
557
608
|
}
|
|
558
609
|
}
|
|
559
|
-
return { text: lastRes.text, modelRef: lastRes.modelRef, thinkingEffort: lastRes.thinkingEffort }
|
|
610
|
+
return { text: lastRes.text, modelRef: lastRes.modelRef, thinkingEffort: lastRes.thinkingEffort, usage: lastRes.usage }
|
|
560
611
|
}
|
|
561
612
|
|
|
562
613
|
async function filterVisionRoutes(llm, queue) {
|
|
@@ -609,6 +660,20 @@ export async function callChiefLlm(deps, {
|
|
|
609
660
|
})
|
|
610
661
|
: cfgEffort
|
|
611
662
|
|
|
663
|
+
const budgetCheck = checkManagedBudgetBeforeCall(rootSid, reasonKind)
|
|
664
|
+
if (budgetCheck && !budgetCheck.allowed) {
|
|
665
|
+
return {
|
|
666
|
+
ok: false,
|
|
667
|
+
budgetExceeded: true,
|
|
668
|
+
budgetTripped: true,
|
|
669
|
+
error: budgetCheck.reason || '已触发托管硬预算熔断',
|
|
670
|
+
thinkingEffort: desiredEffort,
|
|
671
|
+
thinkingEffortMode,
|
|
672
|
+
}
|
|
673
|
+
}
|
|
674
|
+
|
|
675
|
+
const promptBasis = `${system || ''}\n${prompt || ''}`
|
|
676
|
+
|
|
612
677
|
if (deps && typeof deps.callLlmOverride === 'function') {
|
|
613
678
|
const overrideRes = await deps.callLlmOverride({
|
|
614
679
|
sessionId,
|
|
@@ -622,9 +687,20 @@ export async function callChiefLlm(deps, {
|
|
|
622
687
|
thinkingEffortMode,
|
|
623
688
|
})
|
|
624
689
|
if (overrideRes && typeof overrideRes === 'object') {
|
|
690
|
+
const usage = extractUsageMetrics(overrideRes.usage, promptBasis, overrideRes.text || '')
|
|
691
|
+
const ledgerRec = recordChiefLlmUsage(rootSid, {
|
|
692
|
+
reasonKind,
|
|
693
|
+
modelRef: overrideRes.modelRef || 'override',
|
|
694
|
+
inputTokens: usage.inputTokens,
|
|
695
|
+
outputTokens: usage.outputTokens,
|
|
696
|
+
estimated: usage.estimated,
|
|
697
|
+
ok: overrideRes.ok !== false,
|
|
698
|
+
})
|
|
625
699
|
return {
|
|
626
700
|
thinkingEffort: overrideRes.thinkingEffort || desiredEffort,
|
|
627
701
|
thinkingEffortMode: overrideRes.thinkingEffortMode || thinkingEffortMode,
|
|
702
|
+
usage,
|
|
703
|
+
...(ledgerRec && ledgerRec.tripped ? { budgetTripped: true } : {}),
|
|
628
704
|
...overrideRes,
|
|
629
705
|
}
|
|
630
706
|
}
|
|
@@ -669,6 +745,15 @@ export async function callChiefLlm(deps, {
|
|
|
669
745
|
try {
|
|
670
746
|
const res = await callOneRoute(llm, route, system, prompt, maxTokens, sessionId, sendImages, desiredEffort)
|
|
671
747
|
if (res.text) {
|
|
748
|
+
const usage = res.usage || extractUsageMetrics(null, promptBasis, res.text)
|
|
749
|
+
const ledgerRec = recordChiefLlmUsage(rootSid, {
|
|
750
|
+
reasonKind,
|
|
751
|
+
modelRef: res.modelRef,
|
|
752
|
+
inputTokens: usage.inputTokens,
|
|
753
|
+
outputTokens: usage.outputTokens,
|
|
754
|
+
estimated: usage.estimated,
|
|
755
|
+
ok: true,
|
|
756
|
+
})
|
|
672
757
|
return {
|
|
673
758
|
ok: true,
|
|
674
759
|
text: res.text,
|
|
@@ -676,6 +761,8 @@ export async function callChiefLlm(deps, {
|
|
|
676
761
|
degradedToTextOnly,
|
|
677
762
|
thinkingEffort: res.thinkingEffort || desiredEffort,
|
|
678
763
|
thinkingEffortMode,
|
|
764
|
+
usage,
|
|
765
|
+
...(ledgerRec && ledgerRec.tripped ? { budgetTripped: true } : {}),
|
|
679
766
|
}
|
|
680
767
|
}
|
|
681
768
|
lastError = `${route.ref} 返回内容为空`
|
|
@@ -686,6 +773,15 @@ export async function callChiefLlm(deps, {
|
|
|
686
773
|
try {
|
|
687
774
|
const textRes = await callOneRoute(llm, route, system, prompt, maxTokens, sessionId, [], desiredEffort)
|
|
688
775
|
if (textRes.text) {
|
|
776
|
+
const usage = textRes.usage || extractUsageMetrics(null, promptBasis, textRes.text)
|
|
777
|
+
const ledgerRec = recordChiefLlmUsage(rootSid, {
|
|
778
|
+
reasonKind,
|
|
779
|
+
modelRef: textRes.modelRef,
|
|
780
|
+
inputTokens: usage.inputTokens,
|
|
781
|
+
outputTokens: usage.outputTokens,
|
|
782
|
+
estimated: usage.estimated,
|
|
783
|
+
ok: true,
|
|
784
|
+
})
|
|
689
785
|
return {
|
|
690
786
|
ok: true,
|
|
691
787
|
text: textRes.text,
|
|
@@ -693,6 +789,8 @@ export async function callChiefLlm(deps, {
|
|
|
693
789
|
degradedToTextOnly: true,
|
|
694
790
|
thinkingEffort: textRes.thinkingEffort || desiredEffort,
|
|
695
791
|
thinkingEffortMode,
|
|
792
|
+
usage,
|
|
793
|
+
...(ledgerRec && ledgerRec.tripped ? { budgetTripped: true } : {}),
|
|
696
794
|
}
|
|
697
795
|
}
|
|
698
796
|
} catch {}
|
|
@@ -700,6 +798,15 @@ export async function callChiefLlm(deps, {
|
|
|
700
798
|
markRouteFailure(route, err)
|
|
701
799
|
}
|
|
702
800
|
}
|
|
801
|
+
const failUsage = extractUsageMetrics(null, promptBasis, '')
|
|
802
|
+
recordChiefLlmUsage(rootSid, {
|
|
803
|
+
reasonKind,
|
|
804
|
+
modelRef: queue[0] ? queue[0].ref : '',
|
|
805
|
+
inputTokens: failUsage.inputTokens,
|
|
806
|
+
outputTokens: 0,
|
|
807
|
+
estimated: true,
|
|
808
|
+
ok: false,
|
|
809
|
+
})
|
|
703
810
|
return { ok: false, error: lastError, degradedToTextOnly, thinkingEffort: desiredEffort, thinkingEffortMode }
|
|
704
811
|
}
|
|
705
812
|
|