dsh-dispatch-board 0.2.0 → 0.2.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.
Files changed (3) hide show
  1. package/lib/client.js +191 -49
  2. package/lib/index.js +105 -0
  3. package/package.json +1 -1
package/lib/client.js CHANGED
@@ -111,7 +111,10 @@ window.__ModuleLoader__.load({
111
111
  '.tb-board{display:grid;grid-template-columns:repeat(4,minmax(0,1fr));gap:12px;align-items:stretch;min-width:0;flex:1 0 auto;min-height:240px}',
112
112
  '.tb-col{display:flex;flex-direction:column;gap:8px;min-width:0;background:var(--dsw-alias-bg-layer-1);border:1px solid var(--dsw-alias-border-l1);border-radius:var(--dsw-radius-md);padding:9px 9px 11px;min-height:220px;height:100%}',
113
113
  '.tb-col[data-status=archived]{opacity:.92}',
114
- '.tb-colhead{display:flex;align-items:center;gap:7px;padding:1px 1px 5px;border-bottom:1px solid var(--dsw-alias-border-l1)}',
114
+ '.tb-colhead{display:flex;align-items:center;gap:7px;padding:1px 1px 5px;border-bottom:1px solid var(--dsw-alias-border-l1);height:31px;box-sizing:border-box}',
115
+ // 列头高度固定:只有「已完成」列带「全部归档」按钮(24px 高),其它列只有文字(14px 行高)。
116
+ // 不固定时 flex 行高被按钮撑大 ~10px,导致「已完成」标题和整列卡片都比别的列低一截。
117
+ '.tb-page[data-narrow=true] .tb-colhead{height:29px}',
115
118
  '.tb-coldot{width:8px;height:8px;border-radius:50%;flex:0 0 auto;background:var(--tb-accent)}',
116
119
  '.tb-colname{font:var(--dsw-font-xxs-strong-12);font-weight:600;letter-spacing:.01em}',
117
120
  '.tb-colcount{min-width:18px;height:18px;padding:0 5px;display:inline-flex;align-items:center;justify-content:center;border-radius:999px;background:color-mix(in srgb,var(--tb-accent) 16%,transparent);color:var(--tb-accent);font:var(--dsw-font-xxxs-strong-11);font-variant-numeric:tabular-nums}',
@@ -370,13 +373,16 @@ window.__ModuleLoader__.load({
370
373
  '.tb-rich a{color:var(--dsw-alias-link,var(--tb-accent));text-decoration:none}',
371
374
  '.tb-rich a:hover{text-decoration:underline}',
372
375
 
373
- // ── 卡片精简 + 等高(t-33)──────────────────────────────────
374
- // 卡片正文只剩标题,操作行固定贴底;高度统一用 min-height 兜住(--tb-card-h),
375
- // 标题 / 按钮数量不同的卡片视觉等高,按钮行不会被固定高度裁掉。
376
- // 标题 2 行截断(line-clamp),全文放 title 属性;行内编辑态不带 .tb-card-fixed,高度自适应。
377
- '.tb-card-fixed{min-height:var(--tb-card-h,108px);gap:5px;padding:9px 10px 8px}',
378
- '.tb-card-fixed .tb-card-title{display:-webkit-box;-webkit-line-clamp:2;-webkit-box-orient:vertical;overflow:hidden;line-height:1.4;word-break:break-word}',
379
- '.tb-card-fixed .tb-card-actions{margin-top:auto;gap:3px 4px;padding-top:0}',
376
+ // ── 卡片精简 + 等高(t-33 / t-42)────────────────────────────
377
+ // 卡片正文只剩标题,高度由内容决定(t-42 去掉了整卡 min-height:108px + 操作行
378
+ // margin-top:auto——那两条会把 2 行标题的卡片撑高,多余高度全变成标题与按钮之间的空行)。
379
+ // 等高改用「标题固定 2 行高度」:1 行标题与 2 行标题的标题块都是 2.8em(12.5px × 1.4 × 2
380
+ // = 35px),标题与操作行之间只留 .tb-card-fixed 的 gap:5px,不再有整块空行。
381
+ // 窄栏按钮换 2 行时卡片会变高(不裁按钮的取舍);标题 2 行截断(line-clamp),
382
+ // 全文放 title 属性;行内编辑态不带 .tb-card-fixed,高度自适应。
383
+ '.tb-card-fixed{gap:5px;padding:9px 10px 8px}',
384
+ '.tb-card-fixed .tb-card-title{display:-webkit-box;-webkit-line-clamp:2;-webkit-box-orient:vertical;overflow:hidden;min-height:2.8em;line-height:1.4;word-break:break-word}',
385
+ '.tb-card-fixed .tb-card-actions{gap:3px 4px;padding-top:0}',
380
386
  '.tb-card-fixed .tb-card-actions .tb-btn{height:22px;padding:0 7px;font-size:10.5px}',
381
387
  ].join('\n')
382
388
 
@@ -465,6 +471,7 @@ window.__ModuleLoader__.load({
465
471
  'detail.sessions': '关联对话',
466
472
  'detail.creatorSession': '发起对话',
467
473
  'detail.runSession': '执行对话',
474
+ 'detail.leadSession': '组长对话',
468
475
  'detail.noSession': '(这条任务还没有关联对话)',
469
476
  'detail.basicInfo': '基本信息',
470
477
  'detail.timeline': '时间线',
@@ -632,6 +639,7 @@ window.__ModuleLoader__.load({
632
639
  'detail.sessions': 'Linked conversations',
633
640
  'detail.creatorSession': 'Created in',
634
641
  'detail.runSession': 'Dispatched to',
642
+ 'detail.leadSession': 'Lead conversation',
635
643
  'detail.noSession': '(this task has no linked conversation yet)',
636
644
  'detail.basicInfo': 'Details',
637
645
  'detail.timeline': 'Timeline',
@@ -900,6 +908,51 @@ window.__ModuleLoader__.load({
900
908
  return sessionId.length > 8 ? sessionId.slice(-8) : sessionId
901
909
  }
902
910
 
911
+ // 会话 id 只认两种形态:`session-<id>`(宿主会话)与裸 UUID(宿主 `subagentId` 就是会话 UUID)。
912
+ // 任务 `assignee` 是自由文本,组长也可能写 `worker-42` / `main` 这类普通标签,那些不是会话。
913
+ const SESSION_ID_PATTERN = /^(?:session-[A-Za-z0-9_-]+|[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12})$/i
914
+ // 从自由文本里找会话 id token 用:不带锚点,但两侧边界不能是 `[A-Za-z0-9_-]`(见 extractSessionId)。
915
+ const SESSION_TOKEN_PATTERN = /session-[A-Za-z0-9_-]+|[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}/gi
916
+ const SESSION_TOKEN_EDGE = /[A-Za-z0-9_-]/
917
+
918
+ /** 整串就是会话 id(严格判定,不做提取)。 */
919
+ function looksLikeSessionId(value) {
920
+ return typeof value === 'string' && SESSION_ID_PATTERN.test(value.trim())
921
+ }
922
+
923
+ /**
924
+ * 从 `assignee` 这类自由文本里提取第一个会话 id(t-39 增量):
925
+ * 组长常把 id 写成 `5a6b…(worker)` / `… (worker)` / `…@worker` / `worker: …`,
926
+ * 整串严格匹配会漏掉 → 卡片回退组长会话。这里按 token 扫描,返回 id 本身(保留原始大小写)。
927
+ * 边界:token 前后不能紧邻 `[A-Za-z0-9_-]`,所以 `worker-42` / `main` / 中文标签 /
928
+ * `session-`(dash 后没有 id)/ 被别的词粘住的片段都不算会话,返回空串。
929
+ */
930
+ function extractSessionId(value) {
931
+ if (typeof value !== 'string') return ''
932
+ const text = value.trim()
933
+ if (text === '') return ''
934
+ if (looksLikeSessionId(text)) return text
935
+ SESSION_TOKEN_PATTERN.lastIndex = 0
936
+ let match = SESSION_TOKEN_PATTERN.exec(text)
937
+ while (match !== null) {
938
+ const end = match.index + match[0].length
939
+ const before = match.index > 0 ? text.charAt(match.index - 1) : ''
940
+ const after = end < text.length ? text.charAt(end) : ''
941
+ if (!SESSION_TOKEN_EDGE.test(before) && !SESSION_TOKEN_EDGE.test(after)) return match[0]
942
+ match = SESSION_TOKEN_PATTERN.exec(text)
943
+ }
944
+ return ''
945
+ }
946
+
947
+ /**
948
+ * 任务真正的「执行对话」(worker 子会话):组长直聊派活时把 worker 会话 id 写进 `task.assignee`(§1.8),
949
+ * 但 `assignee` 也可能是普通标签 —— 只有文本里能提取到会话 id 时才返回,否则返回空串(不当会话打开)。
950
+ */
951
+ function workerSessionOf(task) {
952
+ if (!task || typeof task !== 'object') return ''
953
+ return extractSessionId(task.assignee)
954
+ }
955
+
903
956
  /* ── v0.2.0 组长条(主会话 / 工作区组长)纯展示函数 ───────── */
904
957
 
905
958
  // 「派发到」的四个合法值:v0.2.0 起默认 workspace-lead(缺省 / 未知一律回退它,不要回退 new)。
@@ -1757,6 +1810,51 @@ window.__ModuleLoader__.load({
1757
1810
  }
1758
1811
  }
1759
1812
 
1813
+ /**
1814
+ * 「再点一次收起」的判定:右栏已展开、且当前活动 tab 就是本插件的看板页时,返回该 TabRecord
1815
+ * (用它的 id 调 hide),否则返回 null。
1816
+ *
1817
+ * 只看右栏公开 API(0.2.2-rc.1 的 `ISidebarRight`:`active()` / `isExpanded()` / `hide(tabId)`);
1818
+ * 任一 API 缺失或抛错都当作「没开」,让点击继续走 openBoard —— 收起是锦上添花,
1819
+ * 绝不能把打开这条主路径挡掉。`TabRecord` 的形状实测为 `{ id, kind, title, contentId }`,
1820
+ * 本插件注册的页类型 kind 就是 TAB_KIND(见 §5.0)。
1821
+ */
1822
+ function expandedBoardTab() {
1823
+ if (!sidebarRightService) return null
1824
+ if (typeof sidebarRightService.active !== 'function') return null
1825
+ if (typeof sidebarRightService.isExpanded !== 'function') return null
1826
+ try {
1827
+ if (sidebarRightService.isExpanded() !== true) return null
1828
+ } catch (error) {
1829
+ return null
1830
+ }
1831
+ try {
1832
+ const tab = sidebarRightService.active()
1833
+ if (tab && typeof tab === 'object' && tab.kind === TAB_KIND && typeof tab.id === 'string') return tab
1834
+ } catch (error) {
1835
+ /* 读不到活动 tab 就按「没开」处理 */
1836
+ }
1837
+ return null
1838
+ }
1839
+
1840
+ /**
1841
+ * header 图标:看板已展开且在眼前 → 收起(docked 折叠整列 / floating 关闭浮层,tab 本身保留,
1842
+ * 下次点开还在);其它情况(没开 / 已收起 / 活动 tab 是别的)→ 照常 openTab 打开并聚焦。
1843
+ */
1844
+ function toggleBoard(retry = 0) {
1845
+ const tab = expandedBoardTab()
1846
+ if (tab && sidebarRightService && typeof sidebarRightService.hide === 'function') {
1847
+ try {
1848
+ sidebarRightService.hide(tab.id)
1849
+ return
1850
+ } catch (error) {
1851
+ // 收起失败不该变成「点了没反应」:下面照常打开。
1852
+ console.warn('[dsh-dispatch-board] 收起右侧看板失败:' + (error && error.message ? error.message : error))
1853
+ }
1854
+ }
1855
+ openBoard(retry)
1856
+ }
1857
+
1760
1858
  /* ── 图标 ───────────────────────────────────────────────── */
1761
1859
 
1762
1860
  /** 看板 glyph:会话 header 按钮、右栏 tab chip、引导页入口共用。 */
@@ -1840,7 +1938,9 @@ window.__ModuleLoader__.load({
1840
1938
  className: 'tb-headbtn',
1841
1939
  title: tip,
1842
1940
  'aria-label': t('action.openBoard'),
1843
- onClick: openBoard,
1941
+ // 不用 openBoard 直连:onClick 会把事件对象当第一个参数传进去(retry 失效),
1942
+ // 而且这里要走「已展开就收起」的 toggle。
1943
+ onClick: () => toggleBoard(),
1844
1944
  },
1845
1945
  h(BoardGlyph, { size: 16 }),
1846
1946
  badge === null ? null : h('span', { className: 'tb-badge', 'aria-hidden': 'true' }, badge),
@@ -1871,22 +1971,40 @@ window.__ModuleLoader__.load({
1871
1971
  // 组件身份因此保持不变,2s 轮询刷新不会卸载重建它们(否则详情浮层会闪、滚动位置/编辑态会丢)。
1872
1972
 
1873
1973
  /**
1874
- * 任务关联的会话:优先「执行对话」,其次「发起对话」;
1875
1974
  * 老版本 Host 派发的进行中任务没记 dispatchedSessionId,用 snapshot.poll 的最近派发兜底
1876
- * (仅当同时只有一条 doing 任务时,避免张冠李戴)。
1975
+ * (仅当同时只有一条 doing 任务时,避免张冠李戴)。兜底会话是「派发到哪个会话」,即组长会话。
1877
1976
  */
1878
- function sessionOf(task) {
1977
+ function pollFallbackSessionOf(task) {
1879
1978
  if (!task) return ''
1880
- if (task.dispatchedSessionId) return task.dispatchedSessionId
1881
- if (task.sessionId) return task.sessionId
1882
1979
  const fallback = typeof bind.fallbackDispatchSessionId === 'string' ? bind.fallbackDispatchSessionId : ''
1883
- if (task.status === 'doing' && fallback !== '') {
1884
- const lastDispatchTaskId = typeof bind.lastDispatchTaskId === 'string' ? bind.lastDispatchTaskId : ''
1885
- if (lastDispatchTaskId === '' || lastDispatchTaskId === task.id) return fallback
1886
- }
1980
+ if (fallback === '') return ''
1981
+ if (task.status !== 'doing') return ''
1982
+ const lastDispatchTaskId = typeof bind.lastDispatchTaskId === 'string' ? bind.lastDispatchTaskId : ''
1983
+ if (lastDispatchTaskId === '' || lastDispatchTaskId === task.id) return fallback
1887
1984
  return ''
1888
1985
  }
1889
1986
 
1987
+ /** 组长会话:`dispatchedSessionId`,其次老 Host 的 poll 兜底。 */
1988
+ function leadSessionOf(task) {
1989
+ if (!task) return ''
1990
+ const dispatched = typeof task.dispatchedSessionId === 'string' ? task.dispatchedSessionId : ''
1991
+ return dispatched !== '' ? dispatched : pollFallbackSessionOf(task)
1992
+ }
1993
+
1994
+ /**
1995
+ * 任务关联的会话(卡片「对话」按钮的跳转目标),优先级:
1996
+ * worker 子会话(真正执行任务的对话)→ 组长会话(dispatchedSessionId)→ 发起对话(sessionId)
1997
+ * → 老 Host 的 poll 兜底(doing)。没有 worker 时组长会话不再冒充「执行对话」,但仍是卡片的首选跳转。
1998
+ */
1999
+ function sessionOf(task) {
2000
+ if (!task) return ''
2001
+ const worker = workerSessionOf(task)
2002
+ if (worker !== '') return worker
2003
+ if (typeof task.dispatchedSessionId === 'string' && task.dispatchedSessionId !== '') return task.dispatchedSessionId
2004
+ if (typeof task.sessionId === 'string' && task.sessionId !== '') return task.sessionId
2005
+ return pollFallbackSessionOf(task)
2006
+ }
2007
+
1890
2008
  /* ── 子组件 ───────────────────────────────────────────── */
1891
2009
 
1892
2010
  function Banner(props) {
@@ -2030,12 +2148,14 @@ window.__ModuleLoader__.load({
2030
2148
 
2031
2149
  // 卡片只留标题 + 操作按钮:note / result / lastError / id / 工作区 / 时间 /
2032
2150
  // 执行者 / 组长这些背景信息都在详情浮层里看(点标题或「详情」)。
2033
- // 少这一整段后卡片能视觉等高(.tb-card-fixed 的 min-height + 操作行 margin-top:auto)。
2151
+ // 少这一整段后卡片高度由内容决定(.tb-card-fixed 的标题固定 2 行高度 +
2152
+ // 标题/按钮之间只留 gap,不再用整卡 min-height + 操作行 margin-top:auto 撑空行)。
2034
2153
 
2035
2154
  cardActions.unshift(['detail', () => actions.openDetail(task), false])
2155
+ // 卡片「对话」优先 worker 子会话(真正干活的对话),其次组长 / 发起对话;title 用实际目标 id 便于辨认。
2036
2156
  const cardSessionId = sessionOf(task)
2037
2157
  if (cardSessionId !== '') {
2038
- cardActions.push(['openChat', () => actions.openSession({ dispatchedSessionId: cardSessionId, sessionId: task.sessionId }), false])
2158
+ cardActions.push(['openChat', () => actions.openSession(cardSessionId), false, cardSessionId])
2039
2159
  }
2040
2160
 
2041
2161
  return h(
@@ -2072,6 +2192,8 @@ window.__ModuleLoader__.load({
2072
2192
  'tb-btn tb-btn-sm' +
2073
2193
  (entry[2] ? ' tb-btn-primary' : '') +
2074
2194
  (entry[0] === 'delete' ? ' tb-btn-danger tb-btn-ghost' : ''),
2195
+ // 可选第 4 项:按钮 title(「对话」按钮用实际跳转的会话 id)。
2196
+ title: entry[3],
2075
2197
  disabled: busy === busyKey || busy === 'dispatch:' + task.id || busy === 'delete:' + task.id,
2076
2198
  onClick: entry[1],
2077
2199
  },
@@ -2108,6 +2230,21 @@ window.__ModuleLoader__.load({
2108
2230
  [t('detail.archived'), fmtShortWhen(task.archivedAt), fmtWhen(task.archivedAt)],
2109
2231
  ]
2110
2232
  const resultEmpty = task.status === 'done' && !task.result
2233
+ // 「关联对话」:发起(sessionId)/ 执行(worker assignee)/ 组长(dispatchedSessionId / poll 兜底),
2234
+ // 各自可点;同一个会话只显示一次(worker 可能等于发起会话,组长会话也可能等于发起会话)。
2235
+ const creatorSession = typeof task.sessionId === 'string' ? task.sessionId : ''
2236
+ const workerSession = workerSessionOf(task)
2237
+ const leadSession = leadSessionOf(task)
2238
+ const detailSessions = []
2239
+ if (creatorSession !== '') {
2240
+ detailSessions.push({ key: 'creator', label: t('detail.creatorSession'), sessionId: creatorSession })
2241
+ }
2242
+ if (workerSession !== '' && workerSession !== creatorSession) {
2243
+ detailSessions.push({ key: 'worker', label: t('detail.runSession'), sessionId: workerSession })
2244
+ }
2245
+ if (leadSession !== '' && leadSession !== workerSession && leadSession !== creatorSession) {
2246
+ detailSessions.push({ key: 'lead', label: t('detail.leadSession'), sessionId: leadSession })
2247
+ }
2111
2248
  const quick =
2112
2249
  task.status === 'todo'
2113
2250
  ? ['doing', t('card.start')]
@@ -2207,33 +2344,21 @@ window.__ModuleLoader__.load({
2207
2344
  h(
2208
2345
  'div',
2209
2346
  { className: 'tb-detail-sessions' },
2210
- task.sessionId
2211
- ? h(
2212
- 'button',
2213
- {
2214
- type: 'button',
2215
- className: 'tb-btn tb-btn-sm',
2216
- title: task.sessionId,
2217
- onClick: () => props.openSession(task.sessionId),
2218
- },
2219
- t('detail.creatorSession') + ' · ' + shortSession(task.sessionId),
2220
- )
2221
- : null,
2222
- sessionOf(task) !== '' && sessionOf(task) !== task.sessionId
2223
- ? h(
2224
- 'button',
2225
- {
2226
- type: 'button',
2227
- className: 'tb-btn tb-btn-sm',
2228
- title: sessionOf(task),
2229
- onClick: () => props.openSession(sessionOf(task)),
2230
- },
2231
- t('detail.runSession') + ' · ' + shortSession(sessionOf(task)),
2232
- )
2233
- : null,
2234
- sessionOf(task) === ''
2235
- ? h('span', { className: 'tb-detail-muted' }, t('detail.noSession'))
2236
- : null,
2347
+ detailSessions.length === 0
2348
+ ? h('span', { className: 'tb-detail-muted' }, t('detail.noSession'))
2349
+ : detailSessions.map((entry) =>
2350
+ h(
2351
+ 'button',
2352
+ {
2353
+ key: entry.key,
2354
+ type: 'button',
2355
+ className: 'tb-btn tb-btn-sm',
2356
+ title: entry.sessionId,
2357
+ onClick: () => props.openSession(entry.sessionId),
2358
+ },
2359
+ entry.label + ' · ' + shortSession(entry.sessionId),
2360
+ ),
2361
+ ),
2237
2362
  ),
2238
2363
  ),
2239
2364
  ),
@@ -2737,7 +2862,14 @@ window.__ModuleLoader__.load({
2737
2862
  if (task) loadTaskAttachments(task, attachmentSourcesRef, setAttachmentSources)
2738
2863
  setDetailId(task.id)
2739
2864
  },
2740
- openSession: (task) => openSessionById(task && (task.dispatchedSessionId || task.sessionId)),
2865
+ // 「对话」按钮传目标会话 id;仍兼容传任务对象的旧调用(取 dispatched → creator)。
2866
+ openSession: (target) => {
2867
+ if (typeof target === 'string') {
2868
+ openSessionById(target)
2869
+ return
2870
+ }
2871
+ openSessionById(target && (target.dispatchedSessionId || target.sessionId))
2872
+ },
2741
2873
  startEdit,
2742
2874
  saveEdit,
2743
2875
  setEditing,
@@ -3312,7 +3444,14 @@ window.__ModuleLoader__.load({
3312
3444
  if (task) loadTaskAttachments(task, attachmentSourcesRef, setAttachmentSources)
3313
3445
  setDetailId(task.id)
3314
3446
  },
3315
- openSession: (task) => openSessionById(task && (task.dispatchedSessionId || task.sessionId)),
3447
+ // 「对话」按钮传目标会话 id;仍兼容传任务对象的旧调用(取 dispatched → creator)。
3448
+ openSession: (target) => {
3449
+ if (typeof target === 'string') {
3450
+ openSessionById(target)
3451
+ return
3452
+ }
3453
+ openSessionById(target && (target.dispatchedSessionId || target.sessionId))
3454
+ },
3316
3455
  startEdit,
3317
3456
  saveEdit,
3318
3457
  setEditing,
@@ -3716,6 +3855,9 @@ window.__ModuleLoader__.load({
3716
3855
  fmtClock,
3717
3856
  fmtInterval,
3718
3857
  shortSession,
3858
+ // v0.2.0 任务关联对话:worker 会话判定(assignee 只在能提取到会话 id 时算会话)
3859
+ workerSessionOf,
3860
+ extractSessionId,
3719
3861
  groupTasks,
3720
3862
  statusAccent: STATUS_ACCENT,
3721
3863
  statusOrder: STATUS_ORDER,
package/lib/index.js CHANGED
@@ -67,6 +67,11 @@ const MAX_WORKSPACE_LEADS = 32
67
67
  const MAX_RECEIPT_BATCH = 10
68
68
  /** 完成回执里每条任务结论压成单行后的最大字符数(§1.6)。 */
69
69
  const RECEIPT_RESULT_CHARS = 200
70
+ /**
71
+ * 归档保留期(§1.2 / §2):`archived` 任务在归档满 30 分钟后由宿主自动从看板删除,
72
+ * 不需要用户手动点「删除」。判定见 `pruneArchivedTasks()`。
73
+ */
74
+ export const ARCHIVE_RETENTION_MS = 30 * 60 * 1000
70
75
 
71
76
  /* ------------------------------------------------------------------ *
72
77
  * 粘贴图片附件(v0.2.0,§2.1)
@@ -855,6 +860,73 @@ export function deleteTask(state, id) {
855
860
  return task
856
861
  }
857
862
 
863
+ /** 解析一个 ISO 时间字段;缺失 / 非字符串 / 不可解析都返回 null。 */
864
+ function timeOrNull(value) {
865
+ if (typeof value !== 'string') return null
866
+ const text = value.trim()
867
+ if (text === '') return null
868
+ const ms = Date.parse(text)
869
+ return Number.isFinite(ms) ? ms : null
870
+ }
871
+
872
+ /**
873
+ * 任务的归档时间(§1.2):优先 `archivedAt`,缺失 / 不可解析时回退 `updatedAt` → `createdAt`。
874
+ * 三个都不可用 → null(调用方保守保留,不删)。
875
+ */
876
+ export function archiveTimeOf(task) {
877
+ if (!isPlainObject(task)) return null
878
+ for (const field of ['archivedAt', 'updatedAt', 'createdAt']) {
879
+ const ms = timeOrNull(task[field])
880
+ if (ms !== null) return ms
881
+ }
882
+ return null
883
+ }
884
+
885
+ /**
886
+ * 归档保留期清理(§1.2 / §2):把「归档满 retentionMs」的任务从 state.tasks 移除。纯函数。
887
+ *
888
+ * - 只处理 `status === 'archived'`;`todo` / `doing` / `done` 一律不动;
889
+ * - 归档时间见 `archiveTimeOf()`(`archivedAt` → `updatedAt` → `createdAt`);
890
+ * 三个字段都不可解析时**保守保留**——宁可留一条,也不把时间坏掉的任务误删;
891
+ * - `now - archivedAt >= retentionMs` 即删除(满 30 分钟,边界即删);
892
+ * - **不回退 `state.nextId`**:已删除任务的 id 不复用,避免旧引用指到新任务上;
893
+ * - 原地改写 `state.tasks`(保持数组引用不变),未过期任务保持原顺序。
894
+ *
895
+ * @param {object} state 看板状态
896
+ * @param {number} [now] 当前时间(ms)
897
+ * @param {number} [retentionMs] 保留期(默认 `ARCHIVE_RETENTION_MS`)
898
+ * @returns {{removed:number, ids:string[]}} 删除数量与被删任务 id(board 顺序)
899
+ */
900
+ export function pruneArchivedTasks(state, now = Date.now(), retentionMs = ARCHIVE_RETENTION_MS) {
901
+ const outcome = { removed: 0, ids: [] }
902
+ if (!isPlainObject(state) || !Array.isArray(state.tasks)) return outcome
903
+ const ttl = Number.isFinite(Number(retentionMs)) && Number(retentionMs) >= 0 ? Number(retentionMs) : ARCHIVE_RETENTION_MS
904
+ const kept = []
905
+ for (const task of state.tasks) {
906
+ if (!isPlainObject(task) || task.status !== 'archived') {
907
+ kept.push(task)
908
+ continue
909
+ }
910
+ const archivedAt = archiveTimeOf(task)
911
+ if (archivedAt === null) {
912
+ kept.push(task)
913
+ continue
914
+ }
915
+ if (now - archivedAt >= ttl) {
916
+ outcome.ids.push(task.id)
917
+ outcome.removed += 1
918
+ continue
919
+ }
920
+ kept.push(task)
921
+ }
922
+ if (outcome.removed > 0) {
923
+ // 原地替换内容:其它持有 state.tasks 引用的地方(快照 / 轮询)不会看到旧数组。
924
+ state.tasks.length = 0
925
+ for (const task of kept) state.tasks.push(task)
926
+ }
927
+ return outcome
928
+ }
929
+
858
930
  /* ------------------------------------------------------------------ *
859
931
  * 派单:候选选择 / 目标 Agent / 消息构造
860
932
  * ------------------------------------------------------------------ */
@@ -1920,6 +1992,33 @@ function leadRefFrom(key, mapping, agent, archived = false) {
1920
1992
  }
1921
1993
  }
1922
1994
 
1995
+ /**
1996
+ * 归档保留期清理的落地版(§1.2 / §2):调纯函数 `pruneArchivedTasks`,
1997
+ * **有删除才** `commitBoard`(revision +1);没删除不动盘、不改 revision。
1998
+ *
1999
+ * 调用点:
2000
+ * - `runTick` 在 `pollEnabled` 判断**之前**——轮询暂停时归档也要按时清理;
2001
+ * - `apply()` 启动时清一次(宿主停机期间到期的归档不必等第一个 tick)。
2002
+ * 全程不抛:清理失败只告警,绝不影响派发 / 完成回执。
2003
+ * @returns {number} 删除的任务数
2004
+ */
2005
+ function pruneArchivedBoard(rt, now = Date.now()) {
2006
+ let outcome
2007
+ try {
2008
+ outcome = pruneArchivedTasks(rt.state, now)
2009
+ } catch (error) {
2010
+ console.warn('[taskboard] 归档清理失败:' + describeError(error))
2011
+ return 0
2012
+ }
2013
+ if (outcome.removed === 0) return 0
2014
+ const saved = commitBoard(rt.state, rt.filePath)
2015
+ console.log(
2016
+ '[taskboard] 已删除 ' + outcome.removed + ' 条归档满 30 分钟的任务:' + outcome.ids.join(', ') +
2017
+ (saved.saved ? '' : '(写盘失败,仅在内存中删除)'),
2018
+ )
2019
+ return outcome.removed
2020
+ }
2021
+
1923
2022
  /**
1924
2023
  * 轮询派单入口。每次 tick 末尾都会尝试 flush 一次完成回执(§1.6):
1925
2024
  * 回执投递不占用派发名额(未绑定主会话 / 主会话忙或离线时是 no-op)。
@@ -1938,6 +2037,8 @@ async function runTick(rt, options = {}) {
1938
2037
  reloadBoardIfStale(rt)
1939
2038
  const now = Date.now()
1940
2039
  rt.poll.lastRunAt = iso(now)
2040
+ // 归档清理(§1.2)必须在 pollEnabled 判断之前:暂停派发时归档仍要按时删除。
2041
+ pruneArchivedBoard(rt, now)
1941
2042
  const config = normalizeConfig(rt.state.config)
1942
2043
  if (!config.pollEnabled && !options.taskId) {
1943
2044
  rt.poll.lastSkipReason = 'poll-disabled'
@@ -2976,6 +3077,10 @@ export function apply(ctx) {
2976
3077
  console.warn('[taskboard] 读取看板失败,从空板开始:' + describeError(error))
2977
3078
  }
2978
3079
 
3080
+ // 启动时清一次过期归档(§1.2):宿主停机期间到期的归档不必等第一个 tick。
3081
+ // 只在真的有删除时写盘(pruneArchivedBoard 内部判断),因此没有副作用地复用同一路径。
3082
+ pruneArchivedBoard(runtime, Date.now())
3083
+
2979
3084
  // 模式状态:agentPresets 服务就绪后读名单(预设本身由 bundle patch 声明,不在这里安装)。
2980
3085
  // 预设行的激活是异步的,所以前几次读不到不算失败——最多重试 3 次再告警。
2981
3086
  try {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dsh-dispatch-board",
3
- "version": "0.2.0",
3
+ "version": "0.2.1",
4
4
  "description": "DeepSeek Harness 插件:派单看板 + 派单模式。主 Agent 只拆解/派发/审核/汇报,子 Agent 干活;看板四列(待开始/进行中/已完成/已归档)由轮询自动派发,结论按 Markdown 展示。Dispatcher mode plus a dispatch board for DeepSeek Harness.",
5
5
  "type": "module",
6
6
  "main": "lib/index.js",