dsh-chief 1.3.0 → 1.4.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 +42 -12
- package/README.en.md +143 -116
- package/README.md +134 -142
- package/api-routes.js +19 -2
- package/board-actions.js +72 -1
- package/board-schema.js +1 -1
- package/chief-approvals.js +241 -81
- package/chief-archives.js +35 -0
- package/chief-commands.js +615 -108
- package/chief-executor.js +279 -555
- package/chief-network.js +157 -8
- package/chief-review.js +33 -1
- package/chief-state.js +497 -81
- package/chief-view.js +21 -6
- package/client.js +1953 -506
- package/model-tools.js +78 -2
- package/package.json +35 -24
- package/runtime-hooks.js +108 -2
- package/safety/audit-log.js +95 -0
- package/safety/command-analyzer.js +339 -0
- package/safety/guard-walls.js +993 -0
- package/safety/index.js +78 -0
- package/safety/invariant-guards.js +280 -0
- package/safety/path-guard.js +227 -0
- package/safety/policy-engine.js +387 -0
- package/safety/policy-plugin.js +391 -0
- package/safety/tool-gate.js +278 -0
- package/subagent-runtime.js +9 -2
package/chief-approvals.js
CHANGED
|
@@ -11,6 +11,10 @@ import {
|
|
|
11
11
|
permissionRank,
|
|
12
12
|
evaluateSafetyGuardHardRules,
|
|
13
13
|
executeChiefToolCall,
|
|
14
|
+
HUMAN_BOUNDS_CONFIRM_TOKEN,
|
|
15
|
+
buildCapabilityScope,
|
|
16
|
+
verifyCapabilityScope,
|
|
17
|
+
mutateGuardWalls,
|
|
14
18
|
} from './chief-executor.js'
|
|
15
19
|
import {
|
|
16
20
|
CHAT_CAP,
|
|
@@ -21,7 +25,9 @@ import {
|
|
|
21
25
|
cleanMultiline,
|
|
22
26
|
hasValidChiefRiskAck,
|
|
23
27
|
computeApprovalPayloadHash,
|
|
24
|
-
|
|
28
|
+
upsertTaskLedgerOnWs,
|
|
29
|
+
markTaskResumingActive,
|
|
30
|
+
unmarkTaskResumingActive,
|
|
25
31
|
pushAuditToWs,
|
|
26
32
|
normalizeMail,
|
|
27
33
|
resolveWorkspaceConfig,
|
|
@@ -38,7 +44,7 @@ import { buildChiefArchiveUpsertFn } from './chief-archives.js'
|
|
|
38
44
|
import { buildChiefCompactFn, recordSmartCompactionChatReceipt } from './chief-compaction.js'
|
|
39
45
|
import { askOrCommandChief } from './chief-commands.js'
|
|
40
46
|
|
|
41
|
-
export async function handleSuggestionDecision(deps, { sessionId, suggestionId, decision, editedText, boundField, pinGlobal }) {
|
|
47
|
+
export async function handleSuggestionDecision(deps, { sessionId, suggestionId, decision, editedText, boundField, pinGlobal, _suppressChatReceipt = false }) {
|
|
42
48
|
const rootId = deps.resolveRootSessionId(sessionId) || String(sessionId || 'global')
|
|
43
49
|
let found = null
|
|
44
50
|
mutateChiefState((state) => {
|
|
@@ -76,6 +82,8 @@ export async function handleSuggestionDecision(deps, { sessionId, suggestionId,
|
|
|
76
82
|
pinned: shouldPin,
|
|
77
83
|
session: rootId === 'global' ? '' : rootId,
|
|
78
84
|
by: 'DSH 参谋长',
|
|
85
|
+
_callerOrigin: 'human_approval',
|
|
86
|
+
_humanConfirmToken: HUMAN_BOUNDS_CONFIRM_TOKEN,
|
|
79
87
|
})
|
|
80
88
|
: deps.runAction({
|
|
81
89
|
action: 'bounds.add',
|
|
@@ -84,6 +92,8 @@ export async function handleSuggestionDecision(deps, { sessionId, suggestionId,
|
|
|
84
92
|
pinned: shouldPin,
|
|
85
93
|
session: rootId === 'global' ? '' : rootId,
|
|
86
94
|
by: 'DSH 参谋长',
|
|
95
|
+
_callerOrigin: 'human_approval',
|
|
96
|
+
_humanConfirmToken: HUMAN_BOUNDS_CONFIRM_TOKEN,
|
|
87
97
|
}))
|
|
88
98
|
: { ok: false, error: 'runAction unavailable' }
|
|
89
99
|
if (!writeRes || !writeRes.ok) {
|
|
@@ -123,6 +133,59 @@ export async function handleSuggestionDecision(deps, { sessionId, suggestionId,
|
|
|
123
133
|
}
|
|
124
134
|
}
|
|
125
135
|
|
|
136
|
+
if (decision === 'approve' && found.kind === 'guard_wall') {
|
|
137
|
+
const cwdNow = resolveSessionWorkspaceCwd(deps, rootId)
|
|
138
|
+
const spec = found.wallSpec && typeof found.wallSpec === 'object' ? found.wallSpec : {
|
|
139
|
+
title: String(found.title || '参谋长手搓物理墙').replace(/^🧱\s*驻防物理墙[::]\s*/, ''),
|
|
140
|
+
scope: found.pinGlobal === false ? 'workspace' : 'global',
|
|
141
|
+
ruleType: 'forbid_path',
|
|
142
|
+
patterns: String(found.draftText || '').split(/[,,;\n]+/).map(s => s.trim()).filter(Boolean),
|
|
143
|
+
reason: found.reason || '',
|
|
144
|
+
source: 'chief_crafted',
|
|
145
|
+
}
|
|
146
|
+
const wallRes = mutateGuardWalls({
|
|
147
|
+
op: 'save',
|
|
148
|
+
wall: { ...spec, source: 'chief_crafted' },
|
|
149
|
+
cwd: cwdNow,
|
|
150
|
+
callerOrigin: 'human_approval',
|
|
151
|
+
humanConfirmToken: HUMAN_BOUNDS_CONFIRM_TOKEN,
|
|
152
|
+
})
|
|
153
|
+
if (!wallRes || !wallRes.ok) {
|
|
154
|
+
return { ok: false, error: (wallRes && wallRes.error) || '驻防物理墙落锁失败' }
|
|
155
|
+
}
|
|
156
|
+
mutateChiefState((state) => {
|
|
157
|
+
const ws = ensureWorkspace(state, rootId)
|
|
158
|
+
if (!_suppressChatReceipt) {
|
|
159
|
+
ws.chat.push({
|
|
160
|
+
id: nextId('chat'),
|
|
161
|
+
role: 'chief',
|
|
162
|
+
kind: 'receipt',
|
|
163
|
+
text: `- 【🧱 物理驻墙已确认落锁】${wallRes.wall.title}\n- 【拦截范围】${wallRes.wall.scope === 'workspace' ? '仅绑定当前工程' : '🌐 全局随身常驻'} | 规则:\`${wallRes.wall.patterns.join(', ')}\``,
|
|
164
|
+
actions: ['确认驻防物理墙'],
|
|
165
|
+
at: Date.now(),
|
|
166
|
+
})
|
|
167
|
+
if (ws.chat.length > CHAT_CAP) ws.chat = ws.chat.slice(-CHAT_CAP)
|
|
168
|
+
}
|
|
169
|
+
pushAuditToWs(ws, {
|
|
170
|
+
mode: resolveWorkspaceConfig(state, rootId).permissionMode,
|
|
171
|
+
stage: 'guard_wall',
|
|
172
|
+
rule: 'guard_wall_approved',
|
|
173
|
+
decision: 'executed',
|
|
174
|
+
operation: '落锁物理驻墙',
|
|
175
|
+
target: wallRes.wall.title,
|
|
176
|
+
reason: wallRes.wall.patterns.join(', '),
|
|
177
|
+
outcome: 'ok',
|
|
178
|
+
by: 'operator',
|
|
179
|
+
}, rootId)
|
|
180
|
+
})
|
|
181
|
+
return {
|
|
182
|
+
ok: true,
|
|
183
|
+
wallApplied: wallRes.wall,
|
|
184
|
+
guardWalls: wallRes.walls,
|
|
185
|
+
view: await getChiefView(deps, rootId, { autoSummarize: false }),
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
|
|
126
189
|
if (decision === 'approve' && found.kind === 'goal_approval') {
|
|
127
190
|
const res = deps.runAction
|
|
128
191
|
? deps.runAction({ action: 'goal.approve', session: rootId, by: 'user' })
|
|
@@ -229,7 +292,7 @@ export async function executeBatchSuggestions(deps, {
|
|
|
229
292
|
continue
|
|
230
293
|
}
|
|
231
294
|
|
|
232
|
-
if (sug.kind !== 'bound' && sug.kind !== 'goal_approval') {
|
|
295
|
+
if (sug.kind !== 'bound' && sug.kind !== 'goal_approval' && sug.kind !== 'guard_wall') {
|
|
233
296
|
const cmdSample = `${sug.draftText || ''}\n${sug.title || ''}`.trim()
|
|
234
297
|
const guardRes = evaluateSafetyGuardHardRules({
|
|
235
298
|
tool: 'relay',
|
|
@@ -265,12 +328,18 @@ export async function executeBatchSuggestions(deps, {
|
|
|
265
328
|
suggestionId: sug.id,
|
|
266
329
|
decision: 'approve',
|
|
267
330
|
pinGlobal: sug.kind === 'bound' && typeof pinGlobalDefault === 'boolean' ? pinGlobalDefault : undefined,
|
|
331
|
+
_suppressChatReceipt: true,
|
|
268
332
|
})
|
|
269
333
|
|
|
270
334
|
if (res && res.ok) {
|
|
271
335
|
executed.push({ id: sug.id, kind: sug.kind, title: sug.title, target: sug.targetLabel || sug.target })
|
|
272
336
|
if (res.boundApplied) {
|
|
273
337
|
summaries.push(`✅ 已应用边界「${res.boundApplied.label}」:${clamp(res.boundApplied.item, 44)}`)
|
|
338
|
+
} else if (res.wallApplied) {
|
|
339
|
+
const w = res.wallApplied
|
|
340
|
+
const scopeLabel = w.scope === 'workspace' ? '仅本工程' : '🌐全局随身'
|
|
341
|
+
const patPreview = Array.isArray(w.patterns) ? clamp(w.patterns.join(', '), 60) : ''
|
|
342
|
+
summaries.push(`🧱 已落锁物理驻墙「${clamp(w.title, 36)}」(${scopeLabel}${patPreview ? ` · \`${patPreview}\`` : ''})`)
|
|
274
343
|
} else if (res.goalApproved) {
|
|
275
344
|
summaries.push('✅ 已批准并结项当前核心目标')
|
|
276
345
|
} else if (res.delivery && res.delivery.target) {
|
|
@@ -337,6 +406,7 @@ export async function handleApprovalDecision(deps, {
|
|
|
337
406
|
const rootId = (deps && typeof deps.resolveRootSessionId === 'function' ? deps.resolveRootSessionId(sessionId) : '') || String(sessionId || 'global')
|
|
338
407
|
let found = null
|
|
339
408
|
let currentMode = DEFAULT_CHIEF_PERMISSION_MODE
|
|
409
|
+
let effPolicyPlugins = []
|
|
340
410
|
let existingNonPending = false
|
|
341
411
|
let hashMismatch = false
|
|
342
412
|
|
|
@@ -345,12 +415,14 @@ export async function handleApprovalDecision(deps, {
|
|
|
345
415
|
const ackOk = hasValidChiefRiskAck(ws)
|
|
346
416
|
const eff = resolveWorkspaceConfig(state, rootId)
|
|
347
417
|
currentMode = normalizePermissionMode(eff.permissionMode, { requireAck: true, hasChiefRiskAck: ackOk })
|
|
418
|
+
effPolicyPlugins = Array.isArray(eff.policyPlugins) ? eff.policyPlugins : []
|
|
348
419
|
const item = (ws.pendingApprovals || []).find(a => a && a.id === approvalId && a.status === 'pending')
|
|
349
420
|
if (!item) {
|
|
350
421
|
existingNonPending = (ws.pendingApprovals || []).some(a => a && a.id === approvalId)
|
|
351
422
|
pushAuditToWs(ws, {
|
|
352
423
|
mode: currentMode,
|
|
353
424
|
stage: 'permission_gate',
|
|
425
|
+
rule: existingNonPending ? 'replayed_approval' : 'invalid_approval_id',
|
|
354
426
|
decision: existingNonPending ? 'replayed_approval' : 'invalid_approval_id',
|
|
355
427
|
operation: '伪造或重放审批拦截',
|
|
356
428
|
target: clamp(approvalId || '(empty)', 100),
|
|
@@ -361,6 +433,7 @@ export async function handleApprovalDecision(deps, {
|
|
|
361
433
|
return
|
|
362
434
|
}
|
|
363
435
|
|
|
436
|
+
const linkedTaskId = item.taskId || item.resumeTaskId || ''
|
|
364
437
|
const expectedHash = computeApprovalPayloadHash(item.kind, item.payload || {})
|
|
365
438
|
if (decision === 'approve' && item.payloadHash && item.payloadHash !== expectedHash) {
|
|
366
439
|
hashMismatch = true
|
|
@@ -369,16 +442,20 @@ export async function handleApprovalDecision(deps, {
|
|
|
369
442
|
item.resolvedAt = Date.now()
|
|
370
443
|
item.resolvedBy = 'safety_guard'
|
|
371
444
|
item.resultMessage = '审批载荷哈希校验失败(疑似载荷被篡改)'
|
|
372
|
-
|
|
373
|
-
|
|
445
|
+
const ledgerList = Array.isArray(ws.taskLedger) ? ws.taskLedger : (Array.isArray(ws.resumeTasks) ? ws.resumeTasks : [])
|
|
446
|
+
if (linkedTaskId) {
|
|
447
|
+
const linkedTask = ledgerList.find(t => t && (t.taskId === linkedTaskId || t.id === linkedTaskId))
|
|
374
448
|
if (linkedTask) {
|
|
375
449
|
linkedTask.status = 'failed'
|
|
450
|
+
linkedTask.updatedAt = Date.now()
|
|
376
451
|
linkedTask.resolvedAt = Date.now()
|
|
452
|
+
linkedTask.lastReceipt = '审批载荷哈希校验失败,事务中止'
|
|
377
453
|
}
|
|
378
454
|
}
|
|
379
455
|
pushAuditToWs(ws, {
|
|
380
456
|
mode: currentMode,
|
|
381
457
|
stage: 'safety_guard',
|
|
458
|
+
rule: 'tampered_approval_payload',
|
|
382
459
|
decision: 'tampered_approval_payload',
|
|
383
460
|
operation: item.operationLabel,
|
|
384
461
|
target: item.targetLabel,
|
|
@@ -404,16 +481,20 @@ export async function handleApprovalDecision(deps, {
|
|
|
404
481
|
item.resolvedAt = Date.now()
|
|
405
482
|
item.resolvedBy = 'operator'
|
|
406
483
|
item.resultMessage = '用户拒绝执行本次操作'
|
|
407
|
-
|
|
408
|
-
|
|
484
|
+
const ledgerList = Array.isArray(ws.taskLedger) ? ws.taskLedger : (Array.isArray(ws.resumeTasks) ? ws.resumeTasks : [])
|
|
485
|
+
if (linkedTaskId) {
|
|
486
|
+
const linkedTask = ledgerList.find(t => t && (t.taskId === linkedTaskId || t.id === linkedTaskId))
|
|
409
487
|
if (linkedTask) {
|
|
410
488
|
linkedTask.status = 'cancelled'
|
|
489
|
+
linkedTask.updatedAt = Date.now()
|
|
411
490
|
linkedTask.resolvedAt = Date.now()
|
|
491
|
+
linkedTask.lastReceipt = '用户拒绝本次操作授权'
|
|
412
492
|
}
|
|
413
493
|
}
|
|
414
494
|
pushAuditToWs(ws, {
|
|
415
495
|
mode: currentMode,
|
|
416
496
|
stage: 'permission_gate',
|
|
497
|
+
rule: 'rejected_by_user',
|
|
417
498
|
decision: 'rejected_by_user',
|
|
418
499
|
operation: item.operationLabel,
|
|
419
500
|
target: item.targetLabel,
|
|
@@ -437,6 +518,7 @@ export async function handleApprovalDecision(deps, {
|
|
|
437
518
|
pushAuditToWs(ws, {
|
|
438
519
|
mode: currentMode,
|
|
439
520
|
stage: 'permission_gate',
|
|
521
|
+
rule: 'approved_by_user',
|
|
440
522
|
decision: 'approved_by_user',
|
|
441
523
|
operation: item.operationLabel,
|
|
442
524
|
target: item.targetLabel,
|
|
@@ -462,6 +544,13 @@ export async function handleApprovalDecision(deps, {
|
|
|
462
544
|
? deps.boardForSession(deps.boardFileFor(rootId))
|
|
463
545
|
: null
|
|
464
546
|
const payload = found.payload || {}
|
|
547
|
+
const resolvedScope = found.capabilityScope || buildCapabilityScope({
|
|
548
|
+
kind: found.kind,
|
|
549
|
+
tool: payload.tool || found.kind,
|
|
550
|
+
args: payload.args || {},
|
|
551
|
+
cwd: payload.cwd || resolveSessionWorkspaceCwd(deps, rootId),
|
|
552
|
+
target: payload.target || '',
|
|
553
|
+
})
|
|
465
554
|
let execOk = false
|
|
466
555
|
let execRes = null
|
|
467
556
|
let resultMsg = ''
|
|
@@ -474,6 +563,8 @@ export async function handleApprovalDecision(deps, {
|
|
|
474
563
|
cwd: payload.cwd || resolveSessionWorkspaceCwd(deps, rootId),
|
|
475
564
|
permissionMode: currentMode === 'chief' ? 'chief' : 'strategist',
|
|
476
565
|
hasChiefRiskAck: currentMode === 'chief',
|
|
566
|
+
policyPlugins: effPolicyPlugins,
|
|
567
|
+
capabilityScope: resolvedScope,
|
|
477
568
|
approvedOnce: true,
|
|
478
569
|
board,
|
|
479
570
|
upsertArchiveFn: buildChiefArchiveUpsertFn(deps, rootId),
|
|
@@ -486,45 +577,55 @@ export async function handleApprovalDecision(deps, {
|
|
|
486
577
|
? `已执行 ${found.operationLabel}(${found.targetLabel})`
|
|
487
578
|
: (execRes && execRes.error) || '执行未成功'
|
|
488
579
|
} else if (found.kind === 'relay') {
|
|
489
|
-
const
|
|
490
|
-
if (
|
|
580
|
+
const scopeCheck = verifyCapabilityScope(resolvedScope, { kind: 'relay', tool: 'relay', target: payload.target || '' })
|
|
581
|
+
if (!scopeCheck.ok) {
|
|
491
582
|
execOk = false
|
|
492
|
-
execRes = { ok: false,
|
|
493
|
-
resultMsg =
|
|
494
|
-
appendChiefAuditLog(rootId, { mode: currentMode, stage: '
|
|
583
|
+
execRes = { ok: false, deniedByScopeMismatch: true, error: scopeCheck.error }
|
|
584
|
+
resultMsg = scopeCheck.error
|
|
585
|
+
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' })
|
|
495
586
|
} else {
|
|
496
|
-
const
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
587
|
+
const guardRes = evaluateSafetyGuardHardRules({ tool: 'relay', opClass: 'workflow', command: payload.text || '', board })
|
|
588
|
+
if (guardRes.blocked) {
|
|
589
|
+
execOk = false
|
|
590
|
+
execRes = { ok: false, blockedBySafetyGuard: true, error: guardRes.reason }
|
|
591
|
+
resultMsg = guardRes.reason
|
|
592
|
+
appendChiefAuditLog(rootId, { mode: currentMode, stage: 'safety_guard', rule: guardRes.rule || 'hard_guard', decision: 'blocked', operation: found.operationLabel, target: found.targetLabel, reason: guardRes.reason, outcome: 'blocked', by: 'operator' })
|
|
593
|
+
} else {
|
|
594
|
+
const deliv = await deliverCrossMessage(deps, {
|
|
595
|
+
fromSession: rootId,
|
|
596
|
+
fromLabel: payload.fromLabel || 'DSH 参谋 (逐项授权)',
|
|
597
|
+
target: payload.target || 'lead',
|
|
598
|
+
subject: payload.subject || '',
|
|
599
|
+
text: payload.text || '',
|
|
600
|
+
mode: payload.mode || 'steer',
|
|
601
|
+
envelopeKind: payload.envelopeKind || 'execution_request',
|
|
602
|
+
taskId: payload.taskId || '',
|
|
603
|
+
imageBlocks: Array.isArray(payload.imageBlocks) ? payload.imageBlocks : [],
|
|
604
|
+
savedImagePaths: Array.isArray(payload.savedImagePaths) ? payload.savedImagePaths : [],
|
|
605
|
+
})
|
|
606
|
+
const isLeadTarget = /^(?:lead|main|主对话)$/i.test(String(payload.target || 'lead').trim())
|
|
607
|
+
if (isLeadTarget && (!deliv.ok || (payload.isBlankContext && !deliv.deliveredPhysical)) && deps && typeof deps.stagePendingTransfer === 'function') {
|
|
608
|
+
const staged = deps.stagePendingTransfer(
|
|
609
|
+
`[DSH 参谋预派发指令${payload.subject ? ' · ' + payload.subject : ''}]\n${payload.text || ''}`,
|
|
610
|
+
rootId,
|
|
611
|
+
{ by: 'chief', subject: payload.subject || '' },
|
|
612
|
+
)
|
|
613
|
+
if (staged) {
|
|
614
|
+
execOk = true
|
|
615
|
+
execRes = { ...deliv, ok: true, stagedPending: true, target: '主对话(待领)' }
|
|
616
|
+
resultMsg = '已暂存主对话待领交接指令'
|
|
617
|
+
} else {
|
|
618
|
+
execOk = Boolean(deliv && deliv.ok)
|
|
619
|
+
execRes = deliv
|
|
620
|
+
resultMsg = execOk ? `已投递给 ${deliv.target || found.targetLabel}` : ((deliv && deliv.error) || '投递失败')
|
|
621
|
+
}
|
|
517
622
|
} else {
|
|
518
623
|
execOk = Boolean(deliv && deliv.ok)
|
|
519
624
|
execRes = deliv
|
|
520
625
|
resultMsg = execOk ? `已投递给 ${deliv.target || found.targetLabel}` : ((deliv && deliv.error) || '投递失败')
|
|
521
626
|
}
|
|
522
|
-
|
|
523
|
-
execOk = Boolean(deliv && deliv.ok)
|
|
524
|
-
execRes = deliv
|
|
525
|
-
resultMsg = execOk ? `已投递给 ${deliv.target || found.targetLabel}` : ((deliv && deliv.error) || '投递失败')
|
|
627
|
+
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' })
|
|
526
628
|
}
|
|
527
|
-
appendChiefAuditLog(rootId, { mode: currentMode, stage: 'executor', decision: 'executed_approved', operation: found.operationLabel, target: found.targetLabel, reason: clamp(resultMsg, 120), outcome: execOk ? 'ok' : 'error', by: 'operator' })
|
|
528
629
|
}
|
|
529
630
|
} else if (found.kind === 'compact') {
|
|
530
631
|
const guardRes = evaluateSafetyGuardHardRules({ tool: 'session_compact', opClass: 'workflow', board })
|
|
@@ -545,7 +646,7 @@ export async function handleApprovalDecision(deps, {
|
|
|
545
646
|
resultMsg = execOk
|
|
546
647
|
? (execRes.noop ? '已抢救细节入本会话档案柜(当前历史免压)' : '已抢救细节并完成主对话上下文静默压缩')
|
|
547
648
|
: ((execRes && execRes.error) || '压缩未完成')
|
|
548
|
-
appendChiefAuditLog(rootId, { mode: currentMode, stage: 'executor', decision: 'executed_approved', operation: found.operationLabel, target: found.targetLabel, reason: clamp(resultMsg, 120), outcome: execOk ? 'ok' : 'error', by: 'operator' })
|
|
649
|
+
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' })
|
|
549
650
|
}
|
|
550
651
|
} else if (found.kind === 'auto_continue') {
|
|
551
652
|
if (deps && typeof deps.triggerAutoContinueNow === 'function') {
|
|
@@ -556,7 +657,7 @@ export async function handleApprovalDecision(deps, {
|
|
|
556
657
|
execOk = false
|
|
557
658
|
resultMsg = '当前宿主未提供续跑回调接口'
|
|
558
659
|
}
|
|
559
|
-
appendChiefAuditLog(rootId, { mode: currentMode, stage: 'executor', decision: 'executed_approved', operation: found.operationLabel, target: found.targetLabel, reason: clamp(resultMsg, 120), outcome: execOk ? 'ok' : 'error', by: 'operator' })
|
|
660
|
+
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' })
|
|
560
661
|
}
|
|
561
662
|
} catch (err) {
|
|
562
663
|
execOk = false
|
|
@@ -566,6 +667,7 @@ export async function handleApprovalDecision(deps, {
|
|
|
566
667
|
|
|
567
668
|
let updatedApproval = { ...found, status: execOk ? 'executed' : 'failed', resultMessage: resultMsg }
|
|
568
669
|
let resumeTaskToRun = null
|
|
670
|
+
const linkedTaskId = found.taskId || found.resumeTaskId || ''
|
|
569
671
|
mutateChiefState((state) => {
|
|
570
672
|
const ws = ensureWorkspace(state, rootId)
|
|
571
673
|
const item = (ws.pendingApprovals || []).find(a => a && a.id === approvalId)
|
|
@@ -578,7 +680,7 @@ export async function handleApprovalDecision(deps, {
|
|
|
578
680
|
id: nextId('chat'),
|
|
579
681
|
role: 'chief',
|
|
580
682
|
kind: 'receipt',
|
|
581
|
-
...(
|
|
683
|
+
...(linkedTaskId ? { resumeTaskId: linkedTaskId } : {}),
|
|
582
684
|
text: execOk
|
|
583
685
|
? `- 【✅ 已允许并执行】${found.operationLabel} · \`${found.targetLabel}\`\n 结果:${clamp(resultMsg, 240)}`
|
|
584
686
|
: `- 【⚠️ 授权操作未完成】${found.operationLabel} · \`${found.targetLabel}\`\n 说明:${clamp(resultMsg, 240)}`,
|
|
@@ -587,31 +689,39 @@ export async function handleApprovalDecision(deps, {
|
|
|
587
689
|
})
|
|
588
690
|
if (ws.chat.length > CHAT_CAP) ws.chat = ws.chat.slice(-CHAT_CAP)
|
|
589
691
|
|
|
590
|
-
|
|
591
|
-
|
|
692
|
+
const ledgerList = Array.isArray(ws.taskLedger) ? ws.taskLedger : (Array.isArray(ws.resumeTasks) ? ws.resumeTasks : [])
|
|
693
|
+
if (linkedTaskId) {
|
|
694
|
+
const task = ledgerList.find(t => t && (t.taskId === linkedTaskId || t.id === linkedTaskId))
|
|
592
695
|
if (task) {
|
|
593
696
|
const doneSummary = `${found.operationLabel} (${found.targetLabel}): ${clamp(resultMsg, 140)}`
|
|
594
|
-
const updatedTask =
|
|
595
|
-
|
|
697
|
+
const updatedTask = upsertTaskLedgerOnWs(ws, {
|
|
698
|
+
taskId: task.taskId || task.id,
|
|
699
|
+
id: task.taskId || task.id,
|
|
596
700
|
appendCompletedAction: doneSummary,
|
|
701
|
+
lastReceipt: clamp(resultMsg, 240),
|
|
597
702
|
})
|
|
598
703
|
const siblingPending = (ws.pendingApprovals || []).some(
|
|
599
|
-
a => a && a.id !== approvalId && a.resumeTaskId === task.id && a.status === 'pending',
|
|
704
|
+
a => a && a.id !== approvalId && (a.taskId === (task.taskId || task.id) || a.resumeTaskId === (task.taskId || task.id)) && a.status === 'pending',
|
|
600
705
|
)
|
|
601
706
|
if (!execOk) {
|
|
602
707
|
if (updatedTask) {
|
|
603
708
|
updatedTask.status = 'failed'
|
|
709
|
+
updatedTask.updatedAt = Date.now()
|
|
604
710
|
updatedTask.resolvedAt = Date.now()
|
|
605
711
|
}
|
|
606
712
|
} else if (!siblingPending && updatedTask) {
|
|
607
713
|
if (updatedTask.expiresAt && Date.now() > updatedTask.expiresAt) {
|
|
608
714
|
updatedTask.status = 'expired'
|
|
715
|
+
updatedTask.updatedAt = Date.now()
|
|
609
716
|
updatedTask.resolvedAt = Date.now()
|
|
610
|
-
} else if (found.kind === 'tool' && (updatedTask.resumeDepth || 0) < 2 && updatedTask.status !== 'resuming') {
|
|
717
|
+
} else if (found.kind === 'tool' && (updatedTask.retryCount || updatedTask.resumeDepth || 0) < 2 && updatedTask.status !== 'resuming') {
|
|
718
|
+
markTaskResumingActive(updatedTask.taskId || updatedTask.id)
|
|
611
719
|
updatedTask.status = 'resuming'
|
|
720
|
+
updatedTask.updatedAt = Date.now()
|
|
612
721
|
resumeTaskToRun = { ...updatedTask }
|
|
613
722
|
} else if (updatedTask.status === 'awaiting_approval') {
|
|
614
723
|
updatedTask.status = 'completed'
|
|
724
|
+
updatedTask.updatedAt = Date.now()
|
|
615
725
|
updatedTask.resolvedAt = Date.now()
|
|
616
726
|
}
|
|
617
727
|
}
|
|
@@ -621,34 +731,54 @@ export async function handleApprovalDecision(deps, {
|
|
|
621
731
|
|
|
622
732
|
let resumeOut = null
|
|
623
733
|
if (resumeTaskToRun) {
|
|
734
|
+
const rTaskId = resumeTaskToRun.taskId || resumeTaskToRun.id
|
|
624
735
|
const execSummaryJson = (() => {
|
|
625
736
|
try { return JSON.stringify(execRes || {}).slice(0, 1000) } catch { return clamp(resultMsg, 400) }
|
|
626
737
|
})()
|
|
627
738
|
try {
|
|
739
|
+
const spec = resumeTaskToRun.resumeSpec || resumeTaskToRun
|
|
628
740
|
resumeOut = await askOrCommandChief(deps, {
|
|
629
741
|
sessionId: rootId,
|
|
630
|
-
text: resumeTaskToRun.originalText,
|
|
631
|
-
forcedTarget: resumeTaskToRun.forcedTarget || '',
|
|
632
|
-
forcedMode: resumeTaskToRun.forcedMode || 'steer',
|
|
742
|
+
text: spec.originalText || resumeTaskToRun.originalText,
|
|
743
|
+
forcedTarget: spec.forcedTarget || resumeTaskToRun.forcedTarget || '',
|
|
744
|
+
forcedMode: spec.forcedMode || resumeTaskToRun.forcedMode || 'steer',
|
|
633
745
|
_resumeCtx: {
|
|
634
|
-
resumeTaskId:
|
|
635
|
-
resumeDepth: (resumeTaskToRun.resumeDepth || 0) + 1,
|
|
746
|
+
resumeTaskId: rTaskId,
|
|
747
|
+
resumeDepth: (resumeTaskToRun.retryCount || resumeTaskToRun.resumeDepth || 0) + 1,
|
|
636
748
|
triggerKind: 'approval',
|
|
637
749
|
executedSummary: `${found.operationLabel} (${found.targetLabel}) => ${execSummaryJson}`,
|
|
638
|
-
savedImagePaths: resumeTaskToRun.savedImagePaths || [],
|
|
750
|
+
savedImagePaths: spec.savedImagePaths || resumeTaskToRun.savedImagePaths || [],
|
|
639
751
|
},
|
|
640
752
|
})
|
|
753
|
+
mutateChiefState((st) => {
|
|
754
|
+
const ws = ensureWorkspace(st, rootId)
|
|
755
|
+
const ledgerList = Array.isArray(ws.taskLedger) ? ws.taskLedger : (ws.resumeTasks || [])
|
|
756
|
+
const task = ledgerList.find(t => t && (t.taskId === rTaskId || t.id === rTaskId))
|
|
757
|
+
if (task && (task.status === 'resuming' || task.status === 'verifying')) {
|
|
758
|
+
task.status = 'verifying'
|
|
759
|
+
task.updatedAt = Date.now()
|
|
760
|
+
const verifiedOk = Boolean(resumeOut && resumeOut.ok !== false)
|
|
761
|
+
task.status = verifiedOk ? 'completed' : 'failed'
|
|
762
|
+
task.lastReceipt = clamp((resumeOut && (resumeOut.reply || resumeOut.error)) || resultMsg, 260)
|
|
763
|
+
task.resolvedAt = Date.now()
|
|
764
|
+
}
|
|
765
|
+
})
|
|
641
766
|
} catch (err) {
|
|
642
767
|
resumeOut = { ok: false, error: (err && err.message) || String(err || 'resume failed') }
|
|
768
|
+
mutateChiefState((st) => {
|
|
769
|
+
const ws = ensureWorkspace(st, rootId)
|
|
770
|
+
const ledgerList = Array.isArray(ws.taskLedger) ? ws.taskLedger : (ws.resumeTasks || [])
|
|
771
|
+
const task = ledgerList.find(t => t && (t.taskId === rTaskId || t.id === rTaskId))
|
|
772
|
+
if (task && (task.status === 'resuming' || task.status === 'verifying')) {
|
|
773
|
+
task.status = 'failed'
|
|
774
|
+
task.updatedAt = Date.now()
|
|
775
|
+
task.lastReceipt = clamp(resumeOut.error, 260)
|
|
776
|
+
task.resolvedAt = Date.now()
|
|
777
|
+
}
|
|
778
|
+
})
|
|
779
|
+
} finally {
|
|
780
|
+
unmarkTaskResumingActive(rTaskId)
|
|
643
781
|
}
|
|
644
|
-
mutateChiefState((st) => {
|
|
645
|
-
const ws = ensureWorkspace(st, rootId)
|
|
646
|
-
const task = (ws.resumeTasks || []).find(t => t && t.id === resumeTaskToRun.id)
|
|
647
|
-
if (task && task.status === 'resuming') {
|
|
648
|
-
task.status = resumeOut && resumeOut.ok !== false ? 'completed' : 'failed'
|
|
649
|
-
task.resolvedAt = Date.now()
|
|
650
|
-
}
|
|
651
|
-
})
|
|
652
782
|
}
|
|
653
783
|
|
|
654
784
|
const latestBoard = (resumeOut && resumeOut.board)
|
|
@@ -697,16 +827,21 @@ export async function handleElevationDecision(deps, {
|
|
|
697
827
|
mutateChiefState((s) => {
|
|
698
828
|
const ws = ensureWorkspace(s, rootId)
|
|
699
829
|
ws.pendingElevation = null
|
|
700
|
-
|
|
701
|
-
|
|
830
|
+
const elevTaskId = elev.taskId || elev.resumeTaskId || ''
|
|
831
|
+
const ledgerList = Array.isArray(ws.taskLedger) ? ws.taskLedger : (ws.resumeTasks || [])
|
|
832
|
+
if (elevTaskId) {
|
|
833
|
+
const linkedTask = ledgerList.find(t => t && (t.taskId === elevTaskId || t.id === elevTaskId))
|
|
702
834
|
if (linkedTask) {
|
|
703
835
|
linkedTask.status = 'cancelled'
|
|
836
|
+
linkedTask.updatedAt = Date.now()
|
|
704
837
|
linkedTask.resolvedAt = Date.now()
|
|
838
|
+
linkedTask.lastReceipt = '用户拒绝提权申请'
|
|
705
839
|
}
|
|
706
840
|
}
|
|
707
841
|
pushAuditToWs(ws, {
|
|
708
842
|
mode: resolveWorkspaceConfig(s, rootId).permissionMode,
|
|
709
843
|
stage: 'permission_gate',
|
|
844
|
+
rule: 'elevation_rejected',
|
|
710
845
|
decision: 'elevation_rejected',
|
|
711
846
|
operation: '拒绝提权申请',
|
|
712
847
|
target: `${elev.fromMode} -> ${elev.targetMode}`,
|
|
@@ -760,12 +895,14 @@ export async function handleElevationDecision(deps, {
|
|
|
760
895
|
}
|
|
761
896
|
|
|
762
897
|
let resumeTaskToRun = null
|
|
898
|
+
const elevTaskId = elev.taskId || elev.resumeTaskId || ''
|
|
763
899
|
mutateChiefState((s) => {
|
|
764
900
|
const ws = ensureWorkspace(s, rootId)
|
|
765
901
|
ws.pendingElevation = null
|
|
766
902
|
pushAuditToWs(ws, {
|
|
767
903
|
mode: chosenMode,
|
|
768
904
|
stage: 'permission_gate',
|
|
905
|
+
rule: 'elevation_approved',
|
|
769
906
|
decision: 'elevation_approved',
|
|
770
907
|
operation: '批准提权',
|
|
771
908
|
target: `${CHIEF_PERMISSION_META[elev.fromMode]?.level || elev.fromMode} -> ${CHIEF_PERMISSION_META[chosenMode].level}`,
|
|
@@ -778,21 +915,25 @@ export async function handleElevationDecision(deps, {
|
|
|
778
915
|
id: nextId('chat'),
|
|
779
916
|
role: 'chief',
|
|
780
917
|
kind: 'receipt',
|
|
781
|
-
...(
|
|
918
|
+
...(elevTaskId ? { resumeTaskId: elevTaskId } : {}),
|
|
782
919
|
text: `- 【🔐 已批准提权】当前会话权限已升级为「**${meta.level} · ${meta.title}**」${chosenMode === 'chief' ? '(已完成高风险确认)' : ''}。`,
|
|
783
920
|
actions: [`🔐 升至 ${meta.level}`],
|
|
784
921
|
at: Date.now(),
|
|
785
922
|
})
|
|
786
923
|
if (ws.chat.length > CHAT_CAP) ws.chat = ws.chat.slice(-CHAT_CAP)
|
|
787
924
|
|
|
788
|
-
|
|
789
|
-
|
|
925
|
+
const ledgerList = Array.isArray(ws.taskLedger) ? ws.taskLedger : (ws.resumeTasks || [])
|
|
926
|
+
if (elevTaskId) {
|
|
927
|
+
const task = ledgerList.find(t => t && (t.taskId === elevTaskId || t.id === elevTaskId))
|
|
790
928
|
if (task) {
|
|
791
929
|
if (task.expiresAt && Date.now() > task.expiresAt) {
|
|
792
930
|
task.status = 'expired'
|
|
931
|
+
task.updatedAt = Date.now()
|
|
793
932
|
task.resolvedAt = Date.now()
|
|
794
|
-
} else if ((task.resumeDepth || 0) < 2 && task.status !== 'resuming') {
|
|
933
|
+
} else if ((task.retryCount || task.resumeDepth || 0) < 2 && task.status !== 'resuming') {
|
|
934
|
+
markTaskResumingActive(task.taskId || task.id)
|
|
795
935
|
task.status = 'resuming'
|
|
936
|
+
task.updatedAt = Date.now()
|
|
796
937
|
resumeTaskToRun = { ...task }
|
|
797
938
|
}
|
|
798
939
|
}
|
|
@@ -801,32 +942,51 @@ export async function handleElevationDecision(deps, {
|
|
|
801
942
|
|
|
802
943
|
let resumeOut = null
|
|
803
944
|
if (resumeTaskToRun) {
|
|
945
|
+
const rTaskId = resumeTaskToRun.taskId || resumeTaskToRun.id
|
|
804
946
|
const meta = CHIEF_PERMISSION_META[chosenMode]
|
|
805
947
|
try {
|
|
948
|
+
const spec = resumeTaskToRun.resumeSpec || resumeTaskToRun
|
|
806
949
|
resumeOut = await askOrCommandChief(deps, {
|
|
807
950
|
sessionId: rootId,
|
|
808
|
-
text: resumeTaskToRun.originalText,
|
|
809
|
-
forcedTarget: resumeTaskToRun.forcedTarget || '',
|
|
810
|
-
forcedMode: resumeTaskToRun.forcedMode || 'steer',
|
|
951
|
+
text: spec.originalText || resumeTaskToRun.originalText,
|
|
952
|
+
forcedTarget: spec.forcedTarget || resumeTaskToRun.forcedTarget || '',
|
|
953
|
+
forcedMode: spec.forcedMode || resumeTaskToRun.forcedMode || 'steer',
|
|
811
954
|
_resumeCtx: {
|
|
812
|
-
resumeTaskId:
|
|
813
|
-
resumeDepth: (resumeTaskToRun.resumeDepth || 0) + 1,
|
|
955
|
+
resumeTaskId: rTaskId,
|
|
956
|
+
resumeDepth: (resumeTaskToRun.retryCount || resumeTaskToRun.resumeDepth || 0) + 1,
|
|
814
957
|
triggerKind: 'elevation',
|
|
815
958
|
executedSummary: `已批准提权至 ${meta.level} · ${meta.title}`,
|
|
816
|
-
savedImagePaths: resumeTaskToRun.savedImagePaths || [],
|
|
959
|
+
savedImagePaths: spec.savedImagePaths || resumeTaskToRun.savedImagePaths || [],
|
|
817
960
|
},
|
|
818
961
|
})
|
|
962
|
+
mutateChiefState((st) => {
|
|
963
|
+
const ws = ensureWorkspace(st, rootId)
|
|
964
|
+
const ledgerList = Array.isArray(ws.taskLedger) ? ws.taskLedger : (ws.resumeTasks || [])
|
|
965
|
+
const task = ledgerList.find(t => t && (t.taskId === rTaskId || t.id === rTaskId))
|
|
966
|
+
if (task && (task.status === 'resuming' || task.status === 'verifying')) {
|
|
967
|
+
task.status = 'verifying'
|
|
968
|
+
task.updatedAt = Date.now()
|
|
969
|
+
task.status = resumeOut && resumeOut.ok !== false ? 'completed' : 'failed'
|
|
970
|
+
task.lastReceipt = clamp((resumeOut && (resumeOut.reply || resumeOut.error)) || `提权至 ${meta.level} 后已自动续跑`, 260)
|
|
971
|
+
task.resolvedAt = Date.now()
|
|
972
|
+
}
|
|
973
|
+
})
|
|
819
974
|
} catch (err) {
|
|
820
975
|
resumeOut = { ok: false, error: (err && err.message) || String(err || 'elevation resume failed') }
|
|
976
|
+
mutateChiefState((st) => {
|
|
977
|
+
const ws = ensureWorkspace(st, rootId)
|
|
978
|
+
const ledgerList = Array.isArray(ws.taskLedger) ? ws.taskLedger : (ws.resumeTasks || [])
|
|
979
|
+
const task = ledgerList.find(t => t && (t.taskId === rTaskId || t.id === rTaskId))
|
|
980
|
+
if (task && (task.status === 'resuming' || task.status === 'verifying')) {
|
|
981
|
+
task.status = 'failed'
|
|
982
|
+
task.updatedAt = Date.now()
|
|
983
|
+
task.lastReceipt = clamp(resumeOut.error, 260)
|
|
984
|
+
task.resolvedAt = Date.now()
|
|
985
|
+
}
|
|
986
|
+
})
|
|
987
|
+
} finally {
|
|
988
|
+
unmarkTaskResumingActive(rTaskId)
|
|
821
989
|
}
|
|
822
|
-
mutateChiefState((st) => {
|
|
823
|
-
const ws = ensureWorkspace(st, rootId)
|
|
824
|
-
const task = (ws.resumeTasks || []).find(t => t && t.id === resumeTaskToRun.id)
|
|
825
|
-
if (task && task.status === 'resuming') {
|
|
826
|
-
task.status = resumeOut && resumeOut.ok !== false ? 'completed' : 'failed'
|
|
827
|
-
task.resolvedAt = Date.now()
|
|
828
|
-
}
|
|
829
|
-
})
|
|
830
990
|
}
|
|
831
991
|
|
|
832
992
|
return {
|