dsh-handoff-baton 0.5.0 → 0.5.1

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 CHANGED
@@ -1,5 +1,17 @@
1
1
  # Changelog
2
2
 
3
+ ## 0.5.1 · 2026-11-29
4
+
5
+ 实测事故:`/baton` 的 now / pick 在任何对话里都报「无法将请求排入对话(宿主模块不可用)」,压力提醒也一次没触发过。
6
+
7
+ - 根因:插件用 `import('@deepseek-ai/dsh-llm')` 取宿主的 `createUserMessage`,该宿主里动态 import 落空,函数恒为 undefined——排队与提醒同时静默失效
8
+ - 修法:新增 `lib/message.js` 本地消息工厂(role + 随机 id + 深拷贝后深冻结,与宿主 `createMessage` 同形),插件不再依赖任何宿主模块
9
+ - 排队逻辑抽到 `lib/queue.js`(followup / send / inbox 三条入口 + 两种失败原因),首次被单测覆盖
10
+ - 压力提醒恢复:60% / 80% / 压缩后三条提醒重新生效
11
+ - 排队失败的话术拆成两种:没有会话(新建对话页面)与没有排队入口,各自给出下一步,不再笼统一句「宿主模块不可用」
12
+ - 接棒口令容错:标记后面可以直接贴 `baton list` 的整行或 markdown 链接,行尾的 `.md` 路径会被自动取出(纯路径与名称写法照旧)
13
+ - 测试新增 11 项(消息形状、id 唯一、深冻结、深拷贝隔离、不再动态 import;三条排队入口、两种失败、话术区分;口令容错)
14
+
3
15
  ## 0.5.0 · 2026-11-29
4
16
 
5
17
  全局唯一索引。修的是实测事故:索引跟着工作目录各写一份,换个会话就找不到上一份交接。
package/index.js CHANGED
@@ -58,16 +58,8 @@ import {
58
58
  DEFAULT_STALE_DAYS,
59
59
  inspectIndex,
60
60
  } from './lib/maintenance.js'
61
-
62
- // 注入用到的宿主模块:拿不到就退化成"只注册技能和工具",插件照样能装。
63
- async function loadCreateUserMessage() {
64
- try {
65
- const mod = await import('@deepseek-ai/dsh-llm')
66
- return typeof mod.createUserMessage === 'function' ? mod.createUserMessage : undefined
67
- } catch {
68
- return undefined
69
- }
70
- }
61
+ import { createUserMessage } from './lib/message.js'
62
+ import { enqueueCommand, queueFailureText } from './lib/queue.js'
71
63
 
72
64
  const PACKAGE_ROOT = dirname(fileURLToPath(import.meta.url))
73
65
  const SKILL_PATH = join(PACKAGE_ROOT, 'skills', 'baton', 'SKILL.md')
