dsh-chief 0.0.0-stage → 1.0.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 +34 -0
- package/LICENSE +661 -0
- package/README.en.md +161 -0
- package/README.md +213 -2
- package/archives.js +1157 -0
- package/chief.js +3216 -0
- package/client.js +11079 -0
- package/cordis.patch.yml +3 -0
- package/digest.js +509 -0
- package/handoff.js +481 -0
- package/icon.svg +52 -0
- package/idle-guard.js +536 -0
- package/index.js +4453 -0
- package/locale/en.json +6 -0
- package/locale/zh.json +6 -0
- package/memory.js +515 -0
- package/package.json +77 -3
- package/screenshots.json +9 -0
package/cordis.patch.yml
ADDED
package/digest.js
ADDED
|
@@ -0,0 +1,509 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The automatic half of dsh-chief.
|
|
3
|
+
*
|
|
4
|
+
* Three jobs, all meant to survive compaction without the model being reminded:
|
|
5
|
+
*
|
|
6
|
+
* 1. renderDigest(board, extras) — a small, budgeted section injected into every
|
|
7
|
+
* prompt assembly, so the objective, the plan state, DAG task graph, subagent
|
|
8
|
+
* team status, and anchor notes are in context even after compaction.
|
|
9
|
+
* 2. Subagent & Team awareness — when running inside a subagent tree, tells the
|
|
10
|
+
* root agent which DAG leaf tasks are unlocked for parallel dispatch, and tells
|
|
11
|
+
* each child subagent which task on the shared root board belongs to it.
|
|
12
|
+
* 3. planFromTodos / objectiveFromGoal — mirror the model's own working plan
|
|
13
|
+
* (`todo_write`, `create_goal`, `update_goal`) into the board.
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
const DIGEST_BUDGET = 2500
|
|
17
|
+
|
|
18
|
+
function ownerName(id) {
|
|
19
|
+
const text = String(id || '')
|
|
20
|
+
return text.indexOf('session-') === 0 ? '会话 ' + text.slice(8, 16) : text
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
function shortAgentLabel(name) {
|
|
24
|
+
const raw = String(name || '').trim()
|
|
25
|
+
if (!raw) return ''
|
|
26
|
+
return raw
|
|
27
|
+
.split('/')
|
|
28
|
+
.map(seg => seg.replace(/[-_]([0-9a-f]{8,40})$/i, (_, hex) => '·' + hex.slice(0, 4)))
|
|
29
|
+
.join('/')
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
function boundsLines(board) {
|
|
33
|
+
const bounds = (board && board.bounds) || {}
|
|
34
|
+
const pinned = (board && board.pinnedBounds) || {}
|
|
35
|
+
const pinSet = (col) => new Set(Array.isArray(pinned[col]) ? pinned[col].map(s => String(s).trim()) : [])
|
|
36
|
+
const fmtItem = (col, s, limit = 75) => {
|
|
37
|
+
const clean = String(s || '').trim()
|
|
38
|
+
return (pinSet(col).has(clean) ? '📌' : '') + clamp(clean, limit)
|
|
39
|
+
}
|
|
40
|
+
const forbidden = Array.isArray(bounds.forbidden) ? bounds.forbidden.filter(Boolean) : []
|
|
41
|
+
const allowed = Array.isArray(bounds.allowed) ? bounds.allowed.filter(Boolean) : []
|
|
42
|
+
const notes = Array.isArray(bounds.notes) ? bounds.notes.filter(Boolean) : []
|
|
43
|
+
if (!forbidden.length && !allowed.length && !notes.length) return []
|
|
44
|
+
const lines = ['⚖️ 当前生效边界(📌=全局定住跨任务铁律,其余为本任务边界;全体主子代理必须严守):']
|
|
45
|
+
if (forbidden.length) lines.push(' 🚫 禁止红线: ' + forbidden.slice(0, 6).map(s => fmtItem('forbidden', s)).join(' ; '))
|
|
46
|
+
if (allowed.length) lines.push(' ✅ 允许范围: ' + allowed.slice(0, 5).map(s => fmtItem('allowed', s)).join(' ; '))
|
|
47
|
+
if (notes.length) lines.push(' 💡 边界注意: ' + notes.slice(0, 4).map(s => fmtItem('notes', s)).join(' ; '))
|
|
48
|
+
return lines
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
const ANCHOR_TAG = 'anchor'
|
|
52
|
+
const ANCHOR_BUDGET = 520
|
|
53
|
+
const ANCHOR_RESERVE = 300
|
|
54
|
+
|
|
55
|
+
const MEMORY_SIGNAL = [
|
|
56
|
+
'已修', '已改', '已部署', '根因', '结论', '验证', '实测', 'PASS', 'FAIL', '失败',
|
|
57
|
+
'commit', 'node ', 'npm ', 'git ', 'http', '\\', '/', '文件', '路径', '命令',
|
|
58
|
+
]
|
|
59
|
+
const MEMORY_NOISE = ['随时', '谢谢', '希望', '祝你', '不客气', '你现在看到', '我在这里', '有什么可以']
|
|
60
|
+
|
|
61
|
+
function memoryScore(item) {
|
|
62
|
+
const text = String((item && (item.title || item.text)) || '')
|
|
63
|
+
let score = 0
|
|
64
|
+
for (const word of MEMORY_SIGNAL) if (text.includes(word)) score += 2
|
|
65
|
+
for (const word of MEMORY_NOISE) if (text.includes(word)) score -= 4
|
|
66
|
+
if (/[0-9]/.test(text)) score += 1
|
|
67
|
+
if (item && (item.section === 'decision' || item.section === 'verify')) score += 2
|
|
68
|
+
if (text.length >= 14) score += 1
|
|
69
|
+
return score
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
function memoryLines(board, limit) {
|
|
73
|
+
const pool = (board.memory || [])
|
|
74
|
+
.slice(-Math.max(limit * 3, 10))
|
|
75
|
+
.filter(item => item && (item.title || item.text))
|
|
76
|
+
if (!pool.length) return []
|
|
77
|
+
const ranked = pool
|
|
78
|
+
.map((item, index) => ({ item, index, score: memoryScore(item) }))
|
|
79
|
+
.sort((a, b) => b.score - a.score || b.index - a.index)
|
|
80
|
+
.slice(0, limit)
|
|
81
|
+
.sort((a, b) => b.index - a.index)
|
|
82
|
+
.map(entry => entry.item)
|
|
83
|
+
const open = ranked.filter(item => item.section === 'open')
|
|
84
|
+
const rest = ranked.filter(item => item.section !== 'open')
|
|
85
|
+
const ordered = open.concat(rest)
|
|
86
|
+
return ['记忆(本对话最近的结论 ' + ranked.length + ' 条)'].concat(
|
|
87
|
+
ordered.map(item => ' · [' + sectionLabel(item.section) + '] ' + clamp(item.title || item.text, 60)),
|
|
88
|
+
)
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
function sectionLabel(key) {
|
|
92
|
+
const labels = { progress: '目标与进展', decision: '决策与结论', verify: '门禁与验证', open: '未完成与风险', other: '其他要点' }
|
|
93
|
+
return labels[key] || '其他要点'
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
function clamp(text, limit) {
|
|
97
|
+
const value = String(text === undefined || text === null ? '' : text).replace(/\s+/g, ' ').trim()
|
|
98
|
+
return value.length > limit ? value.slice(0, limit) + '…' : value
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
function planLines(board, limit) {
|
|
102
|
+
const steps = Array.isArray(board.plan) ? board.plan : []
|
|
103
|
+
const tasks = Array.isArray(board.tasks) ? board.tasks : []
|
|
104
|
+
if (!steps.length) {
|
|
105
|
+
if (tasks.length) {
|
|
106
|
+
return [
|
|
107
|
+
'规划: 已启用任务图模式(共 ' + tasks.length + ' 个任务节点)。可用 graph.plan 批量维护层级(parentId)与依赖(blockedBy),或用 task.dispatch 派发子代理。',
|
|
108
|
+
]
|
|
109
|
+
}
|
|
110
|
+
return [
|
|
111
|
+
'规划: 空 —— 复杂或多步任务请先用 chief_board 的 graph.plan 规划可视化任务图(支持 parentId 拆解与 blockedBy 依赖),或用 plan.set / todo_write 写步骤。',
|
|
112
|
+
]
|
|
113
|
+
}
|
|
114
|
+
const done = steps.filter(step => step.done).length
|
|
115
|
+
const open = steps.filter(step => !step.done)
|
|
116
|
+
const shown = open.slice(0, limit)
|
|
117
|
+
const lines = ['规划 ' + done + '/' + steps.length + (open.length > shown.length ? '(未完成 ' + open.length + ',此处列前 ' + shown.length + ')' : '') + ':']
|
|
118
|
+
for (const step of shown) lines.push(' [ ] ' + clamp(step.text, 90))
|
|
119
|
+
lines.push(
|
|
120
|
+
' ↳ 每轮先按顺序读这一段:做完的步 plan.set 标 done;新发现的步补进去;'
|
|
121
|
+
+ '不再做的旧步删掉;需要拆解子代理并行时用 graph.plan 建任务图并用 task.dispatch 派发。',
|
|
122
|
+
)
|
|
123
|
+
return lines
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
function modeLine(board) {
|
|
127
|
+
const mode = String((board && board.mode) || 'plan')
|
|
128
|
+
if (mode === 'goal') {
|
|
129
|
+
return '模式: goal —— 这活是一个持续目标,不是一串步骤。每轮只回答「目标有没有真的前进」,'
|
|
130
|
+
+ '没前进就明说没前进并写清卡在哪;要改目标用 objective.set,别用步骤列表代替目标。'
|
|
131
|
+
}
|
|
132
|
+
return ''
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
function matchAgentTask(task, identity) {
|
|
136
|
+
if (!task || !identity || task.status === 'done') return false
|
|
137
|
+
const self = String(identity.self || '').toLowerCase()
|
|
138
|
+
const shortSelf = self.split('/').pop()
|
|
139
|
+
const owner = String(task.owner || '').toLowerCase()
|
|
140
|
+
const shortOwner = owner.split('/').pop()
|
|
141
|
+
if (self && owner && (owner === self || shortOwner === shortSelf)) return true
|
|
142
|
+
if (identity.sessionId && task.session && String(task.session) === String(identity.sessionId)) return true
|
|
143
|
+
return false
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
function subagentHeaderLines(board, extras) {
|
|
147
|
+
const lines = []
|
|
148
|
+
const identity = extras && extras.agentIdentity ? extras.agentIdentity : null
|
|
149
|
+
const subagents = Array.isArray(extras && extras.subagents) ? extras.subagents : []
|
|
150
|
+
const tasks = Array.isArray(board && board.tasks) ? board.tasks : []
|
|
151
|
+
const byId = new Map(tasks.map(t => [t.id, t]))
|
|
152
|
+
|
|
153
|
+
if (identity && identity.isChild) {
|
|
154
|
+
lines.push(
|
|
155
|
+
'🤖 子代理协同身份: 你是子代理 `' + identity.self + '`(上级 `' + (identity.parent || 'lead') + '`,与主代理共享同一份根任务图)。',
|
|
156
|
+
)
|
|
157
|
+
const mine = tasks.filter(t => matchAgentTask(t, identity))
|
|
158
|
+
if (mine.length) {
|
|
159
|
+
for (const t of mine.slice(0, 3)) {
|
|
160
|
+
const upEvidences = (Array.isArray(t.blockedBy) ? t.blockedBy : [])
|
|
161
|
+
.map(depId => byId.get(depId))
|
|
162
|
+
.filter(dep => dep && dep.evidence)
|
|
163
|
+
.map(dep => '#' + dep.id + ':' + clamp(dep.evidence, 60))
|
|
164
|
+
lines.push(
|
|
165
|
+
' 🎯 指派给你的任务: [' + t.status + '] #' + t.id + ' ' + clamp(t.title, 70)
|
|
166
|
+
+ (t.intent ? ' | 意图: ' + clamp(t.intent, 70) : '')
|
|
167
|
+
+ (upEvidences.length ? ' | 上游成果: ' + upEvidences.join(' ; ') : ''),
|
|
168
|
+
)
|
|
169
|
+
}
|
|
170
|
+
lines.push(
|
|
171
|
+
' ↳ 【强制回写纪律】任务推进/受阻/完成时必须立即调 chief_board({ action: "task.update", id: "' + mine[0].id + '", status: "doing"|"blocked"|"done", evidence: "验证结果" })!需向主代理或队友同步信息必须调 mail.send;工作中若撞墙(权限/沙箱/红线受阻)必须调 bounds.propose 上报参谋长请示用户!',
|
|
172
|
+
)
|
|
173
|
+
} else {
|
|
174
|
+
lines.push(' ↳ 【强制协同纪律】进展变化必须调 chief_board 更新任务与 evidence;跨代理传话必须调 mail.send;工作中撞墙或发现新禁区必须调 bounds.propose 上报参谋长!')
|
|
175
|
+
}
|
|
176
|
+
} else if (subagents.length > 0) {
|
|
177
|
+
const children = subagents.filter(a => a && a.name !== 'lead')
|
|
178
|
+
if (children.length > 0) {
|
|
179
|
+
const running = children.filter(a => a.status === 'running')
|
|
180
|
+
const idle = children.filter(a => a.status !== 'running')
|
|
181
|
+
const shown = [...running.slice(0, 5), ...idle.slice(0, Math.max(0, 4 - running.length))]
|
|
182
|
+
const summary = shown.map(a => {
|
|
183
|
+
const icon = a.status === 'running' ? '🟢' : a.status === 'inactive' ? '⏸' : '⚠'
|
|
184
|
+
return icon + shortAgentLabel(a.name) + (a.taskId ? '(#' + a.taskId + ')' : '')
|
|
185
|
+
}).join(' · ')
|
|
186
|
+
const restCount = Math.max(0, children.length - shown.length)
|
|
187
|
+
lines.push('🤖 子代理团队 (' + running.length + ' 运行中 / 共 ' + children.length + ' 个): ' + summary + (restCount > 0 ? ' · 另有 ' + restCount + ' 个空闲成员' : ''))
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
if (extras && Number(extras.linkedPeersCount) > 0) {
|
|
191
|
+
lines.push('📡 跨窗口协同已开启: 已与 ' + extras.linkedPeersCount + ' 个外部主对话窗口互通,可用 chief_board({ action: "mail.send", target, text }) 跨对话递话。')
|
|
192
|
+
}
|
|
193
|
+
return lines
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
function taskLines(board, limit) {
|
|
197
|
+
const tasks = Array.isArray(board.tasks) ? board.tasks : []
|
|
198
|
+
const open = tasks.filter(task => task.status !== 'done')
|
|
199
|
+
if (!open.length) return []
|
|
200
|
+
const doneIds = new Set(tasks.filter(task => task.status === 'done').map(task => task.id))
|
|
201
|
+
const order = { doing: 0, blocked: 1, todo: 2 }
|
|
202
|
+
const sorted = open.slice().sort((a, b) => (order[a.status] ?? 3) - (order[b.status] ?? 3))
|
|
203
|
+
const shown = sorted.slice(0, limit)
|
|
204
|
+
const doneCount = tasks.length - open.length
|
|
205
|
+
const lines = ['任务图 (' + open.length + ' 未完 / ' + doneCount + ' 已完):']
|
|
206
|
+
|
|
207
|
+
const childrenOf = {}
|
|
208
|
+
for (const task of tasks) {
|
|
209
|
+
const pid = task.parentId
|
|
210
|
+
if (pid && pid !== ' ') {
|
|
211
|
+
if (!childrenOf[pid]) childrenOf[pid] = []
|
|
212
|
+
childrenOf[pid].push(task)
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
// blockedBy 全部 done 的 blocked 任务可以自动或手动解锁
|
|
217
|
+
const ready = open.filter(task =>
|
|
218
|
+
task.status === 'blocked' && Array.isArray(task.blockedBy) && task.blockedBy.length
|
|
219
|
+
&& task.blockedBy.every(id => doneIds.has(id)))
|
|
220
|
+
|
|
221
|
+
// 无阻塞且无未完成子任务的叶子 todo 节点 -> 可立即并行派发给子代理
|
|
222
|
+
const dispatchable = open.filter(task => {
|
|
223
|
+
if (task.status !== 'todo') return false
|
|
224
|
+
const deps = Array.isArray(task.blockedBy) ? task.blockedBy : []
|
|
225
|
+
if (deps.some(id => !doneIds.has(id))) return false
|
|
226
|
+
const kids = childrenOf[task.id] || []
|
|
227
|
+
if (kids.some(k => k.status !== 'done')) return false
|
|
228
|
+
return true
|
|
229
|
+
})
|
|
230
|
+
|
|
231
|
+
// 子任务全部 done 但父任务还挂着的——父任务可以标记 done 了。
|
|
232
|
+
const parentReady = open.filter(task => {
|
|
233
|
+
const kids = childrenOf[task.id]
|
|
234
|
+
return Array.isArray(kids) && kids.length && kids.every(k => k.status === 'done')
|
|
235
|
+
})
|
|
236
|
+
|
|
237
|
+
const STALE_TODO_MS = 10 * 60 * 1000
|
|
238
|
+
const staleTodo = open.filter(task => {
|
|
239
|
+
if (task.status !== 'todo') return false
|
|
240
|
+
const at = task.at || task.updatedAt || task.createdAt
|
|
241
|
+
if (!at) return false
|
|
242
|
+
const age = Date.now() - new Date(at).getTime()
|
|
243
|
+
return age > STALE_TODO_MS
|
|
244
|
+
})
|
|
245
|
+
|
|
246
|
+
for (const task of shown) {
|
|
247
|
+
const owner = task.owner ? ' @' + clamp(task.owner, 18) : ''
|
|
248
|
+
const parentMark = task.parentId ? ' [父:' + task.parentId + ']' : ''
|
|
249
|
+
const unresolvedDeps = (Array.isArray(task.blockedBy) ? task.blockedBy : []).filter(id => !doneIds.has(id))
|
|
250
|
+
const deps = unresolvedDeps.length && task.status !== 'done'
|
|
251
|
+
? ' [等 ' + unresolvedDeps.join(',') + ']'
|
|
252
|
+
: ''
|
|
253
|
+
const unverified = (Array.isArray(task.assumptions) ? task.assumptions : []).filter(a => !a.verified).length
|
|
254
|
+
const assumMark = unverified > 0 ? ' ⚠' + unverified + '假设' : ''
|
|
255
|
+
lines.push(' [' + task.status + (task.priority === 'high' ? '!' : '') + '] #' + task.id + ' ' + clamp(task.title, 72) + owner + parentMark + deps + assumMark)
|
|
256
|
+
}
|
|
257
|
+
if (open.length > shown.length) lines.push(' …还有 ' + (open.length - shown.length) + ' 条,用 chief_board snapshot 取全量')
|
|
258
|
+
if (ready.length) {
|
|
259
|
+
lines.push(' 🔓 前置依赖已全部完成,可立即解锁:' + ready.slice(0, 3).map(task => '#' + task.id + ' ' + clamp(task.title, 34)).join(' · ')
|
|
260
|
+
+ ' —— 调 task.dispatch 派发或 task.update 改 doing')
|
|
261
|
+
}
|
|
262
|
+
if (dispatchable.length) {
|
|
263
|
+
lines.push(' 🚀 就绪可并行派发节点 (' + dispatchable.length + ' 个):' + dispatchable.slice(0, 4).map(t => '#' + t.id + ' ' + clamp(t.title, 28) + (t.owner ? '(@' + t.owner + ')' : '')).join(' · ')
|
|
264
|
+
+ ' —— 可用 chief_board({ action: "task.dispatch", id, agent }) 一键派给子代理')
|
|
265
|
+
}
|
|
266
|
+
if (parentReady.length) {
|
|
267
|
+
lines.push(' 📦 子任务已全部完成,父任务可收尾:' + parentReady.slice(0, 3).map(task => '#' + task.id + ' ' + clamp(task.title, 34)).join(' · ')
|
|
268
|
+
+ ' —— 用 task.update 把父任务标 done 并汇总 evidence')
|
|
269
|
+
}
|
|
270
|
+
if (staleTodo.length) {
|
|
271
|
+
const min = Math.floor((Date.now() - new Date(staleTodo[0].at || staleTodo[0].updatedAt || staleTodo[0].createdAt).getTime()) / 60000)
|
|
272
|
+
lines.push(' ⏰ todo 任务挂了 ' + min + ' 分钟没动:' + staleTodo.slice(0, 3).map(task => '#' + task.id + ' ' + clamp(task.title, 34)).join(' · ')
|
|
273
|
+
+ ' —— 认领/派发/删除,别让它挂着')
|
|
274
|
+
}
|
|
275
|
+
return lines
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
function stampOf(value) {
|
|
279
|
+
if (!value) return ''
|
|
280
|
+
const date = new Date(value)
|
|
281
|
+
if (Number.isNaN(date.getTime())) return ''
|
|
282
|
+
const pad = number => String(number).padStart(2, '0')
|
|
283
|
+
return pad(date.getMonth() + 1) + '-' + pad(date.getDate()) + ' ' + pad(date.getHours()) + ':' + pad(date.getMinutes())
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
function cursorLines(board) {
|
|
287
|
+
const cursor = board && board.cursor ? board.cursor : null
|
|
288
|
+
const step = clamp(cursor && cursor.step, 120)
|
|
289
|
+
const next = clamp(cursor && cursor.next, 160)
|
|
290
|
+
const blocked = clamp(cursor && cursor.blocked, 120)
|
|
291
|
+
if (!step && !next && !blocked) {
|
|
292
|
+
const open = (Array.isArray(board && board.plan) ? board.plan : []).filter(item => !item.done)
|
|
293
|
+
if (!open.length) return []
|
|
294
|
+
return ['断点: 空 —— 做到哪一步、下一句做什么、卡在哪,一样都没写。规划每往前走一步必须 chief_board cursor.set 记下这三句;换会话或被压缩之后,只有这一段能接上。']
|
|
295
|
+
}
|
|
296
|
+
const at = stampOf(cursor.at)
|
|
297
|
+
const lines = ['现在在哪(断点' + (at ? ',' + at : ',未标时间') + '):']
|
|
298
|
+
if (step) lines.push(' 做到: ' + step)
|
|
299
|
+
if (next) lines.push(' 下一句就做: ' + next)
|
|
300
|
+
if (blocked) lines.push(' 卡在: ' + blocked)
|
|
301
|
+
if (at) {
|
|
302
|
+
const hours = Math.floor((Date.now() - new Date(cursor.at).getTime()) / 3600000)
|
|
303
|
+
if (hours >= 2) lines.push(' ⚠ 断点已停留 ' + hours + ' 小时,确认一下是不是该更新 cursor.set 了')
|
|
304
|
+
}
|
|
305
|
+
return lines
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
function takenOutArchiveLines(extras, limit, budget) {
|
|
309
|
+
const taken = extras && Array.isArray(extras.takenOutArchives) ? extras.takenOutArchives : []
|
|
310
|
+
// Strictly ZERO auto-injection when cold/sealed: only items explicitly taken out onto this session desk appear.
|
|
311
|
+
if (!taken.length) return []
|
|
312
|
+
const shown = taken.slice(0, limit || 4)
|
|
313
|
+
const header = '📎 当前主动取出的工程档案 ' + taken.length + ' 份(用户从档案柜取出备查):'
|
|
314
|
+
const total = Number.isFinite(budget) ? Math.max(200, budget) : ANCHOR_BUDGET
|
|
315
|
+
const build = perItem =>
|
|
316
|
+
[header].concat(
|
|
317
|
+
shown.map(item => {
|
|
318
|
+
const pts = (Array.isArray(item.points) ? item.points : []).slice(0, 3).map(p => clamp(p.title, 26)).join(' / ')
|
|
319
|
+
const bodyBit = clamp(item.summary || item.body || '', perItem)
|
|
320
|
+
return ' · 《' + clamp(item.title || '未命名', 36) + '》: ' + bodyBit + (pts ? ' [要点: ' + pts + ']' : '')
|
|
321
|
+
}),
|
|
322
|
+
)
|
|
323
|
+
let perItem = 180
|
|
324
|
+
let lines = build(perItem)
|
|
325
|
+
while (lines.join('\n').length > total && perItem > 60) {
|
|
326
|
+
perItem = Math.max(60, Math.floor(perItem * 0.72))
|
|
327
|
+
lines = build(perItem)
|
|
328
|
+
}
|
|
329
|
+
return lines
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
function anchorLines(board, limit, budget) {
|
|
333
|
+
const notes = Array.isArray(board.notes) ? board.notes : []
|
|
334
|
+
const anchors = notes.filter(note => (note.tags || []).some(tag => String(tag).toLowerCase() === ANCHOR_TAG))
|
|
335
|
+
if (!anchors.length) return []
|
|
336
|
+
const ranked = anchors.slice().sort((a, b) => Number(!!b.pinned) - Number(!!a.pinned))
|
|
337
|
+
const shown = ranked.slice(0, limit)
|
|
338
|
+
const header =
|
|
339
|
+
'锚点笔记 ' + anchors.length +
|
|
340
|
+
(shown.length < anchors.length ? '(此处 ' + shown.length + ' 条,全文用 chief_board snapshot)' : '(压缩后仍保留的硬事实)') + ':'
|
|
341
|
+
const total = Number.isFinite(budget) ? Math.max(180, budget) : ANCHOR_BUDGET
|
|
342
|
+
const build = perNote =>
|
|
343
|
+
[header].concat(shown.map(note => ' · ' + clamp(note.title || '(无标题)', 34) + ': ' + clamp(note.body, perNote)))
|
|
344
|
+
let perNote = 200
|
|
345
|
+
let lines = build(perNote)
|
|
346
|
+
while (lines.join('\n').length > total && perNote > 70) {
|
|
347
|
+
perNote = Math.max(70, Math.floor(perNote * 0.7))
|
|
348
|
+
lines = build(perNote)
|
|
349
|
+
}
|
|
350
|
+
return lines
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
function logLines(board, limit) {
|
|
354
|
+
const log = Array.isArray(board.log) ? board.log : []
|
|
355
|
+
if (!log.length) return []
|
|
356
|
+
return ['最近交接:'].concat(
|
|
357
|
+
log.slice(-limit).map(entry => {
|
|
358
|
+
const stamp = new Date(entry.at)
|
|
359
|
+
const hh = String(stamp.getHours()).padStart(2, '0')
|
|
360
|
+
const mm = String(stamp.getMinutes()).padStart(2, '0')
|
|
361
|
+
return ' ' + hh + ':' + mm + ' ' + clamp(entry.by || '?', 12) + ': ' + clamp(entry.text, 90)
|
|
362
|
+
}),
|
|
363
|
+
)
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
function teamTaskLine(task) {
|
|
367
|
+
const status = task.status === 'in_progress' ? 'doing' : task.status === 'completed' ? 'done' : task.status || 'pending'
|
|
368
|
+
const owner = task.ownerName ? ' → ' + clamp(task.ownerName, 20) : ''
|
|
369
|
+
const blocked = (Array.isArray(task.blockedBy) ? task.blockedBy : []).length && task.status !== 'completed'
|
|
370
|
+
? ' [等 ' + task.blockedBy.join(',') + ']'
|
|
371
|
+
: ''
|
|
372
|
+
return ' [' + status + '] ' + clamp(task.id, 12) + ' ' + clamp(task.subject, 60) + owner + blocked
|
|
373
|
+
}
|
|
374
|
+
|
|
375
|
+
function teamTaskLines(tasks, max) {
|
|
376
|
+
if (!Array.isArray(tasks) || !tasks.length) return []
|
|
377
|
+
const open = tasks.filter(t => t.status !== 'completed' && t.status !== 'deleted')
|
|
378
|
+
const done = tasks.filter(t => t.status === 'completed')
|
|
379
|
+
const lines = ['团队共享任务 ' + open.length + ' 项' + (done.length ? '(' + done.length + ' 已完成)' : '') + ':']
|
|
380
|
+
for (const t of open.slice(0, max || 8)) lines.push(teamTaskLine(t))
|
|
381
|
+
if (open.length > (max || 8)) lines.push(' …另有 ' + (open.length - (max || 8)) + ' 项(调 team_task_list 看全量)')
|
|
382
|
+
return lines
|
|
383
|
+
}
|
|
384
|
+
|
|
385
|
+
/** The injected section. Never throws: a broken digest must not break a prompt. */
|
|
386
|
+
export function renderDigest(board, extras) {
|
|
387
|
+
try {
|
|
388
|
+
const head = ['## 参谋看板(DSH 参谋长指挥看板 · 父子代理与跨对话互通)']
|
|
389
|
+
const mode = modeLine(board)
|
|
390
|
+
if (mode) head.push(mode)
|
|
391
|
+
if (board.objective) {
|
|
392
|
+
const owner = board.objectiveBy ? '(' + ownerName(board.objectiveBy) + ' 设定)' : ''
|
|
393
|
+
head.push('目标' + owner + ': ' + clamp(board.objective, 200))
|
|
394
|
+
}
|
|
395
|
+
const bLines = boundsLines(board)
|
|
396
|
+
if (bLines.length) head.push(...bLines)
|
|
397
|
+
const subLines = subagentHeaderLines(board, extras)
|
|
398
|
+
if (subLines.length) head.push(...subLines)
|
|
399
|
+
|
|
400
|
+
const blockerTasks = (Array.isArray(board.tasks) ? board.tasks : []).filter(t => t.status === 'blocked')
|
|
401
|
+
if (blockerTasks.length) {
|
|
402
|
+
head.push('⛔ 未解阻塞 ' + blockerTasks.length + ' 条:' + blockerTasks.slice(0, 3).map(t => '#' + t.id + ' ' + clamp(t.title, 50)).join(' · '))
|
|
403
|
+
}
|
|
404
|
+
|
|
405
|
+
const tailReserve =
|
|
406
|
+
memoryLines(board, 3).join('\n').length + logLines(board, 3).join('\n').length + 2
|
|
407
|
+
|
|
408
|
+
const hasAnchors = (Array.isArray(board.notes) ? board.notes : []).some(note =>
|
|
409
|
+
(note.tags || []).some(tag => String(tag).toLowerCase() === ANCHOR_TAG),
|
|
410
|
+
)
|
|
411
|
+
|
|
412
|
+
const unreadNotes = (Array.isArray(board.notes) ? board.notes : []).filter(note => !(note.readBy || []).length)
|
|
413
|
+
const unreadLine = unreadNotes.length
|
|
414
|
+
? '📬 未读笔记 ' + unreadNotes.length + ' 条:' + unreadNotes.slice(0, 3).map(n => clamp(n.title || n.body.slice(0, 24), 30)).join(' · ') + (unreadNotes.length > 3 ? ' 等' : '') + '(note.read 打开即标记已读)'
|
|
415
|
+
: ''
|
|
416
|
+
|
|
417
|
+
const sections = [
|
|
418
|
+
{ key: '规划', build: () => planLines(board, 6), hold: ANCHOR_RESERVE },
|
|
419
|
+
{ key: '断点', build: () => cursorLines(board), hold: ANCHOR_RESERVE },
|
|
420
|
+
{ key: '待办', build: () => taskLines(board, 10), hold: ANCHOR_RESERVE },
|
|
421
|
+
{ key: '团队任务', build: () => teamTaskLines(extras && extras.teamTasks, 8), hold: ANCHOR_RESERVE },
|
|
422
|
+
{
|
|
423
|
+
key: '取出档案',
|
|
424
|
+
build: room => takenOutArchiveLines(extras, 4, Math.min(ANCHOR_BUDGET, Math.max(200, room - tailReserve))),
|
|
425
|
+
},
|
|
426
|
+
{ key: '未读笔记', build: () => unreadLine ? [unreadLine] : [], hold: ANCHOR_RESERVE },
|
|
427
|
+
{
|
|
428
|
+
key: '锚点',
|
|
429
|
+
build: room => anchorLines(board, 3, Math.min(ANCHOR_BUDGET, Math.max(180, room - tailReserve))),
|
|
430
|
+
},
|
|
431
|
+
{ key: '记忆', build: () => memoryLines(board, 3) },
|
|
432
|
+
{ key: '交接痕迹', build: () => logLines(board, 3) },
|
|
433
|
+
]
|
|
434
|
+
|
|
435
|
+
const body = []
|
|
436
|
+
const dropped = []
|
|
437
|
+
let used = head.join('\n').length
|
|
438
|
+
for (const section of sections) {
|
|
439
|
+
const hold = hasAnchors ? section.hold || 0 : 0
|
|
440
|
+
const room = DIGEST_BUDGET - used - hold
|
|
441
|
+
if (room < 80) {
|
|
442
|
+
dropped.push(section.key)
|
|
443
|
+
continue
|
|
444
|
+
}
|
|
445
|
+
const lines = section.build(room)
|
|
446
|
+
if (!lines.length) continue
|
|
447
|
+
let block = lines.join('\n')
|
|
448
|
+
if (block.length > room) {
|
|
449
|
+
block = block.slice(0, room - 24) + '\n …(' + section.key + '已截断)'
|
|
450
|
+
dropped.push(section.key + '(截断)')
|
|
451
|
+
}
|
|
452
|
+
body.push(block)
|
|
453
|
+
used += block.length + 1
|
|
454
|
+
}
|
|
455
|
+
|
|
456
|
+
if (!body.length && !board.objective) {
|
|
457
|
+
head.push('板子尚空:启动多步工作前必须先调 chief_board({ action: "graph.plan", objective, tasks }) 建立分层任务图(写明 parentId、blockedBy 与负责 subagent)。')
|
|
458
|
+
}
|
|
459
|
+
head.push(
|
|
460
|
+
'【强制执行铁律 · 严禁只说不调工具】\n'
|
|
461
|
+
+ ' 1. 逢变必记:定目标、拆任务、切换推进(doing)/受阻(blocked)/完成(done)、产出结论或断点时,必须当轮调用 chief_board(graph.plan / task.update / cursor.set / note.write)或 chief_memory 落盘,纯文字口头宣告无效。\n'
|
|
462
|
+
+ ' 2. 逢协必传:派发子代理、给子代理/主代理/联动窗口传话同步关键发现时,必须调用 chief_board 的 task.dispatch / team.steer / mail.send(务必写明目标代理名)。\n'
|
|
463
|
+
+ ' 3. 撞墙必奏(宰相边界机制):执行中若撞墙(权限被拒、沙箱受限、目录禁改、路线死胡同)或用户要求总结工作边界,必须调用 chief_board({ action: "bounds.propose", field: "forbidden"|"allowed"|"notes", item: "简明边界条文", reason: "撞墙原因" }) 呈交 DSH 参谋长,由参谋长请示用户批准后注入全局边界。',
|
|
464
|
+
)
|
|
465
|
+
if (dropped.length) {
|
|
466
|
+
head.push('预算不够,这些段没进:' + dropped.join('、') + '。需要就用 chief_board snapshot 取全量。')
|
|
467
|
+
}
|
|
468
|
+
let text = head.concat(body).join('\n')
|
|
469
|
+
if (text.length > DIGEST_BUDGET + 450) text = text.slice(0, DIGEST_BUDGET + 450) + '\n…(参谋看板摘要已截断,用 chief_board 取全量)'
|
|
470
|
+
return text
|
|
471
|
+
} catch {
|
|
472
|
+
return ''
|
|
473
|
+
}
|
|
474
|
+
}
|
|
475
|
+
|
|
476
|
+
/** todo_write → plan. Returns the mapped steps, or null when nothing usable. */
|
|
477
|
+
export function planFromTodos(argumentsValue) {
|
|
478
|
+
try {
|
|
479
|
+
const args = argumentsValue && typeof argumentsValue === 'object' ? argumentsValue : {}
|
|
480
|
+
const todos = Array.isArray(args.todos) ? args.todos : []
|
|
481
|
+
const steps = todos
|
|
482
|
+
.map((todo, index) => ({
|
|
483
|
+
id: 'todo-' + index,
|
|
484
|
+
text: clamp(todo && todo.content !== undefined ? todo.content : todo, 200),
|
|
485
|
+
done: !!(todo && todo.status === 'completed'),
|
|
486
|
+
}))
|
|
487
|
+
.filter(step => step.text !== '')
|
|
488
|
+
return steps.length ? steps : null
|
|
489
|
+
} catch {
|
|
490
|
+
return null
|
|
491
|
+
}
|
|
492
|
+
}
|
|
493
|
+
|
|
494
|
+
/** create_goal / update_goal → objective. */
|
|
495
|
+
export function objectiveFromGoal(name, argumentsValue) {
|
|
496
|
+
try {
|
|
497
|
+
const args = argumentsValue && typeof argumentsValue === 'object' ? argumentsValue : {}
|
|
498
|
+
if (name === 'update_goal' && args.action && args.action !== 'edit') return null
|
|
499
|
+
const objective = typeof args.objective === 'string' ? args.objective.trim() : ''
|
|
500
|
+
return objective || null
|
|
501
|
+
} catch {
|
|
502
|
+
return null
|
|
503
|
+
}
|
|
504
|
+
}
|
|
505
|
+
|
|
506
|
+
/** Same content? Used to skip writes that would only churn updatedAt. */
|
|
507
|
+
export function planSignature(steps) {
|
|
508
|
+
return JSON.stringify((steps || []).map(step => [String(step.text || ''), !!step.done]))
|
|
509
|
+
}
|