dsh-chief 1.4.0 → 1.5.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 -0
- package/README.en.md +66 -50
- package/README.md +67 -50
- package/api-routes.js +165 -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 +59 -4
- package/board-schema.js +1 -1
- package/chief-approvals.js +176 -11
- package/chief-archives.js +157 -30
- package/chief-commands.js +565 -101
- package/chief-models.js +158 -28
- package/chief-review.js +211 -70
- package/chief-state.js +362 -32
- package/chief-view.js +17 -14
- package/client.js +3744 -2207
- package/digest.js +18 -9
- package/model-tools.js +15 -5
- package/package.json +9 -4
- package/runtime-hooks.js +115 -12
- package/subagent-runtime.js +257 -1
package/chief-review.js
CHANGED
|
@@ -12,6 +12,7 @@ import {
|
|
|
12
12
|
REVIEW_DELAY_MS,
|
|
13
13
|
SMART_REVIEW_COOLDOWN_MS,
|
|
14
14
|
AUTO_REVIEW_COOLDOWN_MS,
|
|
15
|
+
DEFAULT_MANAGED_IDLE_TIMEOUT_MS,
|
|
15
16
|
SUMMARY_MAX_TOKENS,
|
|
16
17
|
archiveTargetForRoot,
|
|
17
18
|
nextId,
|
|
@@ -37,7 +38,7 @@ import {
|
|
|
37
38
|
import { listChiefModels, callChiefLlm } from './chief-models.js'
|
|
38
39
|
import { listPeerDirectory, deliverCrossMessage } from './chief-network.js'
|
|
39
40
|
import { summarizingLocks, getChiefView, registerIdleAutoPilotHook } from './chief-view.js'
|
|
40
|
-
import {
|
|
41
|
+
import { runManagedDreamRoam } from './chief-archives.js'
|
|
41
42
|
import {
|
|
42
43
|
waitUntilAgentIdle,
|
|
43
44
|
measureAgentSessionTokensFallback,
|
|
@@ -46,6 +47,7 @@ import {
|
|
|
46
47
|
maybeRunPreCompaction,
|
|
47
48
|
} from './chief-compaction.js'
|
|
48
49
|
import { applyFactChanges, applyBoardOps } from './chief-commands.js'
|
|
50
|
+
import { executeBatchSuggestions } from './chief-approvals.js'
|
|
49
51
|
|
|
50
52
|
const TURN_REVIEW_SYSTEM = [
|
|
51
53
|
'你是「DSH 参谋长」(宰相级协同与督导中枢)的后台复盘与督导模块。某个对话刚结束一轮输出。',
|
|
@@ -194,7 +196,7 @@ function extractLatestTurnDigest(agent, turn) {
|
|
|
194
196
|
|
|
195
197
|
const lastReviewAtByRoot = new Map()
|
|
196
198
|
|
|
197
|
-
export function scheduleChiefTurnReview(deps, { agent, sessionId, turn }) {
|
|
199
|
+
export function scheduleChiefTurnReview(deps, { agent, sessionId, turn, abnormalTrigger = '' }) {
|
|
198
200
|
const sid = String(sessionId || (agent && agent.id) || '').trim()
|
|
199
201
|
if (!sid) return
|
|
200
202
|
// Never waste tokens reviewing internal mnemon checkpoint / probe subagents
|
|
@@ -204,35 +206,41 @@ export function scheduleChiefTurnReview(deps, { agent, sessionId, turn }) {
|
|
|
204
206
|
const state = loadChiefState()
|
|
205
207
|
const rootKey = (deps && deps.resolveRootSessionId ? deps.resolveRootSessionId(sid, agent) : '') || sid
|
|
206
208
|
const effCfg = resolveWorkspaceConfig(state, rootKey)
|
|
207
|
-
const
|
|
208
|
-
|
|
209
|
+
const runMode = effCfg.runMode || 'off'
|
|
210
|
+
const tactical = normalizeTacticalModes(effCfg.tacticalModes)
|
|
211
|
+
const wsSnap = state.workspaces && state.workspaces[rootKey] ? state.workspaces[rootKey] : null
|
|
212
|
+
const pendingSmart = Boolean(wsSnap && wsSnap.runtimeGuard && wsSnap.runtimeGuard.pendingSmartCompact)
|
|
213
|
+
const compactChannel = Boolean(tactical.autoCompact || tactical.smartCompact || pendingSmart)
|
|
214
|
+
|
|
215
|
+
if (effCfg.stopped || (runMode === 'off' && !compactChannel)) {
|
|
209
216
|
clearTimeout(reviewTimers.get(rootKey))
|
|
210
217
|
reviewTimers.delete(rootKey)
|
|
211
218
|
return
|
|
212
219
|
}
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
const
|
|
220
|
+
|
|
221
|
+
// In PATROL mode: normal turns consume 0 background tokens; wake ONLY on explicit abnormal triggers or compaction checks
|
|
222
|
+
const hasAbnormal = Boolean(String(abnormalTrigger || '').trim())
|
|
223
|
+
if (runMode === 'patrol' && !hasAbnormal && !compactChannel) {
|
|
224
|
+
return
|
|
225
|
+
}
|
|
226
|
+
|
|
216
227
|
const board = deps && deps.boardForSession && deps.boardFileFor
|
|
217
228
|
? deps.boardForSession(deps.boardFileFor(rootKey))
|
|
218
229
|
: null
|
|
219
230
|
const isGoalMode = Boolean(board && board.mode === 'goal')
|
|
220
|
-
const hasHighPriorityChannel =
|
|
221
|
-
|
|
222
|
-
// Default 'manual' mode without tactical/goal active: ZERO background LLM calls!
|
|
223
|
-
if (!hasHighPriorityChannel && (paceMode === 'manual' || effCfg.autoReview === false)) return
|
|
231
|
+
const hasHighPriorityChannel = hasAbnormal || isGoalMode || compactChannel || tactical.intervention || tactical.redlineSentinel || tactical.strictVerify
|
|
224
232
|
|
|
225
233
|
clearTimeout(reviewTimers.get(rootKey))
|
|
226
234
|
const delayMs = hasHighPriorityChannel ? 1_200 : REVIEW_DELAY_MS
|
|
227
235
|
const timer = setTimeout(() => {
|
|
228
236
|
reviewTimers.delete(rootKey)
|
|
229
|
-
runTurnReview(deps, { agent, sessionId: sid, turn }).catch(() => {})
|
|
237
|
+
runTurnReview(deps, { agent, sessionId: sid, turn, abnormalTrigger }).catch(() => {})
|
|
230
238
|
}, delayMs)
|
|
231
239
|
timer.unref?.()
|
|
232
240
|
reviewTimers.set(rootKey, timer)
|
|
233
241
|
}
|
|
234
242
|
|
|
235
|
-
export async function runTurnReview(deps, { agent, sessionId, turn, force = false }) {
|
|
243
|
+
export async function runTurnReview(deps, { agent, sessionId, turn, force = false, abnormalTrigger = '' }) {
|
|
236
244
|
const sid = String(sessionId || (agent && agent.id) || '').trim()
|
|
237
245
|
if (!sid) return { ok: false, skipped: 'no-session' }
|
|
238
246
|
const rootId = (deps.resolveRootSessionId && deps.resolveRootSessionId(sid, agent)) || sid
|
|
@@ -240,25 +248,28 @@ export async function runTurnReview(deps, { agent, sessionId, turn, force = fals
|
|
|
240
248
|
const stateBefore = loadChiefState()
|
|
241
249
|
const effCfgBefore = resolveWorkspaceConfig(stateBefore, rootId)
|
|
242
250
|
const tactical = normalizeTacticalModes(effCfgBefore.tacticalModes)
|
|
251
|
+
const runMode = effCfgBefore.runMode || 'off'
|
|
243
252
|
const paceMode = effCfgBefore.paceMode || 'manual'
|
|
244
|
-
|
|
253
|
+
const wsBefore = ensureWorkspace(stateBefore, rootId)
|
|
254
|
+
const pendingSmart = Boolean(wsBefore.runtimeGuard && wsBefore.runtimeGuard.pendingSmartCompact)
|
|
255
|
+
const compactChannel = Boolean(tactical.autoCompact || tactical.smartCompact || pendingSmart)
|
|
256
|
+
const hasAbnormal = Boolean(String(abnormalTrigger || '').trim())
|
|
257
|
+
|
|
258
|
+
if (!force && effCfgBefore.stopped) {
|
|
245
259
|
return { ok: false, skipped: 'chief-stopped' }
|
|
246
260
|
}
|
|
247
|
-
|
|
261
|
+
if (!force && runMode === 'off' && !compactChannel) {
|
|
262
|
+
return { ok: false, skipped: 'off' }
|
|
263
|
+
}
|
|
248
264
|
const board = deps.boardForSession && deps.boardFileFor
|
|
249
265
|
? deps.boardForSession(deps.boardFileFor(rootId))
|
|
250
266
|
: null
|
|
251
267
|
const isGoalMode = Boolean(board && board.mode === 'goal')
|
|
252
|
-
const pendingSmart = Boolean(wsBefore.runtimeGuard && wsBefore.runtimeGuard.pendingSmartCompact)
|
|
253
268
|
const smartCoManaged = Boolean(tactical.smartCompact)
|
|
254
|
-
const hasHighPriority = isGoalMode || tactical.intervention || tactical.redlineSentinel || tactical.strictVerify ||
|
|
255
|
-
|
|
256
|
-
if (!force && !hasHighPriority && paceMode === 'manual') {
|
|
257
|
-
return { ok: false, skipped: 'manual' }
|
|
258
|
-
}
|
|
269
|
+
const hasHighPriority = hasAbnormal || isGoalMode || tactical.intervention || tactical.redlineSentinel || tactical.strictVerify || compactChannel
|
|
259
270
|
|
|
260
271
|
// Always check pre-compaction pipeline first when autoCompact/smartCompact/pendingSmartCompact is active
|
|
261
|
-
if (
|
|
272
|
+
if (compactChannel) {
|
|
262
273
|
const preRes = await maybeRunPreCompaction(deps, { agent, sessionId: sid, rootId, bypassIdleWait: Boolean(force) })
|
|
263
274
|
.catch((err) => {
|
|
264
275
|
const msg = (err && err.message) || String(err || 'pre-compact-error')
|
|
@@ -272,13 +283,29 @@ export async function runTurnReview(deps, { agent, sessionId, turn, force = fals
|
|
|
272
283
|
}
|
|
273
284
|
}
|
|
274
285
|
|
|
275
|
-
// If
|
|
276
|
-
|
|
277
|
-
if (!force &&
|
|
286
|
+
// If runMode === 'off' and we only entered for standalone smartCompact/autoCompact pre-check,
|
|
287
|
+
// exit cleanly once autoCompact threshold isn't forcing LLM, except when smartCoManaged needs its idle evaluation.
|
|
288
|
+
if (!force && runMode === 'off' && !smartCoManaged) {
|
|
289
|
+
return { ok: true, onlyCompactionChecked: true }
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
// If ONLY fixed-threshold autoCompact was on (without smartCompact or patrol-abnormal or managed), exit cleanly
|
|
293
|
+
const needsSupervisorLlm = (runMode === 'managed')
|
|
294
|
+
|| (runMode === 'patrol' && hasAbnormal)
|
|
295
|
+
|| smartCoManaged
|
|
296
|
+
|| (force && (isGoalMode || tactical.intervention || tactical.redlineSentinel || tactical.strictVerify))
|
|
297
|
+
if (!force && !needsSupervisorLlm && runMode !== 'patrol') {
|
|
298
|
+
return { ok: true, onlyCompactionChecked: true }
|
|
299
|
+
}
|
|
278
300
|
|
|
279
301
|
const digest = extractLatestTurnDigest(agent, turn)
|
|
280
302
|
if (!digest || !digest.text) return { ok: false, skipped: 'no-digest' }
|
|
281
303
|
|
|
304
|
+
const hasWallCue = hasAbnormal || /(?:permission denied|eacces|eperm|forbidden|blocked|not allowed|sandbox|read-only|撞墙|受阻|权限拒绝|越界)/i.test(digest.text)
|
|
305
|
+
if (!force && runMode === 'patrol' && !hasWallCue && !smartCoManaged) {
|
|
306
|
+
return { ok: false, skipped: 'patrol-no-abnormal' }
|
|
307
|
+
}
|
|
308
|
+
|
|
282
309
|
const cursorKey = `${sid}`
|
|
283
310
|
if (!force && wsBefore.reviewCursors[cursorKey] === digest.stamp) {
|
|
284
311
|
return { ok: false, skipped: 'same-stamp' }
|
|
@@ -287,12 +314,11 @@ export async function runTurnReview(deps, { agent, sessionId, turn, force = fals
|
|
|
287
314
|
const identity = deps.resolveAgentIdentity ? deps.resolveAgentIdentity(deps.runtime, sid, agent) : null
|
|
288
315
|
if (identity && /^mnemon-(?:review|run)/i.test(String(identity.self || ''))) return { ok: false, skipped: 'mnemon' }
|
|
289
316
|
|
|
290
|
-
const hasWallCue = /(?:permission denied|eacces|eperm|forbidden|blocked|not allowed|sandbox|read-only|撞墙|受阻|权限拒绝|越界)/i.test(digest.text)
|
|
291
317
|
const now = Date.now()
|
|
292
318
|
const lastAt = Number(lastReviewAtByRoot.get(rootId) || 0)
|
|
293
319
|
const cooldownMs = hasHighPriority
|
|
294
|
-
? (smartCoManaged && !isGoalMode && !tactical.intervention ? 12_000 : 4_000)
|
|
295
|
-
: (
|
|
320
|
+
? (smartCoManaged && !isGoalMode && !tactical.intervention && !hasAbnormal ? 12_000 : 4_000)
|
|
321
|
+
: (runMode === 'patrol' ? (hasWallCue ? 45_000 : SMART_REVIEW_COOLDOWN_MS) : AUTO_REVIEW_COOLDOWN_MS)
|
|
296
322
|
if (!force && now - lastAt < cooldownMs) return { ok: false, skipped: 'cooldown' }
|
|
297
323
|
|
|
298
324
|
// Wait if supervisor/goal/smart-compact is active so turn-stopping settles to idle before evaluating usage
|
|
@@ -314,17 +340,14 @@ export async function runTurnReview(deps, { agent, sessionId, turn, force = fals
|
|
|
314
340
|
|
|
315
341
|
// In 'smart' background mode (without supervisor/goal), gate LLM call on wall cue or multi-agent human turn;
|
|
316
342
|
// when ONLY smartCoManaged is enabled in manual pace, evaluate only after enough turns or context pressure.
|
|
317
|
-
if (!force && !isGoalMode && !tactical.intervention && !tactical.redlineSentinel && !tactical.strictVerify) {
|
|
318
|
-
if (smartCoManaged &&
|
|
343
|
+
if (!force && !isGoalMode && !tactical.intervention && !tactical.redlineSentinel && !tactical.strictVerify && !hasAbnormal) {
|
|
344
|
+
if (smartCoManaged && runMode === 'off') {
|
|
319
345
|
const ratio = liveUsage && liveUsage.contextWindow ? (Number(liveUsage.totalTokens) || 0) / Number(liveUsage.contextWindow) : 0
|
|
320
346
|
const sinceCompact = now - ((wsBefore.runtimeGuard && wsBefore.runtimeGuard.lastCompactedAt) || 0)
|
|
321
347
|
if ((digest.stamp < 4 && ratio < 0.25) || sinceCompact < 20_000) {
|
|
322
348
|
return { ok: false, skipped: 'smart-compact-light' }
|
|
323
349
|
}
|
|
324
|
-
} else {
|
|
325
|
-
if (paceMode === 'smart' && !hasWallCue && !(digest.hasUser && realPeers.length > 1)) {
|
|
326
|
-
return { ok: false, skipped: 'smart-quiet' }
|
|
327
|
-
}
|
|
350
|
+
} else if (runMode === 'managed') {
|
|
328
351
|
if (!digest.hasUser && realPeers.length <= 1 && !hasWallCue) return { ok: false, skipped: 'single-quiet' }
|
|
329
352
|
}
|
|
330
353
|
}
|
|
@@ -335,11 +358,13 @@ export async function runTurnReview(deps, { agent, sessionId, turn, force = fals
|
|
|
335
358
|
}
|
|
336
359
|
|
|
337
360
|
lastReviewAtByRoot.set(rootId, now)
|
|
338
|
-
const isAutoPilotActiveNow = Boolean(
|
|
361
|
+
const isAutoPilotActiveNow = Boolean(runMode === 'managed' && !effCfgBefore.stopped)
|
|
339
362
|
const reviewWorkId = beginChiefActiveWork(rootId, {
|
|
340
363
|
tone: 'neon',
|
|
341
364
|
tab: '',
|
|
342
|
-
label:
|
|
365
|
+
label: runMode === 'patrol'
|
|
366
|
+
? '参谋长正在巡检异常与诊断边界中...'
|
|
367
|
+
: (isAutoPilotActiveNow ? '参谋长正在托管督导巡检中...' : '参谋长正在后台复盘研判中...'),
|
|
343
368
|
})
|
|
344
369
|
try {
|
|
345
370
|
const speaker = identity && identity.isChild ? `子代理 @${identity.self}` : '主对话 (@lead)'
|
|
@@ -358,7 +383,8 @@ export async function runTurnReview(deps, { agent, sessionId, turn, force = fals
|
|
|
358
383
|
tactical.strictVerify ? 'STRICT_VERIFY' : '',
|
|
359
384
|
tactical.redlineSentinel ? 'REDLINE_SENTINEL' : '',
|
|
360
385
|
tactical.conciseBrief ? 'CONCISE_BRIEF_3_BULLETS' : '',
|
|
361
|
-
Boolean(
|
|
386
|
+
Boolean(runMode === 'managed' && !effCfgBefore.stopped) ? 'AUTO_PILOT_SESSION_ONLY' : '',
|
|
387
|
+
hasAbnormal ? `ABNORMAL_PATROL_TRIGGER(${abnormalTrigger})` : '',
|
|
362
388
|
].filter(Boolean)
|
|
363
389
|
|
|
364
390
|
const smartUsageHint = smartCoManaged
|
|
@@ -366,7 +392,7 @@ export async function runTurnReview(deps, { agent, sessionId, turn, force = fals
|
|
|
366
392
|
: ''
|
|
367
393
|
|
|
368
394
|
const prompt = [
|
|
369
|
-
`当前激活监督模式:${activeFlags.join(' | ') ||
|
|
395
|
+
`当前激活监督模式:${activeFlags.join(' | ') || runMode}`,
|
|
370
396
|
...(smartUsageHint ? [smartUsageHint] : []),
|
|
371
397
|
`工作区总目标 (Objective):${(board && board.objective) || '(暂未开启 /goal 固定目标 — 请对照下方最新用户原始诉求核验完成度)'}`,
|
|
372
398
|
`最新用户原始诉求:${digest.latestUserPrompt || '(见下方对话片段)'}`,
|
|
@@ -387,8 +413,12 @@ export async function runTurnReview(deps, { agent, sessionId, turn, force = fals
|
|
|
387
413
|
digest.text,
|
|
388
414
|
].join('\n')
|
|
389
415
|
|
|
416
|
+
const reviewReasonKind = hasAbnormal
|
|
417
|
+
? 'wall_blocked'
|
|
418
|
+
: (tactical.intervention ? 'strong_intervention' : 'turn_review')
|
|
390
419
|
const res = await callChiefLlm(deps, {
|
|
391
420
|
sessionId: rootId,
|
|
421
|
+
reasonKind: reviewReasonKind,
|
|
392
422
|
system: TURN_REVIEW_SYSTEM,
|
|
393
423
|
prompt,
|
|
394
424
|
maxTokens: SUMMARY_MAX_TOKENS,
|
|
@@ -403,7 +433,7 @@ export async function runTurnReview(deps, { agent, sessionId, turn, force = fals
|
|
|
403
433
|
const hasAckNow = hasValidChiefRiskAck(wsNowPerm)
|
|
404
434
|
const permNow = normalizePermissionMode(cfgNow.permissionMode, { requireAck: true, hasChiefRiskAck: hasAckNow })
|
|
405
435
|
const permRankNow = permissionRank(permNow)
|
|
406
|
-
const autoPilot = Boolean(cfgNow.
|
|
436
|
+
const autoPilot = Boolean(cfgNow.runMode === 'managed' && !cfgNow.stopped)
|
|
407
437
|
const rawSuggestions = (Array.isArray(parsed.suggestions) ? parsed.suggestions : [])
|
|
408
438
|
.map(raw => normalizeSuggestion({ ...raw, fromAgent: raw?.fromAgent || speaker }))
|
|
409
439
|
.filter(Boolean)
|
|
@@ -530,8 +560,9 @@ export async function runTurnReview(deps, { agent, sessionId, turn, force = fals
|
|
|
530
560
|
}
|
|
531
561
|
}
|
|
532
562
|
|
|
563
|
+
const useOneStopRunnerNow = Boolean(permNow === 'chief' && hasAckNow && tactical.intervention)
|
|
533
564
|
for (const sug of rawSuggestions) {
|
|
534
|
-
if (sug.kind === 'bound') {
|
|
565
|
+
if (!useOneStopRunnerNow && sug.kind === 'bound') {
|
|
535
566
|
const colList = Array.isArray(bounds[sug.boundField]) ? bounds[sug.boundField] : []
|
|
536
567
|
if (colList.some(x => String(x).trim() === sug.draftText)) continue
|
|
537
568
|
if (autoPilot && sug.draftText) {
|
|
@@ -541,7 +572,7 @@ export async function runTurnReview(deps, { agent, sessionId, turn, force = fals
|
|
|
541
572
|
continue
|
|
542
573
|
}
|
|
543
574
|
}
|
|
544
|
-
if (autoPilot && sug.kind === 'relay' && sug.draftText && isInternalRelayTarget(sug.target)) {
|
|
575
|
+
if (!useOneStopRunnerNow && autoPilot && sug.kind === 'relay' && sug.draftText && isInternalRelayTarget(sug.target)) {
|
|
545
576
|
if (permNow === 'chief' && hasAckNow) {
|
|
546
577
|
sug.status = 'applied'
|
|
547
578
|
autoRelays.push(sug)
|
|
@@ -571,9 +602,9 @@ export async function runTurnReview(deps, { agent, sessionId, turn, force = fals
|
|
|
571
602
|
}
|
|
572
603
|
}
|
|
573
604
|
|
|
574
|
-
if (autoPilot && rootId && rootId !== 'global') {
|
|
605
|
+
if (autoPilot && permRankNow >= 1 && rootId && rootId !== 'global') {
|
|
575
606
|
const lastOrgAt = Number(ws.runtimeGuard.lastAutoArchiveOrgAt || 0)
|
|
576
|
-
if (!lastOrgAt || Date.now() - lastOrgAt >
|
|
607
|
+
if (!lastOrgAt || Date.now() - lastOrgAt > 60_000) {
|
|
577
608
|
shouldAutoOrganizeSession = true
|
|
578
609
|
}
|
|
579
610
|
}
|
|
@@ -871,29 +902,91 @@ export async function runTurnReview(deps, { agent, sessionId, turn, force = fals
|
|
|
871
902
|
}
|
|
872
903
|
}
|
|
873
904
|
|
|
874
|
-
|
|
905
|
+
let dreamRoamRes = null
|
|
906
|
+
if (autoPilot && permRankNow >= 1 && !hazardStopped && rootId && rootId !== 'global') {
|
|
875
907
|
try {
|
|
876
908
|
const sessOpt = archiveTargetForRoot(deps, rootId, { vaultScope: 'session' })
|
|
909
|
+
const globOpt = archiveTargetForRoot(deps, rootId, { vaultScope: 'global' })
|
|
877
910
|
const stSess = loadArchiveStore(sessOpt)
|
|
878
|
-
const
|
|
879
|
-
|
|
880
|
-
).length
|
|
881
|
-
|
|
911
|
+
const stGlob = loadArchiveStore(globOpt)
|
|
912
|
+
const hasWatch = Array.isArray(stSess.recentlyUnpinnedWatch) && stSess.recentlyUnpinnedWatch.length > 0
|
|
913
|
+
const hasUnhookedSess = (stSess.items || []).some(it => !it.hasHook || it.folder === 'inbox' || it.organizedBy !== 'chief' || (it.points || []).length <= 1)
|
|
914
|
+
const hasUnhookedGlob = (stGlob.items || []).some(it => !it.hasHook || it.folder === 'inbox' || it.organizedBy !== 'chief' || (it.points || []).length <= 1)
|
|
915
|
+
if (hasWatch || ((shouldAutoOrganizeSession || force) && (hasUnhookedSess || hasUnhookedGlob))) {
|
|
882
916
|
updateChiefActiveWork(rootId, reviewWorkId, {
|
|
883
|
-
tone: '
|
|
917
|
+
tone: 'dream',
|
|
884
918
|
tab: 'archive',
|
|
885
|
-
label: '
|
|
919
|
+
label: '🌙 参谋长正在梦境漫游 · 轻拢卷宗与织就钩子中...',
|
|
886
920
|
})
|
|
887
921
|
mutateChiefState((s) => {
|
|
888
922
|
const ws = ensureWorkspace(s, rootId)
|
|
889
923
|
ws.runtimeGuard.lastAutoArchiveOrgAt = Date.now()
|
|
890
924
|
})
|
|
891
|
-
await
|
|
925
|
+
dreamRoamRes = await runManagedDreamRoam(deps, { sessionId: rootId }).catch(() => null)
|
|
926
|
+
}
|
|
927
|
+
} catch {}
|
|
928
|
+
}
|
|
929
|
+
|
|
930
|
+
// L3 Chief + Strong Intervention: One-Stop Task Runner sweeping safe pending suggestions
|
|
931
|
+
// (excludes guard_wall, goal_approval, pinGlobal global bounds, and superseded L1 fallback advice when live intervention ran)
|
|
932
|
+
let oneStopReport = null
|
|
933
|
+
if (permNow === 'chief' && hasAckNow && tactical.intervention && !hazardStopped) {
|
|
934
|
+
try {
|
|
935
|
+
const wsLatest = ensureWorkspace(loadChiefState(), rootId)
|
|
936
|
+
const safePending = (wsLatest.suggestions || []).filter(s =>
|
|
937
|
+
s
|
|
938
|
+
&& s.status === 'pending'
|
|
939
|
+
&& s.kind !== 'guard_wall'
|
|
940
|
+
&& s.kind !== 'wall_removal'
|
|
941
|
+
&& s.kind !== 'goal_approval'
|
|
942
|
+
&& !(s.kind === 'bound' && s.pinGlobal)
|
|
943
|
+
&& !(interventionMessage && s.title === 'L1 强介入建议(等待拍板,未自动递给主对话)'),
|
|
944
|
+
)
|
|
945
|
+
if (safePending.length > 0) {
|
|
946
|
+
updateChiefActiveWork(rootId, reviewWorkId, {
|
|
947
|
+
tone: 'neon',
|
|
948
|
+
tab: 'chief',
|
|
949
|
+
label: '🏁 参谋长正在一站式推进建议清单中...',
|
|
950
|
+
})
|
|
951
|
+
const batchOut = await executeBatchSuggestions(deps, {
|
|
892
952
|
sessionId: rootId,
|
|
893
|
-
|
|
894
|
-
|
|
895
|
-
|
|
896
|
-
|
|
953
|
+
ids: safePending.map(s => s.id),
|
|
954
|
+
includeGoalApproval: false,
|
|
955
|
+
skipGuardWalls: true,
|
|
956
|
+
skipGlobalBounds: true,
|
|
957
|
+
byAutoRunner: true,
|
|
958
|
+
skipChatReceipt: true,
|
|
959
|
+
})
|
|
960
|
+
if (batchOut && (batchOut.executedCount > 0 || batchOut.blockedCount > 0 || batchOut.failedCount > 0)) {
|
|
961
|
+
oneStopReport = {
|
|
962
|
+
executedCount: batchOut.executedCount,
|
|
963
|
+
blockedCount: batchOut.blockedCount,
|
|
964
|
+
failedCount: batchOut.failedCount,
|
|
965
|
+
skippedGoalCount: batchOut.skippedGoalCount,
|
|
966
|
+
summaries: batchOut.summaries || [],
|
|
967
|
+
}
|
|
968
|
+
mutateChiefState((s) => {
|
|
969
|
+
const ws = ensureWorkspace(s, rootId)
|
|
970
|
+
ws.chat.push({
|
|
971
|
+
id: nextId('chat'),
|
|
972
|
+
role: 'chief',
|
|
973
|
+
kind: 'receipt',
|
|
974
|
+
isOneStopReport: true,
|
|
975
|
+
...(res.thinkingEffort ? { thinkingEffort: res.thinkingEffort } : {}),
|
|
976
|
+
...(res.thinkingEffortMode ? { thinkingEffortMode: res.thinkingEffortMode } : {}),
|
|
977
|
+
text: [
|
|
978
|
+
`- 【🏁 一站式战报回执】L3 强介入已顺序推进 ${batchOut.executedCount} 项安全建议${batchOut.blockedCount ? ` · 安全护栏拦禁 ${batchOut.blockedCount} 项` : ''}${batchOut.skippedGoalCount ? ` · 保留高危/结项/全局项 ${batchOut.skippedGoalCount} 项待您亲裁` : ''}`,
|
|
979
|
+
...(batchOut.summaries || []).map(line => ` ${line}`),
|
|
980
|
+
].join('\n'),
|
|
981
|
+
actions: [
|
|
982
|
+
`🏁 一站式执行 ×${batchOut.executedCount}`,
|
|
983
|
+
...(batchOut.blockedCount ? [`🛡️ 红线拦截 ×${batchOut.blockedCount}`] : []),
|
|
984
|
+
],
|
|
985
|
+
at: Date.now(),
|
|
986
|
+
})
|
|
987
|
+
if (ws.chat.length > CHAT_CAP) ws.chat = ws.chat.slice(-CHAT_CAP)
|
|
988
|
+
})
|
|
989
|
+
}
|
|
897
990
|
}
|
|
898
991
|
} catch {}
|
|
899
992
|
}
|
|
@@ -935,6 +1028,7 @@ export async function runTurnReview(deps, { agent, sessionId, turn, force = fals
|
|
|
935
1028
|
if (autoPilot && !interventionMessage && autoRelays.length === 0 && !circuitTripped) {
|
|
936
1029
|
autoPilotStopRes = await checkAndStopIdleAutoPilot(deps, rootId, {
|
|
937
1030
|
allowStartupGrace: false,
|
|
1031
|
+
goalAchievedFastStop: Boolean(isGoalAchieved),
|
|
938
1032
|
directory,
|
|
939
1033
|
fromViewCheck: false,
|
|
940
1034
|
}).catch(() => null)
|
|
@@ -955,6 +1049,10 @@ export async function runTurnReview(deps, { agent, sessionId, turn, force = fals
|
|
|
955
1049
|
smartCompacted,
|
|
956
1050
|
circuitTripped,
|
|
957
1051
|
circuitReason,
|
|
1052
|
+
...(res.thinkingEffort ? { thinkingEffort: res.thinkingEffort } : {}),
|
|
1053
|
+
...(res.thinkingEffortMode ? { thinkingEffortMode: res.thinkingEffortMode } : {}),
|
|
1054
|
+
...(dreamRoamRes ? { dreamRoam: dreamRoamRes } : {}),
|
|
1055
|
+
...(oneStopReport ? { oneStopReport } : {}),
|
|
958
1056
|
...(autoPilotStopRes ? { autoPilotStopped: Boolean(autoPilotStopRes.stopped) } : {}),
|
|
959
1057
|
}
|
|
960
1058
|
} finally {
|
|
@@ -963,10 +1061,13 @@ export async function runTurnReview(deps, { agent, sessionId, turn, force = fals
|
|
|
963
1061
|
}
|
|
964
1062
|
|
|
965
1063
|
/**
|
|
966
|
-
*
|
|
967
|
-
*
|
|
968
|
-
*
|
|
969
|
-
*
|
|
1064
|
+
* 5-Condition Idle Auto-Sleep Guard for Managed mode (`runMode === 'managed'`):
|
|
1065
|
+
* Auto-stands down `managed -> off` ONLY when ALL 5 conditions hold:
|
|
1066
|
+
* (1) Idle timeout elapsed (or goal-achieved fast stand-down) AND 0 running session agents;
|
|
1067
|
+
* (2) 0 `doing` tasks on board;
|
|
1068
|
+
* (3) 0 pending approvals / elevations;
|
|
1069
|
+
* (4) 0 pending/delivered open inquiries;
|
|
1070
|
+
* (5) 0 pending review timers AND empty `recentlyUnpinnedWatch` queue.
|
|
970
1071
|
*/
|
|
971
1072
|
export async function checkAndStopIdleAutoPilot(deps, sessionId, opts = {}) {
|
|
972
1073
|
const sidRaw = String(sessionId || '').trim()
|
|
@@ -980,12 +1081,39 @@ export async function checkAndStopIdleAutoPilot(deps, sessionId, opts = {}) {
|
|
|
980
1081
|
const stBefore = loadChiefState()
|
|
981
1082
|
const effBefore = resolveWorkspaceConfig(stBefore, rootId)
|
|
982
1083
|
const wsBefore = ensureWorkspace(stBefore, rootId)
|
|
983
|
-
if (effBefore.stopped || effBefore.
|
|
984
|
-
return { ok: true, stopped: false, reason: 'not-
|
|
1084
|
+
if (effBefore.stopped || effBefore.runMode !== 'managed') {
|
|
1085
|
+
return { ok: true, stopped: false, reason: 'not-managed' }
|
|
985
1086
|
}
|
|
1087
|
+
// Condition 5a: 0 pending review timers
|
|
986
1088
|
if (reviewTimers.has(rootId)) {
|
|
987
1089
|
return { ok: true, stopped: false, reason: 'review-pending' }
|
|
988
1090
|
}
|
|
1091
|
+
// Condition 3: 0 pending approvals / elevations
|
|
1092
|
+
const hasPendingApr = Array.isArray(wsBefore.pendingApprovals) && wsBefore.pendingApprovals.some(a => a && a.status === 'pending')
|
|
1093
|
+
if (hasPendingApr || wsBefore.pendingElevation) {
|
|
1094
|
+
return { ok: true, stopped: false, reason: 'pending-approvals' }
|
|
1095
|
+
}
|
|
1096
|
+
// Condition 4: 0 pending / delivered inquiries
|
|
1097
|
+
const hasOpenInq = Array.isArray(wsBefore.inquiries) && wsBefore.inquiries.some(q => q && (q.status === 'pending' || q.status === 'delivered'))
|
|
1098
|
+
if (hasOpenInq) {
|
|
1099
|
+
return { ok: true, stopped: false, reason: 'pending-inquiries' }
|
|
1100
|
+
}
|
|
1101
|
+
// Condition 2: 0 doing tasks on board
|
|
1102
|
+
const board = deps && deps.boardForSession && deps.boardFileFor
|
|
1103
|
+
? deps.boardForSession(deps.boardFileFor(rootId))
|
|
1104
|
+
: null
|
|
1105
|
+
const doingTasks = (board && Array.isArray(board.tasks) ? board.tasks : []).filter(t => t && t.status === 'doing')
|
|
1106
|
+
if (doingTasks.length > 0) {
|
|
1107
|
+
return { ok: true, stopped: false, reason: 'tasks-doing' }
|
|
1108
|
+
}
|
|
1109
|
+
// Condition 5b: empty `recentlyUnpinnedWatch` queue
|
|
1110
|
+
try {
|
|
1111
|
+
const sessOpt = archiveTargetForRoot(deps, rootId, { vaultScope: 'session' })
|
|
1112
|
+
const sStore = loadArchiveStore(sessOpt)
|
|
1113
|
+
if (Array.isArray(sStore.recentlyUnpinnedWatch) && sStore.recentlyUnpinnedWatch.length > 0) {
|
|
1114
|
+
return { ok: true, stopped: false, reason: 'unpinned-watch-pending' }
|
|
1115
|
+
}
|
|
1116
|
+
} catch {}
|
|
989
1117
|
|
|
990
1118
|
const hasRunningInSession = (dirObj) => {
|
|
991
1119
|
try {
|
|
@@ -1022,16 +1150,26 @@ export async function checkAndStopIdleAutoPilot(deps, sessionId, opts = {}) {
|
|
|
1022
1150
|
return false
|
|
1023
1151
|
}
|
|
1024
1152
|
|
|
1153
|
+
// Condition 1a: 0 running session agents
|
|
1025
1154
|
const dirInitial = opts.directory || (deps ? await listPeerDirectory(deps, rootId) : { targets: [] })
|
|
1026
1155
|
if (hasRunningInSession(dirInitial)) {
|
|
1027
1156
|
return { ok: true, stopped: false, reason: 'agent-running' }
|
|
1028
1157
|
}
|
|
1029
1158
|
|
|
1030
|
-
|
|
1031
|
-
|
|
1032
|
-
|
|
1033
|
-
|
|
1034
|
-
|
|
1159
|
+
// Condition 1b: idle timeout elapsed (or explicit goal-achieved fast stand-down)
|
|
1160
|
+
const rg = (wsBefore.runtimeGuard && typeof wsBefore.runtimeGuard === 'object') ? wsBefore.runtimeGuard : {}
|
|
1161
|
+
const lastActivity = Math.max(Number(rg.lastActivityAt) || 0, Number(rg.autoPilotStartedAt) || 0)
|
|
1162
|
+
const idleTimeoutMs = Math.max(50, Number(effBefore.managedIdleTimeoutMs) || DEFAULT_MANAGED_IDLE_TIMEOUT_MS)
|
|
1163
|
+
if (!opts.goalAchievedFastStop) {
|
|
1164
|
+
if (opts.allowStartupGrace !== false) {
|
|
1165
|
+
const startedAt = Number(rg.autoPilotStartedAt || 0)
|
|
1166
|
+
const hadTurn = Boolean(rg.autoPilotHadTurn)
|
|
1167
|
+
if (!hadTurn && startedAt > 0 && Date.now() - startedAt < Math.min(15_000, idleTimeoutMs)) {
|
|
1168
|
+
return { ok: true, stopped: false, reason: 'startup-grace' }
|
|
1169
|
+
}
|
|
1170
|
+
}
|
|
1171
|
+
if (lastActivity > 0 && Date.now() - lastActivity < idleTimeoutMs) {
|
|
1172
|
+
return { ok: true, stopped: false, reason: 'idle-timeout-not-reached' }
|
|
1035
1173
|
}
|
|
1036
1174
|
}
|
|
1037
1175
|
|
|
@@ -1068,12 +1206,14 @@ export async function checkAndStopIdleAutoPilot(deps, sessionId, opts = {}) {
|
|
|
1068
1206
|
mutateChiefState((s) => {
|
|
1069
1207
|
const ws = ensureWorkspace(s, rootId)
|
|
1070
1208
|
const effNow = resolveWorkspaceConfig(s, rootId)
|
|
1071
|
-
if (effNow.stopped || effNow.
|
|
1209
|
+
if (effNow.stopped || effNow.runMode !== 'managed') return
|
|
1072
1210
|
if (!ws.config || typeof ws.config !== 'object') ws.config = { ...effNow }
|
|
1211
|
+
ws.config.runMode = 'off'
|
|
1073
1212
|
ws.config.paceMode = 'manual'
|
|
1074
1213
|
ws.config.autoPilot = false
|
|
1075
1214
|
ws.config.autoReview = false
|
|
1076
|
-
if (s.config && s.config.paceMode === 'auto') {
|
|
1215
|
+
if (s.config && (s.config.runMode === 'managed' || s.config.paceMode === 'auto')) {
|
|
1216
|
+
s.config.runMode = 'off'
|
|
1077
1217
|
s.config.paceMode = 'manual'
|
|
1078
1218
|
s.config.autoPilot = false
|
|
1079
1219
|
s.config.autoReview = false
|
|
@@ -1081,8 +1221,9 @@ export async function checkAndStopIdleAutoPilot(deps, sessionId, opts = {}) {
|
|
|
1081
1221
|
ws.chat.push({
|
|
1082
1222
|
id: nextId('chat'),
|
|
1083
1223
|
role: 'chief',
|
|
1084
|
-
|
|
1085
|
-
|
|
1224
|
+
kind: 'receipt',
|
|
1225
|
+
text: '- 【🤖→⏸ 托管自动休眠】连续闲置达阈值且在途任务、待批弹窗、在途问询与卷宗梳理均已清零,已自动将运行模式切换回「⏸ 关停」以节省 Token,随时可再次开启。',
|
|
1226
|
+
actions: ['托管自动休眠'],
|
|
1086
1227
|
at: Date.now(),
|
|
1087
1228
|
})
|
|
1088
1229
|
if (ws.chat.length > CHAT_CAP) ws.chat = ws.chat.slice(-CHAT_CAP)
|