@@ -162,9 +154,6 @@ function pressureStateFor(key) {
162
154
  * 每档每会话只说一次。钩子挂了也只影响提醒,不影响工具和技能。
163
155
  */
164
156
  async function installNudge(ctx, config) {
165
- const createUserMessage = await loadCreateUserMessage()
166
- if (createUserMessage === undefined) return
167
-
168
157
  ctx.on('session/event', (session, event) => {
169
158
  try {
170
159
  const state = pressureStateFor(session)
@@ -210,28 +199,6 @@ async function installNudge(ctx, config) {
210
199
  async function installCommands(ctx, config) {
211
200
  const commands = typeof ctx.get === 'function' ? ctx.get('commands') : undefined
212
201
  if (!commands || typeof commands.register !== 'function') return
213
- const createUserMessage = await loadCreateUserMessage()
214
-
215
- const enqueue = (agent, text) => {
216
- if (createUserMessage === undefined || agent === undefined) return false
217
- const message = createUserMessage({
218
- content: [{ type: 'text', text }],
219
- source: { kind: 'baton-command', form: 'notice', summary: '/baton' },
220
- })
221
- if (typeof agent.followup === 'function') {
222
- agent.followup(message)
223
- return true
224
- }
225
- if (typeof agent.send === 'function') {
226
- agent.send(message, 'next-turn', true)
227
- return true
228
- }
229
- if (agent.inbox && typeof agent.inbox.append === 'function') {
230
- agent.inbox.append('next-turn', message)
231
- return true
232
- }
233
- return false
234
- }
235
202
 
236
203
  const workspaceOf = (agent) => {
237
204
  const cwd = agent?.session?.header?.cwd
@@ -386,9 +353,8 @@ async function installCommands(ctx, config) {
386
353
  const targetAbs = resolveRowPath(info.root, target.path)
387
354
  if (targetAbs !== null && existsSync(targetAbs)) {
388
355
  const text = pickPrompt(targetAbs, ENTRY_MARKER) + driftHint(entries, targetAbs, info.root)
389
- if (!enqueue(invocation?.agent, text)) {
390
- return { kind: 'error', text: `无法将请求排入对话(宿主模块不可用)。可手动发送以下内容:\n${text}` }
391
- }
356
+ const failure = enqueueCommand(invocation?.agent, text)
357
+ if (failure !== null) return queueFailureText(failure, text)
392
358
  return { kind: 'success', text: `已排入队列:接手「${target.name}」(${targetAbs})。` }
393
359
  }
394
360
  }
@@ -399,9 +365,8 @@ async function installCommands(ctx, config) {
399
365
  return { kind: 'error', text: `未找到 ${parsed.path}(也没有这个名称)。已有交接:\n${listText(listDocs(invocation?.agent))}` }
400
366
  }
401
367
  const text = pickPrompt(abs, ENTRY_MARKER) + driftHint(entries, abs, info.root)
402
- if (!enqueue(invocation?.agent, text)) {
403
- return { kind: 'error', text: `无法将请求排入对话(宿主模块不可用)。可手动发送以下内容:\n${text}` }
404
- }
368
+ const failure = enqueueCommand(invocation?.agent, text)
369
+ if (failure !== null) return queueFailureText(failure, text)
405
370
  return { kind: 'success', text: `已排入队列:接手 ${abs}。` }
406
371
  }
407
372
  const resolved = resolveIndex(entries)
@@ -415,9 +380,8 @@ async function installCommands(ctx, config) {
415
380
  }
416
381
  }
417
382
  const text = pickPrompt(abs, ENTRY_MARKER) + driftHint(entries, abs, info.root)
418
- if (!enqueue(invocation?.agent, text)) {
419
- return { kind: 'error', text: `无法将请求排入对话(宿主模块不可用)。可手动发送以下内容:\n${text}` }
420
- }
383
+ const failure = enqueueCommand(invocation?.agent, text)
384
+ if (failure !== null) return queueFailureText(failure, text)
421
385
  return { kind: 'success', text: `已排入队列:接手 ${abs}(${target.title} · ${target.status})。` }
422
386
  }
423
387
  if (resolved.kind === 'ambiguous') {
@@ -431,9 +395,8 @@ async function installCommands(ctx, config) {
431
395
  return { kind: 'error', text: `当前没有进行中的交接。已有交接:\n${listText(listDocs(invocation?.agent))}` }
432
396
  }
433
397
  const text = writePrompt(parsed.name)
434
- if (!enqueue(invocation?.agent, text)) {
435
- return { kind: 'error', text: `无法将请求排入对话(宿主模块不可用)。可手动发送以下内容:\n${text}` }
436
- }
398
+ const failure = enqueueCommand(invocation?.agent, text)
399
+ if (failure !== null) return queueFailureText(failure, text)
437
400
  return {
438
401
  kind: 'success',
439
402
  text: '已排入队列:下一轮开始编写交接,落盘前会先与你确认。',
package/lib/message.js ADDED
@@ -0,0 +1,28 @@
1
+ /**
2
+ * 本地消息工厂:等价于宿主 `@deepseek-ai/dsh-llm` 的 `createUserMessage`。
3
+ *
4
+ * 为什么自己造:插件原来用 `import('@deepseek-ai/dsh-llm')` 去取宿主的工厂,
5
+ * 在那个宿主里动态 import 没接上,`createUserMessage` 拿到 undefined,
6
+ * `/baton` 的排队与压力提醒就一起静默失效(0.5.0 的实测事故)。
7
+ * 消息的实质只有「role + 一个随机 id + 深冻结」,本地复刻既零依赖,
8
+ * 也不会随宿主版本或加载方式变化。
9
+ *
10
+ * 形状与宿主一致:`{ ...input, role: 'user', id }`,先深拷贝再深冻结。
11
+ */
12
+ import { randomUUID } from 'node:crypto'
13
+
14
+ /** 逐层冻结:宿主发布后的消息不可被下游改写,这里对齐同一语义。 */
15
+ function deepFreeze(value) {
16
+ if (value === null || typeof value !== 'object' || Object.isFrozen(value)) return value
17
+ for (const key of Object.keys(value)) deepFreeze(value[key])
18
+ return Object.freeze(value)
19
+ }
20
+
21
+ /**
22
+ * 造一条不可变的 user 消息。
23
+ * @param input - 消息内容与来源(content / source)。
24
+ * @returns 带新 id、`role: 'user'`、已深冻结的消息。
25
+ */
26
+ export function createUserMessage(input = {}) {
27
+ return deepFreeze(structuredClone({ ...input, role: 'user', id: randomUUID() }))
28
+ }
package/lib/protocol.js CHANGED
@@ -205,6 +205,29 @@ export function verifyDocument(text) {
205
205
  export const ENTRY_MARKER = '⟦baton⟧'
206
206
  export const ENTRY_ALIASES = ['<<baton>>', '[[baton]]']
207
207
 
208
+ /**
209
+ * 从口令正文里取目标路径。
210
+ *
211
+ * 三种写法都认:
212
+ * 1. markdown 链接:[标题](E:/a.md)
213
+ * 2. 列表行直接贴过来:`baton交接测试 — 已接手 · 时间 · 工作区 — E:/…/x.md`,取行尾那个 .md 路径
214
+ * 3. 纯路径 / 名称:原样返回(名称留给上层按 #名称 匹配)
215
+ */
216
+ function extractPathFromTail(tail) {
217
+ const text = String(tail ?? '').trim()
218
+ if (text === '') return undefined
219
+ const link = text.match(/\]\(([^)\s]+\.md)\)/iu)
220
+ if (link) return link[1].trim()
221
+ const end = text.toLowerCase().lastIndexOf('.md')
222
+ if (end >= 0) {
223
+ let start = end
224
+ while (start > 0 && !/[\s—–·|()[\],;]/u.test(text[start - 1])) start -= 1
225
+ const candidate = text.slice(start, end + 3).trim()
226
+ if (candidate !== '' && /[\\/:]/u.test(candidate)) return candidate
227
+ }
228
+ return text
229
+ }
230
+
208
231
  /**
209
232
  * 判断一条消息是不是接棒口令。
210
233
  * 只看第一个非空行:以标记开头才算,否则一律当普通对话(技能视为不存在)。
@@ -218,7 +241,7 @@ export function parseEntryMarker(text) {
218
241
  const marker = [ENTRY_MARKER, ...ENTRY_ALIASES].find((m) => first.startsWith(m))
219
242
  if (!marker) return { active: false }
220
243
  const rest = first.slice(marker.length).trim()
221
- return { active: true, marker, path: rest === '' ? undefined : rest }
244
+ return { active: true, marker, path: extractPathFromTail(rest) }
222
245
  }
223
246
 
224
247
  /** 生成接棒码:带路径的短码 + 裸标记 + 自解释长码。 */
package/lib/queue.js ADDED
@@ -0,0 +1,48 @@
1
+ /**
2
+ * 指令排队:把一句话塞进当前会话(followup 唤醒一轮)。
3
+ *
4
+ * 单独成模块是为了能单测——`/baton` 的 now / pick 只有这条路,
5
+ * 0.5.0 就是它静默失效,而单测看不见。
6
+ */
7
+ import { createUserMessage } from './message.js'
8
+
9
+ /** 指令消息的来源标记;source 是宿主可扩展的联合,允许插件自定义 kind。 */
10
+ export function commandSource() {
11
+ return { kind: 'baton-command', form: 'notice', summary: '/baton' }
12
+ }
13
+
14
+ /**
15
+ * 把一句话排进当前会话。
16
+ * @param agent - 当前会话的 agent(新建对话页面里可能不存在)。
17
+ * @param text - 要排入的消息正文。
18
+ * @returns 成功为 null;失败为原因:`no-agent`(没有会话)或 `no-transport`(没有排队入口)。
19
+ */
20
+ export function enqueueCommand(agent, text) {
21
+ if (agent === undefined || agent === null) return 'no-agent'
22
+ const message = createUserMessage({
23
+ content: [{ type: 'text', text }],
24
+ source: commandSource(),
25
+ })
26
+ if (typeof agent.followup === 'function') {
27
+ agent.followup(message)
28
+ return null
29
+ }
30
+ if (typeof agent.send === 'function') {
31
+ agent.send(message, 'next-turn', true)
32
+ return null
33
+ }
34
+ if (agent.inbox && typeof agent.inbox.append === 'function') {
35
+ agent.inbox.append('next-turn', message)
36
+ return null
37
+ }
38
+ return 'no-transport'
39
+ }
40
+
41
+ /** 排队失败时给用户的话:原因不同,下一步也不同。 */
42
+ export function queueFailureText(reason, text) {
43
+ const why =
44
+ reason === 'no-agent'
45
+ ? '这条对话还没有可排队的会话(新建对话页面就是这种状态)。把下面这句作为第一条消息发送即可'
46
+ : '当前宿主没有提供排队入口。把下面这句作为下一条消息发送即可'
47
+ return { kind: 'error', text: `${why}:\n${text}` }
48
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dsh-handoff-baton",
3
- "version": "0.5.0",
3
+ "version": "0.5.1",
4
4
  "description": "交接棒:上下文快满时自动判断、归档并生成跨模型接棒码的 DeepSeek Harness 插件与通用技能",
5
5
  "type": "module",
6
6
  "main": "index.js",
@@ -32,7 +32,7 @@ description: 交接棒——上下文快到上限、要换会话或要换模型
32
32
  3. 多于一条 → 把候选(绝对路径 + 标题 + 状态 + 更新时间)列出来问用户要接哪份,**不许自己挑**
33
33
  4. 唯一那条指向的文件不存在 → 原样报告用户,不要自己找替代文件
34
34
 
35
- 索引不存在时:不要在当前项目里另建 `handoff/INDEX.md`,先让用户跑 `baton init` 选位置(或问用户索引在哪)。这台机器上所有项目共用同一份索引,项目里没有自己的索引是正常状态。
35
+ 索引不存在时:不要在当前项目里另建 `handoff/INDEX.md`,先让用户跑 `baton init` 选位置(或问用户索引在哪)。这台机器上所有项目共用同一份索引,项目里没有自己的索引是正常状态。标记后面可以直接贴 `baton list` 的整行或 markdown 链接,行尾的 `.md` 路径会被自动取出来,不必手工截。
36
36
 
37
37
  接错任务的代价远高于多问一句:接收方在零上下文里没有能力发现自己接错了,只会顺着错的任务往下做。同一工作区并行开多条线时,一律用短码,不要用裸标记。
38
38
 
@@ -102,7 +102,7 @@ session: session-7ed60911
102
102
  解析规则(实现见 `lib/protocol.js` 的 `parseEntryMarker`):
103
103
 
104
104
  1. 跳过空行,取第一个非空行
105
- 2. 行首匹配任一标记才算命中,标记后面剩下的内容当作文档路径
105
+ 2. 行首匹配任一标记才算命中,标记后面的内容当作文档路径;容错三种写法:纯路径、markdown 链接 `[标题](路径)`、直接贴 `baton list` 的一整行(取行尾那个 `.md` 路径)。都不是时整段原样返回,交给上层按 `#名称` 匹配
106
106
  3. 命中但没有路径 → 读全局索引:只有一条「在用」(进行中 + 已接手)才用它;多于一条必须列出候选并询问,不得静默选择;唯一那条指向的文件不存在时报告用户,不硬找
107
107
  4. 不命中 → 什么都不做
108
108