dsh-dispatch-board 0.2.3 → 0.2.5
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/README.md +9 -1
- package/lib/client.js +425 -36
- package/lib/index.js +578 -11
- package/package.json +1 -1
- package/presets/taskboard.patch.yml +11 -7
package/lib/index.js
CHANGED
|
@@ -118,6 +118,17 @@ export const LEAD_AUTO_MARK = 'lead-dispatch'
|
|
|
118
118
|
* (worker 长跑 / 组长卡死)就不再挡住直聊派活——宁可多一条可见任务,也不让派活无声消失。
|
|
119
119
|
*/
|
|
120
120
|
export const LEAD_IN_FLIGHT_TTL_MS = 30 * 60 * 1000
|
|
121
|
+
/**
|
|
122
|
+
* §1.8(0.2.4):组长把「轮询投递来的任务」置 `doing` = 即将为它启动 worker 的**意图**。
|
|
123
|
+
* 派发闸门只认这条意图一次(认领后删除);超过这个窗口没派发就作废,避免陈旧意图把后续直聊派活
|
|
124
|
+
* 错误地并进旧任务(t-69 事故的根因就是「按组长名下 in-flight 任务盲目去重」)。
|
|
125
|
+
*/
|
|
126
|
+
export const LEAD_DISPATCH_INTENT_MS = 10 * 60 * 1000
|
|
127
|
+
/**
|
|
128
|
+
* §1.8(0.2.4):同一次派发被重复调用(模型重试 / PTC 子调用)时,认领「刚建好、还没挂 worker」
|
|
129
|
+
* 的预建卡片的时间窗;窗口内标题 + note 完全相同 → 复用,不再建第二张。
|
|
130
|
+
*/
|
|
131
|
+
export const LEAD_DISPATCH_DEDUPE_MS = 2 * 60 * 1000
|
|
121
132
|
|
|
122
133
|
/** 看板四列。顺序即 UI 列顺序。 */
|
|
123
134
|
export const STATUSES = ['todo', 'doing', 'done', 'archived']
|
|
@@ -2739,6 +2750,8 @@ export function createLeadAutoState(now = Date.now()) {
|
|
|
2739
2750
|
deduped: 0,
|
|
2740
2751
|
completed: 0,
|
|
2741
2752
|
skipped: 0,
|
|
2753
|
+
// §1.8(0.2.4):派发前强制建卡闸门的计数。
|
|
2754
|
+
aborted: 0,
|
|
2742
2755
|
lastError: '',
|
|
2743
2756
|
lastErrorAt: null,
|
|
2744
2757
|
lastTaskId: null,
|
|
@@ -2761,6 +2774,8 @@ export function normalizeLeadAuto(raw) {
|
|
|
2761
2774
|
deduped: counterOf(source.deduped),
|
|
2762
2775
|
completed: counterOf(source.completed),
|
|
2763
2776
|
skipped: counterOf(source.skipped),
|
|
2777
|
+
// §1.8(0.2.4):老 runtime 没有这个字段 → 0(Snapshot 始终给全)。
|
|
2778
|
+
aborted: counterOf(source.aborted),
|
|
2764
2779
|
lastError: trimmed(source.lastError, 500),
|
|
2765
2780
|
lastErrorAt: asString(source.lastErrorAt) || null,
|
|
2766
2781
|
lastTaskId: asString(source.lastTaskId) || null,
|
|
@@ -2889,10 +2904,452 @@ export function workspacePathForCwd(ctx, cwd) {
|
|
|
2889
2904
|
return raw
|
|
2890
2905
|
}
|
|
2891
2906
|
|
|
2907
|
+
/* ------------------------------------------------------------------ *
|
|
2908
|
+
* §1.8(0.2.4)派发前强制建卡闸门(tools/pre-execute)
|
|
2909
|
+
*
|
|
2910
|
+
* 背景(t-69 事故):旧实现只在 `tools/result`(worker 已经启动之后)观察派发,并且用
|
|
2911
|
+
* `findLeadInFlightTask` 把「组长名下任意一条未陈旧的轮询任务」当成这次 subagent 的归属——
|
|
2912
|
+
* 组长处理完轮询任务 t-68 后,用户在直聊里再让它派活,第二次 subagent 仍被误判成「t-68 的 worker」
|
|
2913
|
+
* 而跳过建卡:worker 跑了、看板没有任务(见 §1.8)。
|
|
2914
|
+
*
|
|
2915
|
+
* 0.2.4 起把建卡提前到派发**之前**:`tools/pre-execute` 拦 `subagent` 调用(只针对
|
|
2916
|
+
* taskboard-dispatch 组长),先认领已有卡片(显式 `任务 id` → 组长刚置 doing 的轮询任务意图 →
|
|
2917
|
+
* 刚建好还没挂 worker 的同参数卡片),认领不到就建一条 `doing` + `autoDispatch` 卡片;
|
|
2918
|
+
* 建卡 / 写盘失败 → `{kind:'deny'}`,worker **不会启动**。`tools/result` 只负责把 worker 会话 id
|
|
2919
|
+
* 挂到这张卡上(`handleSubagentDispatch` 的 prepared 分支),不再决定是否建卡。
|
|
2920
|
+
* ------------------------------------------------------------------ */
|
|
2921
|
+
|
|
2922
|
+
/** 按 id 找任务(找不到返回 null,不抛 not-found——闸门是副作用路径,不该把派发变成异常)。 */
|
|
2923
|
+
export function taskById(state, id) {
|
|
2924
|
+
const target = trimmed(id, 40)
|
|
2925
|
+
if (!state || !Array.isArray(state.tasks) || target === '') return null
|
|
2926
|
+
return state.tasks.find((task) => task && task.id === target) || null
|
|
2927
|
+
}
|
|
2928
|
+
|
|
2929
|
+
/**
|
|
2930
|
+
* 派发 prompt 的行归一化(§1.8,0.2.4 宽容解析):组长(模型)写 worker prompt 习惯给「任务 id」加粗 /
|
|
2931
|
+
* 套反引号 / 用半角冒号,逐行归一后再匹配,避免「同一句话的不同 markdown 写法」漏识别:
|
|
2932
|
+
* 1) 去掉 markdown 强调 / 反引号 `*` `_` `` ` `` `~`(`**任务 id:t-N**` → `任务 id:t-N`);
|
|
2933
|
+
* 2) 全角冒号 `:` → `:`,全角 / 不换行空格 → 普通空格,连续空白压成一个;
|
|
2934
|
+
* 3) 去掉行首缩进与列表 / 标题 / 引用符号(`- ` / `+ ` / `> ` / `1. ` / `## `,可叠加),再 trim。
|
|
2935
|
+
* 只归一「形态」、不放宽「语义」:归一化后仍要求标签在行首(`任务 id:`)或紧跟 `看板任务:`,
|
|
2936
|
+
* 正文段落里顺带提到的 t-N(「上次那个 t-3」「见 t-7」)依旧不认。
|
|
2937
|
+
*/
|
|
2938
|
+
function normalizeDispatchLines(text) {
|
|
2939
|
+
const source = typeof text === 'string' ? text : ''
|
|
2940
|
+
if (source === '') return []
|
|
2941
|
+
return source.split(/\r?\n/).map((raw) =>
|
|
2942
|
+
raw
|
|
2943
|
+
.replace(/[*_`~]/g, '')
|
|
2944
|
+
.replace(/[:﹕]/g, ':')
|
|
2945
|
+
.replace(/[\u00a0\u3000]/g, ' ')
|
|
2946
|
+
.replace(/\s+/g, ' ')
|
|
2947
|
+
.trim()
|
|
2948
|
+
.replace(/^(?:(?:[-+]|#{1,6}|>|\d+[.)]) +)+/, '')
|
|
2949
|
+
.trim(),
|
|
2950
|
+
)
|
|
2951
|
+
}
|
|
2952
|
+
|
|
2953
|
+
/** 一行里出现的任务 id(先去掉括号 / 引号里的标题内容:`任务 id:t-81「…t-71…」` 里的 t-71 是标题)。 */
|
|
2954
|
+
function taskIdsInDispatchLine(text) {
|
|
2955
|
+
const bare = String(text)
|
|
2956
|
+
.replace(/([^)]*)/g, ' ')
|
|
2957
|
+
.replace(/\([^)]*\)/g, ' ')
|
|
2958
|
+
.replace(/「[^」]*」/g, ' ')
|
|
2959
|
+
.replace(/『[^』]*』/g, ' ')
|
|
2960
|
+
.replace(/“[^”]*”/g, ' ')
|
|
2961
|
+
.replace(/\[[^\]]*\]/g, ' ')
|
|
2962
|
+
return bare.match(/t-[0-9]{1,6}(?![0-9])/gi) || []
|
|
2963
|
+
}
|
|
2964
|
+
|
|
2965
|
+
/**
|
|
2966
|
+
* 派发 prompt 里显式声明的看板任务 id(§1.8):`任务 id:t-68` / `- 任务 id: \`t-68\`` → `t-68`。
|
|
2967
|
+
* 只认行首(允许列表符号 / 缩进 / markdown 强调)的「任务 id」标签,避免验证类 prompt 里顺带提到 t-xx
|
|
2968
|
+
* 被误认领。宽容化来自 t-78 事故:组长把 prompt 写成 `**任务 id:t-78**`,旧正则要求行首直接是
|
|
2969
|
+
* `任务`/`- 任务`,`*` 不在列表符号的「符号 + 空格」形态里 → 整行不匹配 → 没复用已有的 t-78,
|
|
2970
|
+
* 另建了重复卡 t-79(已归档)。
|
|
2971
|
+
*/
|
|
2972
|
+
export function dispatchTaskIdFromPrompt(text) {
|
|
2973
|
+
for (const line of normalizeDispatchLines(text)) {
|
|
2974
|
+
const match = /^任务 *id *: *(t-[0-9]{1,6})(?![0-9])/i.exec(line)
|
|
2975
|
+
if (!match) continue
|
|
2976
|
+
const id = match[1].toLowerCase()
|
|
2977
|
+
// 同一行还写了别的任务 id(`任务 id:t-3 或 t-4`)= 归属不明:宁可不认领,也不认错卡片。
|
|
2978
|
+
const rest = line.slice(match[0].length)
|
|
2979
|
+
if (taskIdsInDispatchLine(rest).some((other) => other.toLowerCase() !== id)) continue
|
|
2980
|
+
return id
|
|
2981
|
+
}
|
|
2982
|
+
return ''
|
|
2983
|
+
}
|
|
2984
|
+
|
|
2985
|
+
/**
|
|
2986
|
+
* 派发 prompt 里以「看板任务:t-N」/「看板任务 `t-N`」/「看板任务:**任务 id:t-N**」形式提到的卡片 id
|
|
2987
|
+
* (§1.8,0.2.4)。组长给 worker 的 prompt 不一定写行首「任务 id:」(实测 t-70 写成
|
|
2988
|
+
* 「看板任务:**t-70…**」;t-78 写成「看板任务:**任务 id:t-78**,后面跟标题」——行首是正文,行首规则不认,
|
|
2989
|
+
* 旧「看板任务 + 紧邻 t-N」也不认中间夹着的「任务 id:」,于是另建了重复卡 t-79)。这种明确的任务引用
|
|
2990
|
+
* 也应认领原卡片;是否复用还要过 `canReuseMentionedTask`(待做 / 未挂 worker)。
|
|
2991
|
+
*/
|
|
2992
|
+
export function dispatchReferencedTaskId(text) {
|
|
2993
|
+
for (const line of normalizeDispatchLines(text)) {
|
|
2994
|
+
const match = /看板任务 *:? *(?:任务 *id *: *)?(t-[0-9]{1,6})(?![0-9])/i.exec(line)
|
|
2995
|
+
if (match) return match[1].toLowerCase()
|
|
2996
|
+
}
|
|
2997
|
+
return ''
|
|
2998
|
+
}
|
|
2999
|
+
|
|
3000
|
+
/** assignee 看起来是不是一个真实 worker 会话 id(UUID / session-…);自由文本标签(如 taskboard-worker)不算。 */
|
|
3001
|
+
export function looksLikeSessionId(value) {
|
|
3002
|
+
const text = typeof value === 'string' ? value.trim() : ''
|
|
3003
|
+
if (text === '') return false
|
|
3004
|
+
if (/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i.test(text)) return true
|
|
3005
|
+
return /^session-[0-9a-z-]{8,}$/i.test(text)
|
|
3006
|
+
}
|
|
3007
|
+
|
|
3008
|
+
/**
|
|
3009
|
+
* 「看板任务:t-N」提到的卡片能否被这次派发认领:
|
|
3010
|
+
* - 必须在待做 / 进行中(做完 / 归档的任务不抢);
|
|
3011
|
+
* - 还没挂真实 worker 会话 id(已挂 = 已有 worker 在干,这次多半是验证子 Agent / 另一个 worker,
|
|
3012
|
+
* 应该有自己的卡片;自由文本 assignee 如 `taskboard-worker` 不算)。
|
|
3013
|
+
*/
|
|
3014
|
+
export function canReuseMentionedTask(task) {
|
|
3015
|
+
if (!task) return false
|
|
3016
|
+
if (task.status !== 'todo' && task.status !== 'doing') return false
|
|
3017
|
+
return !looksLikeSessionId(task.assignee)
|
|
3018
|
+
}
|
|
3019
|
+
|
|
3020
|
+
function preparedDispatchStore(rt) {
|
|
3021
|
+
if (!(rt.preparedDispatches instanceof WeakMap)) rt.preparedDispatches = new WeakMap()
|
|
3022
|
+
return rt.preparedDispatches
|
|
3023
|
+
}
|
|
3024
|
+
|
|
3025
|
+
/** 预建卡片的执行句柄(WeakMap 键 = tools 管线的 exec 对象,pre-execute 与 tools/result 是同一个对象)。 */
|
|
3026
|
+
export function takePreparedDispatch(rt, exec) {
|
|
3027
|
+
if (!exec || typeof exec !== 'object') return null
|
|
3028
|
+
const store = preparedDispatchStore(rt)
|
|
3029
|
+
const hit = store.get(exec) || null
|
|
3030
|
+
if (hit) store.delete(exec)
|
|
3031
|
+
return hit
|
|
3032
|
+
}
|
|
3033
|
+
|
|
3034
|
+
/**
|
|
3035
|
+
* §1.8(0.2.4):组长把轮询投递任务置 doing → 记一条「即将为它派 worker」的意图(不落盘)。
|
|
3036
|
+
* 只在任务当前 `assignee === ''`(还没挂 worker)时记录:worker 已经挂上后再改 doing 不算新意图,
|
|
3037
|
+
* 避免把后续直聊派活错误并进旧任务。
|
|
3038
|
+
*/
|
|
3039
|
+
export function noteLeadDispatchIntent(rt, agent, taskId, now = Date.now()) {
|
|
3040
|
+
try {
|
|
3041
|
+
if (!agent) return false
|
|
3042
|
+
if (sessionPresetOfContext(rt.ctx, agent) !== DISPATCH_PRESET_ID) return false
|
|
3043
|
+
const leadId = agentSessionId(agent) || ''
|
|
3044
|
+
if (leadId === '') return false
|
|
3045
|
+
const task = taskById(rt.state, taskId)
|
|
3046
|
+
if (!task) return false
|
|
3047
|
+
if (task.status !== 'doing' || asString(task.notifiedAt) === '') return false
|
|
3048
|
+
if (task.dispatchedSessionId !== leadId) return false
|
|
3049
|
+
if (task.assignee !== '') return false
|
|
3050
|
+
if (!(rt.leadDispatchIntent instanceof Map)) rt.leadDispatchIntent = new Map()
|
|
3051
|
+
rt.leadDispatchIntent.set(leadId, { taskId: task.id, at: now })
|
|
3052
|
+
return true
|
|
3053
|
+
} catch {
|
|
3054
|
+
return false
|
|
3055
|
+
}
|
|
3056
|
+
}
|
|
3057
|
+
|
|
3058
|
+
/** 取走(一次性)该组长的派发意图;过期 / 任务状态不符 → 作废。 */
|
|
3059
|
+
function takeLeadDispatchIntent(rt, leadId, now) {
|
|
3060
|
+
if (!leadId || !(rt.leadDispatchIntent instanceof Map)) return null
|
|
3061
|
+
const entry = rt.leadDispatchIntent.get(leadId)
|
|
3062
|
+
if (!entry) return null
|
|
3063
|
+
rt.leadDispatchIntent.delete(leadId)
|
|
3064
|
+
if (!Number.isFinite(entry.at) || now - entry.at > LEAD_DISPATCH_INTENT_MS) return null
|
|
3065
|
+
const task = taskById(rt.state, entry.taskId)
|
|
3066
|
+
if (!task || task.status !== 'doing' || asString(task.notifiedAt) === '') return null
|
|
3067
|
+
return task
|
|
3068
|
+
}
|
|
3069
|
+
|
|
3070
|
+
/**
|
|
3071
|
+
* 这次派发的 prompt 是否真的指向这条任务(任务 id 或标题出现在 prompt 里)。
|
|
3072
|
+
* 用来防止「组长刚给 t-68 记了派发意图」的**下一条无关直聊派发**把 t-68 认领走(t-69 事故的变体)。
|
|
3073
|
+
*/
|
|
3074
|
+
export function dispatchMentionsTask(args, task) {
|
|
3075
|
+
if (!task) return false
|
|
3076
|
+
const prompt = subagentPromptText(args)
|
|
3077
|
+
if (prompt === '') return false
|
|
3078
|
+
const id = asString(task.id)
|
|
3079
|
+
if (id !== '') {
|
|
3080
|
+
const escaped = id.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')
|
|
3081
|
+
if (new RegExp('(^|[^A-Za-z0-9_-])' + escaped + '([^A-Za-z0-9_-]|$)').test(prompt)) return true
|
|
3082
|
+
}
|
|
3083
|
+
const title = asString(task.title).trim()
|
|
3084
|
+
return title.length >= 4 && prompt.includes(title)
|
|
3085
|
+
}
|
|
3086
|
+
|
|
3087
|
+
/** 刚建好、还没挂 worker 的同参数卡片(同一组长 / 同 title+note / 时间窗内)→ 幂等复用。 */
|
|
3088
|
+
export function findPendingDispatchCard(state, leadId, title, note, now = Date.now()) {
|
|
3089
|
+
if (!state || !Array.isArray(state.tasks) || !leadId) return null
|
|
3090
|
+
for (let i = state.tasks.length - 1; i >= 0; i -= 1) {
|
|
3091
|
+
const task = state.tasks[i]
|
|
3092
|
+
if (!task || task.autoDispatch !== LEAD_AUTO_MARK) continue
|
|
3093
|
+
if (task.status !== 'doing' || task.assignee !== '') continue
|
|
3094
|
+
if (task.dispatchedSessionId !== leadId) continue
|
|
3095
|
+
if (task.title !== title || (task.note || '') !== (note || '')) continue
|
|
3096
|
+
const stamp = Date.parse(asString(task.createdAt))
|
|
3097
|
+
if (!Number.isFinite(stamp) || now - stamp > LEAD_DISPATCH_DEDUPE_MS) continue
|
|
3098
|
+
return task
|
|
3099
|
+
}
|
|
3100
|
+
return null
|
|
3101
|
+
}
|
|
3102
|
+
|
|
3103
|
+
/** 派发卡片正文(0.2.4):pre-execute 时还没有 worker 会话 id,标题先取 description / prompt 首行。 */
|
|
3104
|
+
export function leadDispatchTitle(args) {
|
|
3105
|
+
return leadTaskTitle(args, '')
|
|
3106
|
+
}
|
|
3107
|
+
|
|
3108
|
+
function leadDispatchNote(args) {
|
|
3109
|
+
return truncateNote(subagentPromptText(args), MAX_NOTE_CHARS)
|
|
3110
|
+
}
|
|
3111
|
+
|
|
3112
|
+
function leadDispatchCwd(rt, agent, args) {
|
|
3113
|
+
const sessionId = agentSessionId(agent) || ''
|
|
3114
|
+
const rawCwd = sessionCwd(agent) || resolveSessionCwd(rt.ctx, sessionId) || (isPlainObject(args) ? asString(args.cwd) : '')
|
|
3115
|
+
return workspacePathForCwd(rt.ctx, rawCwd)
|
|
3116
|
+
}
|
|
3117
|
+
|
|
3118
|
+
function removeTaskFromState(state, taskId) {
|
|
3119
|
+
if (!state || !Array.isArray(state.tasks)) return false
|
|
3120
|
+
const index = state.tasks.findIndex((task) => task && task.id === taskId)
|
|
3121
|
+
if (index < 0) return false
|
|
3122
|
+
state.tasks.splice(index, 1)
|
|
3123
|
+
return true
|
|
3124
|
+
}
|
|
3125
|
+
|
|
3126
|
+
function dispatchDeniedMessage(reason, detail) {
|
|
3127
|
+
return (
|
|
3128
|
+
'派发已中止:没有看板卡片就不能启动 worker(§1.8)。' +
|
|
3129
|
+
reason +
|
|
3130
|
+
(detail ? ':' + detail : '') +
|
|
3131
|
+
'。请修复 $DSH_HOME/storages/dsh-taskboard/board.json 的写权限后重试,' +
|
|
3132
|
+
'或先用 taskboard_add 建卡、再把卡号写进 prompt(任务 id:t-N)重试。'
|
|
3133
|
+
)
|
|
3134
|
+
}
|
|
3135
|
+
|
|
3136
|
+
function finishPreparedDispatch(rt, auto, store, exec, task, created, reused, now) {
|
|
3137
|
+
const outcome = { ok: true, taskId: task.id, created, reused }
|
|
3138
|
+
// created 只在闸门真正新建时 +1;handleSubagentDispatch 的 prepared 分支不再重复计数。
|
|
3139
|
+
if (created) auto.created += 1
|
|
3140
|
+
else auto.deduped += 1
|
|
3141
|
+
auto.lastTaskId = task.id
|
|
3142
|
+
auto.updatedAt = iso(now)
|
|
3143
|
+
if (exec && typeof exec === 'object') store.set(exec, outcome)
|
|
3144
|
+
return outcome
|
|
3145
|
+
}
|
|
3146
|
+
|
|
3147
|
+
/**
|
|
3148
|
+
* §1.8(0.2.4):派发前准备看板卡片。
|
|
3149
|
+
* - 能认领已有卡片就复用(显式 `任务 id` / 组长置 doing 的轮询任务 / 同参数预建卡片),不重复建;
|
|
3150
|
+
* - 认领不到就新建 `doing` + `autoDispatch` 卡片并**同步写盘**;
|
|
3151
|
+
* - 建卡 / 写盘失败 → `{ok:false}`(调用方必须拒绝派发),并回滚内存里的半成品卡片。
|
|
3152
|
+
* @param {{agent:object, exec?:object, args?:object}} input
|
|
3153
|
+
* @returns {{ok:true, taskId:string, created:boolean, reused:string} | {ok:false, reason:string, message:string}}
|
|
3154
|
+
*/
|
|
3155
|
+
export function prepareLeadDispatch(rt, input, now = Date.now()) {
|
|
3156
|
+
const auto = ensureLeadAuto(rt)
|
|
3157
|
+
const agent = input && input.agent
|
|
3158
|
+
const exec = input && input.exec
|
|
3159
|
+
const store = preparedDispatchStore(rt)
|
|
3160
|
+
try {
|
|
3161
|
+
if (!agent) return { ok: true, taskId: '', created: false, reused: 'no-agent' }
|
|
3162
|
+
if (sessionPresetOfContext(rt.ctx, agent) !== DISPATCH_PRESET_ID) {
|
|
3163
|
+
return { ok: true, taskId: '', created: false, reused: 'not-lead' }
|
|
3164
|
+
}
|
|
3165
|
+
// 同一 exec 重复进闸门(PTC / 重试):直接返回第一次的结果,绝不再建卡。
|
|
3166
|
+
if (exec && typeof exec === 'object') {
|
|
3167
|
+
const prior = store.get(exec)
|
|
3168
|
+
if (prior) return { ...prior, reused: 'same-exec' }
|
|
3169
|
+
}
|
|
3170
|
+
const leadId = agentSessionId(agent) || ''
|
|
3171
|
+
reloadBoardIfStale(rt)
|
|
3172
|
+
const args = input && input.args
|
|
3173
|
+
const title = leadDispatchTitle(args)
|
|
3174
|
+
const note = leadDispatchNote(args)
|
|
3175
|
+
// 0) 一次性消费「组长刚置 doing」的意图:即使这次靠显式任务 id 认领,也要把它清掉,
|
|
3176
|
+
// 否则陈旧的 t-68 意图会在下一次无关的直聊派发里把旧任务认领走(t-69 事故的变体)。
|
|
3177
|
+
const intent = takeLeadDispatchIntent(rt, leadId, now)
|
|
3178
|
+
// 1) 显式任务 id(prompt 里的「任务 id:t-N」)优先:轮询任务的 worker / 返工都靠它认领。
|
|
3179
|
+
const explicitId = dispatchTaskIdFromPrompt(subagentPromptText(args))
|
|
3180
|
+
const explicit = explicitId === '' ? null : taskById(rt.state, explicitId)
|
|
3181
|
+
if (explicit && explicit.status !== 'archived') {
|
|
3182
|
+
return finishPreparedDispatch(rt, auto, store, exec, explicit, false, 'task-id', now)
|
|
3183
|
+
}
|
|
3184
|
+
// 1b) 「看板任务:t-N」式引用(0.2.4):组长直聊派活时手动建了卡、prompt 用这种写法也要认领,
|
|
3185
|
+
// 否则会为同一件事建两张卡。只认领待做 / 进行中且还没挂真实 worker 的卡片。
|
|
3186
|
+
const referencedId = dispatchReferencedTaskId(subagentPromptText(args))
|
|
3187
|
+
const referenced = referencedId === '' ? null : taskById(rt.state, referencedId)
|
|
3188
|
+
if (referenced && canReuseMentionedTask(referenced)) {
|
|
3189
|
+
return finishPreparedDispatch(rt, auto, store, exec, referenced, false, 'task-id-mention', now)
|
|
3190
|
+
}
|
|
3191
|
+
// 2) 组长刚把某条轮询任务置 doing → 这次派发就是它的 worker(要求 prompt 真的指向那条任务)。
|
|
3192
|
+
if (intent && dispatchMentionsTask(args, intent)) {
|
|
3193
|
+
return finishPreparedDispatch(rt, auto, store, exec, intent, false, 'lead-intent', now)
|
|
3194
|
+
}
|
|
3195
|
+
// 3) 同参数重复调用(模型重试 / PTC 嵌套)→ 复用刚建好还没挂 worker 的卡片。
|
|
3196
|
+
const pending = findPendingDispatchCard(rt.state, leadId, title, note, now)
|
|
3197
|
+
if (pending) return finishPreparedDispatch(rt, auto, store, exec, pending, false, 'pending-duplicate', now)
|
|
3198
|
+
// 4) 新派发:先建卡(doing + autoDispatch),写盘成功才允许启动 worker。
|
|
3199
|
+
const task = addTask(
|
|
3200
|
+
rt.state,
|
|
3201
|
+
{ title, note, source: 'agent', sessionId: leadId || null, cwd: leadDispatchCwd(rt, agent, args) },
|
|
3202
|
+
now,
|
|
3203
|
+
)
|
|
3204
|
+
applyTaskUpdate(rt.state, task.id, { status: 'doing' }, now)
|
|
3205
|
+
task.autoDispatch = LEAD_AUTO_MARK
|
|
3206
|
+
task.dispatchedSessionId = leadId || null
|
|
3207
|
+
task.dispatchedSessionTitle = sessionTitle(agent)
|
|
3208
|
+
task.lastError = ''
|
|
3209
|
+
task.updatedAt = iso(now)
|
|
3210
|
+
const saved = commitBoard(rt.state, rt.filePath)
|
|
3211
|
+
if (!saved || saved.saved === false) {
|
|
3212
|
+
// 卡没落盘 = 不能派发:回滚内存半成品,保持「板上有卡 ⇔ 真派了活」。
|
|
3213
|
+
removeTaskFromState(rt.state, task.id)
|
|
3214
|
+
auto.aborted += 1
|
|
3215
|
+
auto.updatedAt = iso(now)
|
|
3216
|
+
recordLeadAutoError(rt, '派发前建卡写盘失败,已中止派发:' + ((saved && saved.error) || '未知写盘错误'), now)
|
|
3217
|
+
return {
|
|
3218
|
+
ok: false,
|
|
3219
|
+
reason: 'write-failed',
|
|
3220
|
+
message: dispatchDeniedMessage('看板卡片写入失败', (saved && saved.error) || ''),
|
|
3221
|
+
}
|
|
3222
|
+
}
|
|
3223
|
+
console.log('[taskboard] 派发前已建卡:' + task.id + ' ← 组长会话 ' + (leadId || '(unknown)'))
|
|
3224
|
+
return finishPreparedDispatch(rt, auto, store, exec, task, true, 'created', now)
|
|
3225
|
+
} catch (error) {
|
|
3226
|
+
auto.aborted += 1
|
|
3227
|
+
auto.updatedAt = iso(now)
|
|
3228
|
+
recordLeadAutoError(rt, '派发前建卡失败,已中止派发:' + describeError(error), now)
|
|
3229
|
+
return { ok: false, reason: 'error', message: dispatchDeniedMessage('看板卡片准备失败', describeError(error)) }
|
|
3230
|
+
}
|
|
3231
|
+
}
|
|
3232
|
+
|
|
3233
|
+
/** tools/result 里的错误文本:优先 error.message,其次 content 的 text 块(post-execute block 只有 content)。 */
|
|
3234
|
+
function toolResultFailureText(result) {
|
|
3235
|
+
const error = result && result.error
|
|
3236
|
+
if (typeof error === 'string') return flattenResultLine(error, 200)
|
|
3237
|
+
if (isPlainObject(error)) {
|
|
3238
|
+
const message = asString(error.message)
|
|
3239
|
+
if (message !== '') return flattenResultLine(message, 200)
|
|
3240
|
+
const info = error.info
|
|
3241
|
+
if (isPlainObject(info)) {
|
|
3242
|
+
const name = asString(info.name).trim()
|
|
3243
|
+
const code = asString(info.code).trim()
|
|
3244
|
+
if (name !== '' || code !== '') return name + (code === '' ? '' : '(' + code + ')')
|
|
3245
|
+
}
|
|
3246
|
+
}
|
|
3247
|
+
const content = result && result.content
|
|
3248
|
+
if (!Array.isArray(content)) return ''
|
|
3249
|
+
return flattenResultLine(
|
|
3250
|
+
content
|
|
3251
|
+
.map((block) => (block && block.type === 'text' && typeof block.text === 'string' ? block.text : ''))
|
|
3252
|
+
.filter((text) => text !== '')
|
|
3253
|
+
.join(' '),
|
|
3254
|
+
200,
|
|
3255
|
+
)
|
|
3256
|
+
}
|
|
3257
|
+
|
|
3258
|
+
/**
|
|
3259
|
+
* 非 activation 结果的原因摘要(写进卡片 `lastError='派发未启动:<摘要>'`)。
|
|
3260
|
+
* 覆盖 `isError`(被 pre-execute 拒绝 / post-execute block / 取消 / 启动失败)与返回形状未知两类。
|
|
3261
|
+
*/
|
|
3262
|
+
export function dispatchAbortSummary(result) {
|
|
3263
|
+
if (!isPlainObject(result)) return '没有收到工具执行结果'
|
|
3264
|
+
if (result.isError === true) {
|
|
3265
|
+
const detail = toolResultFailureText(result)
|
|
3266
|
+
return detail === '' ? '派发被拒绝或 worker 启动失败' : '工具调用失败:' + detail
|
|
3267
|
+
}
|
|
3268
|
+
const value = result.value
|
|
3269
|
+
if (!isPlainObject(value)) return '工具返回不是 activation(缺少 value)'
|
|
3270
|
+
if (value.kind !== 'activation') {
|
|
3271
|
+
const kind = asString(value.kind).trim()
|
|
3272
|
+
return '工具返回不是 activation' + (kind === '' ? '(缺少 kind)' : '(kind=' + kind + ')')
|
|
3273
|
+
}
|
|
3274
|
+
return 'activation 结果缺少 subagentId'
|
|
3275
|
+
}
|
|
3276
|
+
|
|
3277
|
+
/**
|
|
3278
|
+
* §1.8(0.2.4,t-70 加固):派发没有真正启动 / **无法确认**已启动时,**绝不删除**闸门预建的卡片,
|
|
3279
|
+
* 只把它降级为可见的待开始记录:`status='todo'`、清掉派发字段、`lastError='派发未启动:<原因>'`,
|
|
3280
|
+
* 保留 title / note / cwd / sessionId。复用来的卡片(轮询任务 / 显式 taskId)绝不动。
|
|
3281
|
+
*
|
|
3282
|
+
* 为什么不删:`tools/result` 的非 activation 结果无法 100% 证明 worker 没启动——`tools/post-execute`
|
|
3283
|
+
* 的 `block` 发生在工具体执行之后、取消可能发生在启动之后、provider 返回形状也可能变化;删卡会抹掉
|
|
3284
|
+
* 一张 worker 可能正在使用的卡,重新制造「有活没卡」(与本任务初衷相反)。保守起见一律保留。
|
|
3285
|
+
*
|
|
3286
|
+
* 幂等:prepared 句柄是 WeakMap 一次性消费(同一 exec 第二次进入 = `no-prepared`);任务对象再加
|
|
3287
|
+
* WeakSet 防重复降级,`leadAuto.aborted` 只 +1 一次。
|
|
3288
|
+
* @param {string} [abortReason] 原因摘要(`dispatchAbortSummary(result)` 或调用方文案)
|
|
3289
|
+
* @returns {{released:boolean, taskId?:string, status?:string, reason?:string}}
|
|
3290
|
+
*/
|
|
3291
|
+
export function releasePreparedDispatch(rt, exec, abortReason = '', now = Date.now()) {
|
|
3292
|
+
const prepared = takePreparedDispatch(rt, exec)
|
|
3293
|
+
if (!prepared) return { released: false, reason: 'no-prepared' }
|
|
3294
|
+
if (prepared.created !== true) return { released: false, taskId: prepared.taskId, reason: 'reused' }
|
|
3295
|
+
try {
|
|
3296
|
+
reloadBoardIfStale(rt)
|
|
3297
|
+
const task = taskById(rt.state, prepared.taskId)
|
|
3298
|
+
if (!task) return { released: false, reason: 'missing' }
|
|
3299
|
+
if (!(rt.releasedDispatchTasks instanceof WeakSet)) rt.releasedDispatchTasks = new WeakSet()
|
|
3300
|
+
if (rt.releasedDispatchTasks.has(task)) return { released: false, taskId: task.id, reason: 'already-released' }
|
|
3301
|
+
const detail = trimmed(abortReason, 300) || '未知原因'
|
|
3302
|
+
const lastError = trimmed('派发未启动:' + detail, 500)
|
|
3303
|
+
if (task.status === 'doing') {
|
|
3304
|
+
try {
|
|
3305
|
+
// doing → todo:applyTaskUpdate 顺带清 assignee / autoDispatch / 通知记账,卡片回到普通待做队列。
|
|
3306
|
+
applyTaskUpdate(rt.state, task.id, { status: 'todo', assignee: '', lastError }, now)
|
|
3307
|
+
} catch (error) {
|
|
3308
|
+
// 归档等不可迁移状态:不猜、也不删卡,保留原状态只写 lastError。
|
|
3309
|
+
task.lastError = lastError
|
|
3310
|
+
task.updatedAt = iso(now)
|
|
3311
|
+
recordLeadAutoError(rt, '降级预建卡片失败(保留原状态):' + describeError(error), now)
|
|
3312
|
+
}
|
|
3313
|
+
} else {
|
|
3314
|
+
// 状态已被组长 / 用户改过(todo / done / archived):不改状态,只补可见原因。
|
|
3315
|
+
task.lastError = lastError
|
|
3316
|
+
task.updatedAt = iso(now)
|
|
3317
|
+
}
|
|
3318
|
+
// status→todo 已清 assignee / autoDispatch;这里再清掉「这次派发」的会话字段。
|
|
3319
|
+
// title / note / cwd / sessionId(创建者)保留:卡片仍是一条可读记录。
|
|
3320
|
+
task.dispatchedSessionId = null
|
|
3321
|
+
task.dispatchedSessionTitle = ''
|
|
3322
|
+
rt.releasedDispatchTasks.add(task)
|
|
3323
|
+
const auto = ensureLeadAuto(rt)
|
|
3324
|
+
auto.aborted += 1
|
|
3325
|
+
auto.lastTaskId = task.id
|
|
3326
|
+
auto.updatedAt = iso(now)
|
|
3327
|
+
const saved = commitBoard(rt.state, rt.filePath)
|
|
3328
|
+
if (saved && saved.saved === false) {
|
|
3329
|
+
recordLeadAutoError(rt, '降级派发预建卡片写盘失败:' + (saved.error || ''), now)
|
|
3330
|
+
}
|
|
3331
|
+
console.warn('[taskboard] 派发未启动,已把预建卡片降级为待开始:' + task.id + '(' + detail + ')')
|
|
3332
|
+
return { released: true, taskId: task.id, status: task.status }
|
|
3333
|
+
} catch (error) {
|
|
3334
|
+
recordLeadAutoError(rt, '降级派发预建卡片失败:' + describeError(error), now)
|
|
3335
|
+
return { released: false, reason: 'error' }
|
|
3336
|
+
}
|
|
3337
|
+
}
|
|
3338
|
+
|
|
3339
|
+
/**
|
|
3340
|
+
* @deprecated 0.2.4 起不再删除预建卡片(见 `releasePreparedDispatch`);保留旧导出名兼容,
|
|
3341
|
+
* 行为等价于「降级为待开始」而不是删除。
|
|
3342
|
+
*/
|
|
3343
|
+
export const discardPreparedDispatch = releasePreparedDispatch
|
|
3344
|
+
|
|
2892
3345
|
/**
|
|
2893
|
-
* tools/result 命中「工作区组长成功派出 worker
|
|
3346
|
+
* tools/result 命中「工作区组长成功派出 worker」时把 worker 会话 id 挂到卡片上(§1.8)。
|
|
3347
|
+
*
|
|
3348
|
+
* 0.2.4 起卡片由 `tools/pre-execute` 的派发闸门(`prepareLeadDispatch`)在 worker 启动**之前**
|
|
3349
|
+
* 建好 / 认领好;这里优先消费闸门留下的 `exec → 卡片` 句柄(`prepared` 分支),只补 worker 字段。
|
|
3350
|
+
* 闸门没留下句柄(老宿主 / 单测直接调用)时退回旧的「观察后补建」路径,保证兜底也有记录。
|
|
2894
3351
|
* 只做副作用:永不抛出(失败只记 leadAuto.lastError),不阻断派发。
|
|
2895
|
-
* @param {{agent:object, childId:string, args?:object}} input
|
|
3352
|
+
* @param {{agent:object, childId:string, args?:object, exec?:object}} input
|
|
2896
3353
|
* @returns {{created:boolean, reason?:string, taskId?:string, task?:object}}
|
|
2897
3354
|
*/
|
|
2898
3355
|
export function handleSubagentDispatch(rt, input, now = Date.now()) {
|
|
@@ -2912,6 +3369,37 @@ export function handleSubagentDispatch(rt, input, now = Date.now()) {
|
|
|
2912
3369
|
return { created: false, reason: 'not-lead' }
|
|
2913
3370
|
}
|
|
2914
3371
|
reloadBoardIfStale(rt)
|
|
3372
|
+
// 0.2.4:闸门已经在 worker 启动前建好 / 认领好卡片 → 只补 worker 会话字段,绝不重复建卡。
|
|
3373
|
+
const prepared = takePreparedDispatch(rt, input && input.exec)
|
|
3374
|
+
if (prepared) {
|
|
3375
|
+
const task = taskById(rt.state, prepared.taskId)
|
|
3376
|
+
if (task) {
|
|
3377
|
+
const sessionId = agentSessionId(agent) || ''
|
|
3378
|
+
if (task.status !== 'doing') {
|
|
3379
|
+
try {
|
|
3380
|
+
applyTaskUpdate(rt.state, task.id, { status: 'doing' }, now)
|
|
3381
|
+
} catch {
|
|
3382
|
+
/* 归档等不可迁移状态:保留原状态,只补 worker 字段(不阻断派发) */
|
|
3383
|
+
}
|
|
3384
|
+
}
|
|
3385
|
+
// 自动建的卡片归当前 worker;轮询任务只在还没挂 worker 时补——组长审核的对象不被覆盖。
|
|
3386
|
+
if (task.autoDispatch === LEAD_AUTO_MARK || task.assignee === '') task.assignee = childId
|
|
3387
|
+
if (sessionId !== '' && asString(task.dispatchedSessionId) === '') task.dispatchedSessionId = sessionId
|
|
3388
|
+
if (asString(task.dispatchedSessionTitle) === '') task.dispatchedSessionTitle = sessionTitle(agent)
|
|
3389
|
+
task.lastError = ''
|
|
3390
|
+
task.updatedAt = iso(now)
|
|
3391
|
+
const saved = commitBoard(rt.state, rt.filePath)
|
|
3392
|
+
auto.lastTaskId = task.id
|
|
3393
|
+
auto.lastWorkerSessionId = childId
|
|
3394
|
+
auto.updatedAt = iso(now)
|
|
3395
|
+
if (saved && saved.saved === false) {
|
|
3396
|
+
// worker 已经跑起来了(无法回退),但卡片在启动前已落盘:只记错误,让排障知道 worker 会话 id 没落盘。
|
|
3397
|
+
recordLeadAutoError(rt, 'worker 已启动,但卡片 worker 字段写盘失败:' + (saved.error || ''), now)
|
|
3398
|
+
}
|
|
3399
|
+
return { created: false, reason: 'prepared', taskId: task.id, task: { ...task } }
|
|
3400
|
+
}
|
|
3401
|
+
// 卡片在闸门之后被删掉了:继续走下面的兜底建卡,绝不出现「worker 在跑、看板没记录」。
|
|
3402
|
+
}
|
|
2915
3403
|
const existing = findTaskByWorker(rt.state, childId)
|
|
2916
3404
|
if (existing) {
|
|
2917
3405
|
auto.deduped += 1
|
|
@@ -2921,7 +3409,8 @@ export function handleSubagentDispatch(rt, input, now = Date.now()) {
|
|
|
2921
3409
|
const sessionId = agentSessionId(agent) || ''
|
|
2922
3410
|
const inFlight = findLeadInFlightTask(rt.state, sessionId, now)
|
|
2923
3411
|
if (inFlight) {
|
|
2924
|
-
//
|
|
3412
|
+
// 兜底路径(闸门未注册 / 卡片被删):组长正在处理轮询投递来的看板任务,这次 subagent 就当它是那条
|
|
3413
|
+
// 任务的 worker,别再建一条。注意:0.2.4 起直聊派发不再依赖这条判断——闸门会先建卡并留下 prepared 句柄。
|
|
2925
3414
|
auto.deduped += 1
|
|
2926
3415
|
auto.updatedAt = iso(now)
|
|
2927
3416
|
return { created: false, reason: 'lead-task-in-flight', taskId: inFlight.id }
|
|
@@ -3068,17 +3557,23 @@ export function registerLeadAutoHooks(ctx, rt) {
|
|
|
3068
3557
|
'tools/result',
|
|
3069
3558
|
(exec, result) => {
|
|
3070
3559
|
try {
|
|
3560
|
+
if (!exec || exec.name !== 'subagent') return
|
|
3071
3561
|
// §1.8 命中条件(三个条件缺一不可,避免别的工具输出里恰好带 subagentId 就误建任务):
|
|
3072
3562
|
// exec.name === 'subagent' + 非错误结果 + value 是 {kind:'activation', subagentId}。
|
|
3073
|
-
|
|
3074
|
-
|
|
3075
|
-
const
|
|
3076
|
-
|
|
3077
|
-
|
|
3078
|
-
|
|
3563
|
+
const failed = !result || result.isError === true
|
|
3564
|
+
const value = result && result.value
|
|
3565
|
+
const activation = !failed && isPlainObject(value) && value.kind === 'activation'
|
|
3566
|
+
const childId = activation && typeof value.subagentId === 'string' ? value.subagentId.trim() : ''
|
|
3567
|
+
if (!activation || childId === '') {
|
|
3568
|
+
// §1.8(0.2.4,t-70 加固):无法确认 worker 已启动(被其它策略拒绝 / post-execute block /
|
|
3569
|
+
// 启动失败 / 取消 / 返回形状变化)→ **绝不删卡**,只把闸门预建的卡片降级为可见的待开始
|
|
3570
|
+
// 记录(status=todo + lastError)。删掉一张 worker 可能正在使用的卡 = 重新制造「有活没卡」。
|
|
3571
|
+
releasePreparedDispatch(rt, exec, dispatchAbortSummary(result))
|
|
3572
|
+
return
|
|
3573
|
+
}
|
|
3079
3574
|
const agent = exec.agent
|
|
3080
3575
|
if (!agent) return
|
|
3081
|
-
handleSubagentDispatch(rt, { agent, childId, args: exec.arguments })
|
|
3576
|
+
handleSubagentDispatch(rt, { agent, childId, args: exec.arguments, exec })
|
|
3082
3577
|
} catch (error) {
|
|
3083
3578
|
recordLeadAutoError(rt, '组长直聊派活挂钩失败:' + describeError(error))
|
|
3084
3579
|
}
|
|
@@ -3100,6 +3595,53 @@ export function registerLeadAutoHooks(ctx, rt) {
|
|
|
3100
3595
|
return true
|
|
3101
3596
|
}
|
|
3102
3597
|
|
|
3598
|
+
/**
|
|
3599
|
+
* 注册 §1.8(0.2.4)的**派发前强制建卡闸门**(`tools/pre-execute`)。
|
|
3600
|
+
*
|
|
3601
|
+
* 这是「没有卡片就不能派发」的硬约束:`tools/pre-execute` 是 tools 管线在**启动工具体之前**的
|
|
3602
|
+
* allow/deny 策略瀑布;这里只拦 `exec.name === 'subagent'` 且调用方预设有 `taskboard-dispatch`
|
|
3603
|
+
* 的派发(worker 自己的 subagent 调用 / 普通会话派子 Agent 一律 `next()` 放行)。
|
|
3604
|
+
* 返回 `{kind:'deny'}` 时工具体不会执行 → worker 不会启动,模型看到可操作错误。
|
|
3605
|
+
*
|
|
3606
|
+
* 注意:非本闸门的调用必须 `return next()`(waterfall 里不调用 next 会截断后面所有策略)。
|
|
3607
|
+
* @returns {boolean} 是否注册成功
|
|
3608
|
+
*/
|
|
3609
|
+
export function registerLeadDispatchGate(ctx, rt) {
|
|
3610
|
+
if (!ctx || typeof ctx.on !== 'function') {
|
|
3611
|
+
console.warn('[taskboard] ctx.on 不可用,跳过派发强制建卡闸门')
|
|
3612
|
+
return false
|
|
3613
|
+
}
|
|
3614
|
+
ctx.on(
|
|
3615
|
+
'tools/pre-execute',
|
|
3616
|
+
(exec, next) => {
|
|
3617
|
+
if (typeof next !== 'function') return { kind: 'allow' }
|
|
3618
|
+
if (!exec || exec.name !== 'subagent') return next()
|
|
3619
|
+
try {
|
|
3620
|
+
const agent = exec.agent
|
|
3621
|
+
if (!agent) return next()
|
|
3622
|
+
// 只拦工作区组长(taskboard-dispatch)的派发;worker / 普通会话的子 Agent 调用不受影响。
|
|
3623
|
+
if (sessionPresetOfContext(rt.ctx, agent) !== DISPATCH_PRESET_ID) return next()
|
|
3624
|
+
const outcome = prepareLeadDispatch(rt, { agent, exec, args: exec.arguments })
|
|
3625
|
+
if (!outcome.ok) {
|
|
3626
|
+
return {
|
|
3627
|
+
kind: 'deny',
|
|
3628
|
+
reason: outcome.message,
|
|
3629
|
+
info: { name: 'TaskboardDispatchError', code: outcome.reason === 'write-failed' ? 'internal' : 'bad-request' },
|
|
3630
|
+
}
|
|
3631
|
+
}
|
|
3632
|
+
return next()
|
|
3633
|
+
} catch (error) {
|
|
3634
|
+
const message =
|
|
3635
|
+
'派发已中止:看板卡片准备失败(' + describeError(error) + ')。没有卡片不会启动 worker;请修复看板后重试。'
|
|
3636
|
+
recordLeadAutoError(rt, message)
|
|
3637
|
+
return { kind: 'deny', reason: message, info: { name: 'TaskboardDispatchError', code: 'internal' } }
|
|
3638
|
+
}
|
|
3639
|
+
},
|
|
3640
|
+
{ global: true },
|
|
3641
|
+
)
|
|
3642
|
+
return true
|
|
3643
|
+
}
|
|
3644
|
+
|
|
3103
3645
|
/** 冷会话唤醒失败的负缓存时长(5 分钟)。 */
|
|
3104
3646
|
const REVIVE_RETRY_MS = 5 * 60 * 1000
|
|
3105
3647
|
|
|
@@ -3435,6 +3977,16 @@ export function registerTools(toolCtx, rt) {
|
|
|
3435
3977
|
}
|
|
3436
3978
|
}
|
|
3437
3979
|
if (typeof patch.note === 'string') assertNoteAttachments(patch.note)
|
|
3980
|
+
// §1.8(0.2.4):组长把轮询投递任务置 doing = 即将为它启动 worker 的意图。必须在
|
|
3981
|
+
// applyTaskUpdate **之前**记(用任务更新前的字段判断「还没挂 worker」),供派发闸门一次性认领。
|
|
3982
|
+
if (patch.status === 'doing') {
|
|
3983
|
+
try {
|
|
3984
|
+
const agent = exec && exec.agent
|
|
3985
|
+
if (agent) noteLeadDispatchIntent(rt, agent, args.id)
|
|
3986
|
+
} catch {
|
|
3987
|
+
/* 意图只是加速认领;记录失败不影响更新本身 */
|
|
3988
|
+
}
|
|
3989
|
+
}
|
|
3438
3990
|
const task = applyTaskUpdate(rt.state, args.id, patch)
|
|
3439
3991
|
commitBoard(rt.state, rt.filePath)
|
|
3440
3992
|
// 任务刚变成 done(或从 done 重开):顺手尝试 flush 完成回执(§1.6 flush 时机)。
|
|
@@ -3473,6 +4025,12 @@ export function apply(ctx) {
|
|
|
3473
4025
|
},
|
|
3474
4026
|
// 组长直聊派活自动建任务(§1.8)的运行时计数 / 最近错误。
|
|
3475
4027
|
leadAuto: createLeadAutoState(),
|
|
4028
|
+
// §1.8(0.2.4):派发闸门的 exec → 预建卡片句柄(WeakMap,不落盘 / 不阻止 GC),
|
|
4029
|
+
// 以及组长「置 doing → 即将派 worker」的一次性意图(Map<组长会话 id, {taskId, at}>)。
|
|
4030
|
+
preparedDispatches: new WeakMap(),
|
|
4031
|
+
// t-70 加固:已降级为 todo 的预建卡片(WeakSet,防同一任务被重复降级 / 重复计数)。
|
|
4032
|
+
releasedDispatchTasks: new WeakSet(),
|
|
4033
|
+
leadDispatchIntent: new Map(),
|
|
3476
4034
|
}
|
|
3477
4035
|
|
|
3478
4036
|
try {
|
|
@@ -3580,4 +4138,13 @@ export function apply(ctx) {
|
|
|
3580
4138
|
} catch (error) {
|
|
3581
4139
|
console.warn('[taskboard] 组长直聊派活挂钩注册失败:' + describeError(error))
|
|
3582
4140
|
}
|
|
3583
|
-
|
|
4141
|
+
|
|
4142
|
+
// §1.8(0.2.4):派发前强制建卡闸门——没有卡片就不能启动 subagent worker。
|
|
4143
|
+
try {
|
|
4144
|
+
if (registerLeadDispatchGate(ctx, runtime)) {
|
|
4145
|
+
console.log('[taskboard] 派发强制建卡闸门已注册:tools/pre-execute(subagent)')
|
|
4146
|
+
}
|
|
4147
|
+
} catch (error) {
|
|
4148
|
+
console.warn('[taskboard] 派发强制建卡闸门注册失败:' + describeError(error))
|
|
4149
|
+
}
|
|
4150
|
+
}
|