dsh-taskbridge 1.6.0 → 1.7.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.
Files changed (66) hide show
  1. package/CHANGELOG.md +10 -1
  2. package/README.md +41 -41
  3. package/THIRD_PARTY_NOTICES.txt +23 -0
  4. package/codex/dsh-taskbridge.toml +140 -71
  5. package/docs/INTRODUCTION.md +44 -22
  6. package/docs/INTRODUCTION.zh.md +32 -26
  7. package/docs/codex-integration.md +90 -10
  8. package/docs/contracts-stage-b.md +4 -4
  9. package/docs/conversation-placement.md +25 -7
  10. package/docs/plugin-market.md +1 -1
  11. package/docs/releases/1.7.0.zh.md +11 -0
  12. package/docs/runtime-compatibility.md +4 -11
  13. package/docs/silent-wait.md +69 -31
  14. package/docs/taskbridge-upgrade.md +1 -1
  15. package/docs/worker-permissions.md +4 -14
  16. package/docs/workspace-grouping.md +126 -0
  17. package/lib/client.d.ts +365 -3
  18. package/lib/client.js +260 -5
  19. package/locale/en.json +1 -1
  20. package/locale/zh.json +1 -1
  21. package/package.json +7 -19
  22. package/src/client/api.js +550 -90
  23. package/src/client/index.jsx +699 -307
  24. package/src/client/sidebar.jsx +6 -5
  25. package/src/client/styles.js +251 -0
  26. package/src/client/task-cleanup.js +304 -0
  27. package/src/client/task-records-table.jsx +217 -0
  28. package/src/client/task-view.js +226 -10
  29. package/src/client/workspace-groups.js +482 -0
  30. package/src/client/workspaces-browser.jsx +770 -0
  31. package/src/codex/agent-ownership.js +270 -0
  32. package/src/codex/installer.js +525 -35
  33. package/src/codex/seam.js +5 -0
  34. package/src/config/defaults.js +63 -17
  35. package/src/config/settings.js +60 -62
  36. package/src/config/validate.js +126 -71
  37. package/src/http/respond.js +8 -0
  38. package/src/http/user-plane.js +197 -2
  39. package/src/index.js +50 -42
  40. package/src/jobs/activity.js +968 -78
  41. package/src/jobs/engine.js +784 -230
  42. package/src/jobs/history.js +188 -12
  43. package/src/jobs/plan-store.js +5 -0
  44. package/src/jobs/record-cleanup.js +196 -0
  45. package/src/jobs/task-tree.js +5 -3
  46. package/src/jobs/title.js +129 -12
  47. package/src/mcp/server.js +16 -8
  48. package/src/mcp/wait.js +64 -5
  49. package/src/runtime/agent-runner.js +53 -15
  50. package/src/runtime/worker-prompt.js +53 -0
  51. package/src/workspace/locks.js +288 -167
  52. package/docs/releases/1.4.0-token-benchmark.zh.md +0 -40
  53. package/docs/releases/1.4.0.zh.md +0 -54
  54. package/docs/releases/1.4.1.zh.md +0 -59
  55. package/docs/releases/1.4.2.zh.md +0 -23
  56. package/docs/releases/1.5.0-token-benchmark.zh.md +0 -73
  57. package/docs/releases/1.5.0.zh.md +0 -51
  58. package/docs/releases/1.5.1.zh.md +0 -70
  59. package/docs/releases/1.5.2.zh.md +0 -30
  60. package/docs/releases/1.5.3-test.1.zh.md +0 -28
  61. package/docs/releases/1.5.3-test.2.zh.md +0 -24
  62. package/docs/releases/1.5.3.zh.md +0 -33
  63. package/docs/releases/1.5.4.zh.md +0 -25
  64. package/src/review/preset.js +0 -868
  65. package/src/review/prompts.js +0 -145
  66. package/src/review/reviewer.js +0 -933
@@ -27,11 +27,12 @@
27
27
  // `plugins.bundle.config` page must keep working on a host without a sidebar — so
28
28
  // nothing in this module is a prerequisite for the settings page.
29
29
  //
30
- // Two public sibling plugins (GT-610/dsh-workspace-organize and
31
- // Larrtroffen/dsh-workspace-folders) occupy or replace the single
32
- // `sidebar.workspaces` slot. This module therefore does NOT register there: it adds
33
- // entries beside whatever those plugins draw, instead of competing for their
34
- // surface. No code is borrowed from them and no package is added.
30
+ // The shell's own workspace/session browsing region belongs to DSH: THIS module never
31
+ // registers there, and no TaskBridge task view replaces it. The grouped workspaces
32
+ // browser is a SEPARATE registration (`workspaces-browser.jsx`, `sidebar.workspaces`,
33
+ // lower priority than the shipped entry) whose identity rules are stated there and in
34
+ // `docs/workspace-grouping.md`; it keeps ordinary Workspaces, and it falls back to the
35
+ // shipped browser whenever it cannot prove what belongs to which project.
35
36
  //
36
37
  // What the panel may and may not do
37
38
  // ---------------------------------
@@ -253,6 +253,50 @@ export const CSS_TEXT = `
253
253
  .dshdg-tree-filters { display: flex; flex-wrap: wrap; gap: 8px; margin-block: 8px; }
254
254
  .dshdg-tree-filters label { display: grid; gap: 2px; flex: 1 1 132px; min-width: 0; }
255
255
  .dshdg-tree-filters label > * { width: 100%; min-width: 0; }
256
+ /* ---- creation-date range ---------------------------------------------------
257
+ Two native date inputs plus a clear button. The inputs stay inside the same
258
+ wrapping filter row, so a narrow window stacks them instead of overflowing;
259
+ the native picker supplies the calendar, so no date widget is shipped. */
260
+ .dshdg-root input[type="date"] {
261
+ width: 100%;
262
+ min-width: 0;
263
+ padding: 5px 8px;
264
+ color: inherit;
265
+ font: inherit;
266
+ background: var(--dsw-alias-bg-base, #f7f8f9);
267
+ border: 1px solid var(--dsw-alias-border-l3, #d3d7dd);
268
+ border-radius: var(--dsw-radius-sm, 6px);
269
+ }
270
+ .dshdg-root input[type="date"]:focus-visible {
271
+ outline: var(--dsw-focus-ring-width, 2px) solid var(--dsw-focus-ring-color, #3457d5);
272
+ outline-offset: 1px;
273
+ }
274
+ .dshdg-root input[type="date"][aria-invalid="true"] { border-color: var(--dsw-alias-label-error, #d92d20); }
275
+ /* The clear control sits on the row's baseline with the inputs, not under them. */
276
+ .dshdg-tree-day-actions { display: flex; align-items: flex-end; flex: 0 1 auto; min-width: 0; }
277
+ .dshdg-tree-day-actions button { padding: 5px 10px; font-size: 12px; }
278
+ .dshdg-record-views { display: flex; flex-wrap: wrap; gap: 4px; }
279
+ .dshdg-record-views button[aria-pressed="true"] { font-weight: 600; background: var(--dsw-alias-interactive-bg-hover, #e9ebee); }
280
+ .dshdg-records-overflow { overflow-x: auto; margin-top: 12px; }
281
+ .dshdg-records-table { width: 100%; border-collapse: collapse; min-width: 36em; }
282
+ .dshdg-records-table th, .dshdg-records-table td {
283
+ padding: 8px 10px; text-align: start; vertical-align: top; border-bottom: 1px solid var(--dshdg-line);
284
+ }
285
+ .dshdg-records-table th { font-size: 12px; white-space: nowrap; }
286
+ .dshdg-records-table td[data-column="title"] { width: 42%; }
287
+ .dshdg-records-table td[data-column="updatedAt"] { font-variant-numeric: tabular-nums; }
288
+ .dshdg-records-table td[data-column="project"] { overflow-wrap: anywhere; }
289
+ .dshdg-record-name { display: flex; flex-direction: column; align-items: flex-start; gap: 2px; }
290
+ .dshdg-records-table .dshdg-record-toggle, .dshdg-records-table .dshdg-record-sort {
291
+ display: inline-flex; align-items: baseline; gap: 6px; text-align: start;
292
+ border: 0; background: transparent; padding: 0; color: inherit; font: inherit;
293
+ }
294
+ .dshdg-records-table .dshdg-record-toggle { font-weight: 600; overflow-wrap: anywhere; }
295
+ .dshdg-records-table .dshdg-record-toggle:hover, .dshdg-records-table .dshdg-record-sort:hover { text-decoration: underline; }
296
+ .dshdg-record-detail { display: grid; gap: 8px; justify-items: start; }
297
+ .dshdg-record-detail p { margin: 0; }
298
+ .dshdg-record-detail .dshdg-tree-execution { width: 100%; }
299
+ .dshdg-record-selection { display: grid; gap: 3px; justify-items: start; }
256
300
  .dshdg-tree-project { border-top: 1px solid var(--dshdg-line); margin-top: 12px; padding-top: 8px; }
257
301
  .dshdg-tree-project h4 { margin: 0; overflow-wrap: anywhere; }
258
302
  .dshdg-tree-project h4 small { font-weight: normal; }
@@ -277,6 +321,26 @@ export const CSS_TEXT = `
277
321
  .dshdg-tree-round-head { display: flex; flex-wrap: wrap; gap: 6px; align-items: center; min-width: 0; }
278
322
  .dshdg-tree-round-head > span { overflow-wrap: anywhere; min-width: 0; }
279
323
  .dshdg-tree-round-head button { padding: 1px 8px; font-size: 12px; }
324
+ /* ---- task-record cleanup ---------------------------------------------------
325
+ One bar above the tree while selection mode is on, one checkbox line per
326
+ execution chain, and a confirmation block that wraps instead of squeezing. The
327
+ warning keeps the error colour because the action is irreversible. */
328
+ .dshdg-cleanup-bar {
329
+ display: flex;
330
+ flex-direction: column;
331
+ gap: 6px;
332
+ margin-block: 8px;
333
+ padding: 8px;
334
+ border: 1px solid var(--dsw-alias-border-l2, #e2e5e9);
335
+ border-radius: 6px;
336
+ min-width: 0;
337
+ }
338
+ .dshdg-cleanup-bar p { margin: 0; overflow-wrap: anywhere; }
339
+ .dshdg-cleanup-row { display: flex; flex-wrap: wrap; gap: 6px; align-items: center; min-width: 0; padding: 2px 0; }
340
+ .dshdg-cleanup-check { display: inline-flex; gap: 6px; align-items: center; min-width: 0; }
341
+ .dshdg-cleanup-row span { overflow-wrap: anywhere; min-width: 0; }
342
+ .dshdg-cleanup-bar .dshdg-row { flex-wrap: wrap; }
343
+
280
344
  @media (max-width: 560px) {
281
345
  /* Narrow window: halve the indent so the deepest level still leaves room for
282
346
  text, and let the round controls wrap under the title instead of squeezing. */
@@ -337,6 +401,193 @@ export const CSS_TEXT = `
337
401
  .dshdg-nav-toggle-row { display: inline-flex; align-items: center; gap: 6px; font-size: 12px; }
338
402
  .dshdg-nav-archived { display: flex; flex-direction: column; gap: 4px; min-width: 0; }
339
403
  .dshdg-nav-icon { display: block; }
404
+ /* ---- grouped workspaces browser (sidebar.workspaces) -----------------------
405
+ This block renders in the shell's workspace cell, which the shell sizes. It
406
+ therefore assumes NOTHING about width: every row is a wrapping flex line, a
407
+ title shrinks and ellipsises, and the actions wrap under the title rather than
408
+ pushing the row wider. Colours, radii and fonts are the shell's own tokens, so
409
+ the tree follows light/dark and the shared menu blur without a second theme. */
410
+ .dshdg-ws {
411
+ display: flex;
412
+ flex-direction: column;
413
+ gap: 6px;
414
+ min-width: 0;
415
+ color: var(--dsw-alias-label-primary, #1f2329);
416
+ font-family: var(--dsw-font-family, system-ui, -apple-system, "Segoe UI", "Microsoft YaHei", sans-serif);
417
+ font-size: 13px;
418
+ line-height: 1.45;
419
+ }
420
+ .dshdg-ws * { box-sizing: border-box; }
421
+ .dshdg-ws p { margin: 0; }
422
+ .dshdg-ws-tree, .dshdg-ws-projects, .dshdg-ws-workspaces, .dshdg-ws-sessions {
423
+ list-style: none;
424
+ margin: 0;
425
+ padding: 0;
426
+ min-width: 0;
427
+ }
428
+ .dshdg-ws-tree, .dshdg-ws-projects, .dshdg-ws-workspaces, .dshdg-ws-sessions { display: flex; flex-direction: column; gap: 1px; }
429
+ /* One indent step per level, and the same dashed guide the task tree uses, so the two
430
+ trees read as one product without sharing a component. */
431
+ .dshdg-ws-projects, .dshdg-ws-workspaces, .dshdg-ws-sessions {
432
+ margin-inline-start: 0.9em;
433
+ padding-inline-start: 8px;
434
+ border-inline-start: 1px dashed var(--dsw-alias-border-l2, #e2e5e9);
435
+ }
436
+ .dshdg-ws-row { display: flex; flex-wrap: wrap; gap: 4px 6px; align-items: center; min-width: 0; padding: 2px 0; }
437
+ .dshdg-ws-caret {
438
+ flex: 0 0 auto;
439
+ width: 18px;
440
+ padding: 0;
441
+ border: 0;
442
+ background: transparent;
443
+ color: inherit;
444
+ font: inherit;
445
+ line-height: 1;
446
+ cursor: pointer;
447
+ opacity: 0.75;
448
+ }
449
+ .dshdg-ws-caret:hover { opacity: 1; }
450
+ .dshdg-ws-title {
451
+ flex: 1 1 5em;
452
+ min-width: 0;
453
+ padding: 0;
454
+ border: 0;
455
+ background: transparent;
456
+ color: inherit;
457
+ font: inherit;
458
+ text-align: start;
459
+ cursor: pointer;
460
+ overflow: hidden;
461
+ text-overflow: ellipsis;
462
+ white-space: nowrap;
463
+ }
464
+ .dshdg-ws-title:hover { text-decoration: underline; }
465
+ .dshdg-ws-root-row .dshdg-ws-title { font-weight: 600; }
466
+ .dshdg-ws-project-row .dshdg-ws-title { font-weight: 500; }
467
+ .dshdg-ws-workspace .dshdg-ws-title { font-weight: 400; }
468
+ .dshdg-ws-count { flex: 0 1 auto; font-size: 11px; color: var(--dsw-alias-label-tertiary, #8a9098); }
469
+ .dshdg-ws-spacer { flex: 1 1 auto; }
470
+ .dshdg-ws-actions { display: inline-flex; flex-wrap: wrap; gap: 2px; min-width: 0; }
471
+ .dshdg-ws button + button { margin: 0; }
472
+ .dshdg-ws-actions button, .dshdg-ws-session-action {
473
+ padding: 0 6px;
474
+ font-size: 11px;
475
+ line-height: 18px;
476
+ border: 1px solid var(--dsw-alias-border-l2, #e2e5e9);
477
+ border-radius: var(--dsw-radius-sm, 6px);
478
+ background: transparent;
479
+ color: inherit;
480
+ cursor: pointer;
481
+ }
482
+ .dshdg-ws-actions button:hover:not(:disabled), .dshdg-ws-session-action:hover:not(:disabled) {
483
+ background: var(--dsw-alias-interactive-bg-hover, #e9ebee);
484
+ }
485
+ .dshdg-ws-actions button:disabled, .dshdg-ws-session-action:disabled { opacity: 0.45; cursor: not-allowed; }
486
+ .dshdg-ws-session { display: flex; flex-wrap: wrap; gap: 4px; align-items: center; min-width: 0; }
487
+ .dshdg-ws-open {
488
+ display: flex;
489
+ flex: 1 1 5em;
490
+ gap: 6px;
491
+ align-items: center;
492
+ min-width: 0;
493
+ padding: 2px 4px;
494
+ border: 0;
495
+ border-radius: var(--dsw-radius-sm, 6px);
496
+ background: transparent;
497
+ color: inherit;
498
+ font: inherit;
499
+ text-align: start;
500
+ cursor: pointer;
501
+ }
502
+ .dshdg-ws-open:hover:not(:disabled) { background: var(--dsw-alias-interactive-bg-hover, #e9ebee); }
503
+ .dshdg-ws-open:disabled { cursor: not-allowed; opacity: 0.6; }
504
+ .dshdg-ws-session-title { min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
505
+ .dshdg-ws-badge {
506
+ flex: 0 0 auto;
507
+ padding: 0 6px;
508
+ border-radius: 999px;
509
+ border: 1px solid var(--dsw-alias-border-l3, #d3d7dd);
510
+ font-size: 10px;
511
+ color: var(--dsw-alias-label-secondary, #61666d);
512
+ }
513
+ /* ---- the per-session marker inside a NATIVE sidebar row --------------------
514
+ Rendered through the host's published child slot (sidebar.workspaces.session.row.action),
515
+ so it sits inside the host's own row. Deliberately tiny and quiet: the operator sees a native
516
+ row with one extra word, never a restyled or reordered one. Host density is respected — the
517
+ host's own row actions are icon buttons, so this only adds an inline label. */
518
+ .dshdg-row-marker {
519
+ display: inline-flex;
520
+ align-items: center;
521
+ flex: 0 0 auto;
522
+ padding: 0 4px;
523
+ border-radius: var(--dsw-radius-sm, 4px);
524
+ border: 1px solid var(--dsw-alias-border-l3, #d3d7dd);
525
+ font-size: 10px;
526
+ line-height: 14px;
527
+ color: var(--dsw-alias-label-secondary, #61666d);
528
+ white-space: nowrap;
529
+ user-select: none;
530
+ }
531
+ .dshdg-row-marker[data-dshdg-row-marker-isolation="worktree"] {
532
+ border-color: var(--dsw-alias-state-business-primary, #12b76a);
533
+ color: var(--dsw-alias-state-business-primary, #12b76a);
534
+ }
535
+ /* ---- the plugin's own grouping surface ------------------------------------
536
+ This is drawn on the plugin's OWN panel, not inside the shell's workspace cell, so it may use
537
+ a full title row; it still wraps instead of pushing a control off a narrow sidebar. */
538
+ .dshdg-ws { display: flex; flex-direction: column; min-width: 0; }
539
+ .dshdg-ws-section {
540
+ padding: 6px 0 2px;
541
+ font-size: 11px;
542
+ color: var(--dsw-alias-label-tertiary, #8a9098);
543
+ }
544
+ /* A project's action bar: the Workspace target picker plus its operations. Both wrap under the
545
+ title so a narrow sidebar never pushes a control off-screen, and the picker is a CONTROL, not
546
+ a level: it stays on the project's own row block. */
547
+ .dshdg-ws-project-actions {
548
+ display: flex;
549
+ flex-wrap: wrap;
550
+ gap: 4px 8px;
551
+ align-items: center;
552
+ min-width: 0;
553
+ margin-inline-start: 1.4em;
554
+ padding: 1px 0 3px;
555
+ }
556
+ .dshdg-ws-target { display: inline-flex; flex-wrap: wrap; gap: 4px; align-items: center; min-width: 0; }
557
+ .dshdg-ws-target select {
558
+ max-width: 14em;
559
+ min-width: 0;
560
+ padding: 0 4px;
561
+ font: inherit;
562
+ font-size: 11px;
563
+ color: inherit;
564
+ background: transparent;
565
+ border: 1px solid var(--dsw-alias-border-l2, #e2e5e9);
566
+ border-radius: var(--dsw-radius-sm, 6px);
567
+ }
568
+ /* The conversations of one project: one indent step, and the same dashed guide as the
569
+ Workspace rows, so the three levels read as one tree. */
570
+ .dshdg-ws-conversations {
571
+ list-style: none;
572
+ margin: 0;
573
+ padding: 0;
574
+ margin-inline-start: 1.4em;
575
+ padding-inline-start: 8px;
576
+ border-inline-start: 1px dashed var(--dsw-alias-border-l2, #e2e5e9);
577
+ display: flex;
578
+ flex-direction: column;
579
+ gap: 1px;
580
+ min-width: 0;
581
+ }
582
+ .dshdg-ws-faint { color: var(--dsw-alias-label-tertiary, #8a9098); font-size: 10px; }
583
+ .dshdg-ws-empty { padding: 2px 4px; font-size: 12px; color: var(--dsw-alias-label-tertiary, #8a9098); }
584
+ .dshdg-ws-hint { font-size: 11px; color: var(--dsw-alias-label-tertiary, #8a9098); padding: 0 2px; }
585
+ .dshdg-ws-warn { font-size: 11px; color: var(--dsw-alias-label-error, #d92d20); padding: 0 2px; }
586
+ .dshdg-ws-archived > summary { cursor: pointer; font-size: 11px; color: var(--dsw-alias-label-tertiary, #8a9098); }
587
+ .dshdg-ws:focus-visible, .dshdg-ws button:focus-visible {
588
+ outline: var(--dsw-focus-ring-width, 2px) solid var(--dsw-focus-ring-color, #3457d5);
589
+ outline-offset: 1px;
590
+ }
340
591
  `;
341
592
 
342
593
  /**
@@ -0,0 +1,304 @@
1
+ // Task-record cleanup: which execution chains may be selected for deletion, what a
2
+ // deletion batch carries, and what the operator is shown before it is sent.
3
+ //
4
+ // Zero Node-only imports and no React: this is the part of "清理记录" that can be
5
+ // reasoned about and tested on its own, while the page (`index.jsx`) only renders
6
+ // the answers computed here. Three rules shape everything:
7
+ //
8
+ // 1. **Identity comes from the host's own tree.** A selectable unit is ONE
9
+ // execution chain (`chainId`, `taskIds`) taken from the projection's
10
+ // `executions` — including a chain attached to a planning node — and the
11
+ // selection carries exactly that chain's ids. Nothing is inferred from a
12
+ // title, a time or a position, and the tree is never re-derived here.
13
+ // 2. **Eligibility is stated, not guessed.** A chain the host marked as still
14
+ // active/undurable, as depended on by another chain, or as holding planning
15
+ // children is listed with `selectable: false` and a reason. A selection only
16
+ // ever contains selectable chains, so a disabled row cannot be sent by accident.
17
+ // The server re-checks every rule; this side only decides what to draw.
18
+ // 3. **The batch is closed.** The request body carries `{ chainId, taskIds }`
19
+ // pairs and nothing else: no time range, no path, and no "delete everything".
20
+
21
+ /** Refusal/notice codes the cleanup surface may produce locally. */
22
+ export const CLEANUP_BLOCKER_LABELS = Object.freeze({
23
+ CLEANUP_CHAIN_LIVE: '仍在执行或未正常结束,不能清理',
24
+ CLEANUP_NOT_DURABLE: '记录尚未写入,不能清理',
25
+ CLEANUP_NEXT_ROUND: '还有续接记录,不能清理',
26
+ CLEANUP_PLAN_CHILDREN: '有规划子节点引用此记录,不能清理',
27
+ CLEANUP_HIERARCHY_CHILDREN: '有子任务引用此记录,不能清理',
28
+ CLEANUP_REVIEW_WRITE: '审查意见正在写入,请稍后重试',
29
+ CLEANUP_UNAVAILABLE: '清理暂不可用',
30
+ CLEANUP_STALE: '任务记录已变化,请重新选择',
31
+ unknown: '暂时无法确认这条记录可以清理',
32
+ 'selection-stale': '任务记录已变化,请重新选择',
33
+ });
34
+
35
+ /** Chinese label for one blocker code, falling back to the raw code. */
36
+ export function cleanupBlockerLabel(code) {
37
+ if (typeof code !== 'string' || code === '') return CLEANUP_BLOCKER_LABELS.unknown;
38
+ return CLEANUP_BLOCKER_LABELS[code] ?? String(code);
39
+ }
40
+
41
+ /**
42
+ * How many chains one request may name. Mirrors `MAX_CLEANUP_CHAINS` in
43
+ * `src/jobs/record-cleanup.js`; the host refuses a larger batch rather than
44
+ * silently dropping the rest, and the page refuses it before sending.
45
+ */
46
+ export const MAX_CLEANUP_CHAINS = 100;
47
+
48
+ /** Chains of one node: a plan node carries `executions`, a task node its own `chain`. */
49
+ function chainsOf(node) {
50
+ if (node === null || typeof node !== 'object') return [];
51
+ if (Array.isArray(node.executions)) return node.executions;
52
+ return node.chain !== null && typeof node.chain === 'object' ? [node.chain] : [];
53
+ }
54
+
55
+ /** One chain whose identity the page can key, or `null`. */
56
+ function selectableChain(chain) {
57
+ if (chain === null || typeof chain !== 'object') return null;
58
+ const chainId = typeof chain.chainId === 'string' && chain.chainId !== '' ? chain.chainId : null;
59
+ const taskIds = Array.isArray(chain.taskIds) ? chain.taskIds.filter((id) => typeof id === 'string' && id !== '') : [];
60
+ if (chainId === null || taskIds.length === 0) return null;
61
+ return { chainId, taskIds };
62
+ }
63
+
64
+ /**
65
+ * Every execution chain of one rendered tree, in pre-order, with its eligibility.
66
+ *
67
+ * A chain that appears twice (it cannot in one projection, but a host could answer a
68
+ * duplicated node) is listed once, by `chainId`: the selection is a set of chains,
69
+ * and a duplicate row would let one chain be counted twice in the confirmation.
70
+ * @param {object | null} view - `taskTreeView(...)` result.
71
+ * @returns {object[]} `{ chainId, taskIds, roundCount, title, planNodeId, parentNodeId,
72
+ * selectable, blockers, reason, nodeId }` entries.
73
+ */
74
+ export function cleanupCandidates(view) {
75
+ const out = [];
76
+ const seen = new Set();
77
+ const walk = (node) => {
78
+ for (const chain of chainsOf(node)) {
79
+ const identity = selectableChain(chain);
80
+ if (identity === null || seen.has(identity.chainId)) continue;
81
+ seen.add(identity.chainId);
82
+ const blockers = Array.isArray(chain.cleanup?.blockers)
83
+ ? chain.cleanup.blockers.filter((code) => typeof code === 'string' && code !== '')
84
+ : [];
85
+ const eligible = chain.cleanup?.eligible === true;
86
+ const selectable = eligible && blockers.length === 0;
87
+ out.push({
88
+ chainId: identity.chainId,
89
+ taskIds: identity.taskIds,
90
+ roundCount: Number.isFinite(chain.roundCount) ? chain.roundCount : identity.taskIds.length,
91
+ title: typeof chain.displayTitle === 'string' && chain.displayTitle !== ''
92
+ ? chain.displayTitle
93
+ : (typeof chain.title === 'string' && chain.title !== '' ? chain.title : '未记录标题'),
94
+ planNodeId: typeof chain.planNodeId === 'string' ? chain.planNodeId : null,
95
+ parentNodeId: typeof chain.parentNodeId === 'string' ? chain.parentNodeId : null,
96
+ nodeId: typeof node?.nodeId === 'string' ? node.nodeId : null,
97
+ nodeKind: typeof node?.kind === 'string' ? node.kind : 'unknown',
98
+ selectable,
99
+ blockers,
100
+ reason: selectable
101
+ ? null
102
+ : (blockers.length > 0
103
+ ? blockers.map(cleanupBlockerLabel).join(';')
104
+ : cleanupBlockerLabel('unknown')),
105
+ });
106
+ }
107
+ for (const child of Array.isArray(node?.children) ? node.children : []) walk(child);
108
+ };
109
+ for (const root of Array.isArray(view?.roots) ? view.roots : []) walk(root);
110
+ return out;
111
+ }
112
+
113
+ /**
114
+ * The cleanup state of one candidate after a refresh.
115
+ *
116
+ * `selection-stale` is the one blocker this module adds: the operator selected a
117
+ * chain, a background refresh replaced the tree, and the chain's rounds are no
118
+ * longer exactly what was selected. Sending it anyway would delete a round the
119
+ * operator never saw, so it is made unselectable instead.
120
+ * @param {object} candidate - entry from {@link cleanupCandidates}.
121
+ * @param {string[]} selectedTaskIds - the ids that were selected for this chain.
122
+ * @returns {object} the candidate, possibly with an added `selection-stale` blocker.
123
+ */
124
+ export function withSelectionState(candidate, selectedTaskIds) {
125
+ if (!Array.isArray(selectedTaskIds) || !candidate.selectable) return candidate;
126
+ const same = selectedTaskIds.length === candidate.taskIds.length
127
+ && selectedTaskIds.every((id, index) => id === candidate.taskIds[index]);
128
+ if (same) return candidate;
129
+ const blockers = [...candidate.blockers, 'selection-stale'];
130
+ return {
131
+ ...candidate,
132
+ selectable: false,
133
+ blockers,
134
+ reason: blockers.map(cleanupBlockerLabel).join(';'),
135
+ };
136
+ }
137
+
138
+ /**
139
+ * Drop selections that no longer name a selectable chain of the current tree.
140
+ *
141
+ * Kept as a pure function so the page can run it on every render: a selection is
142
+ * `{ [chainId]: taskIds }`, and anything this returns is safe to send.
143
+ * @param {object} selection - chain id -> selected task ids.
144
+ * @param {object[]} candidates - current candidates.
145
+ * @returns {{ selection: object, dropped: string[] }} the surviving selection and the ids dropped.
146
+ */
147
+ export function pruneCleanupSelection(selection, candidates) {
148
+ const byId = new Map(candidates.map((candidate) => [candidate.chainId, candidate]));
149
+ const next = {};
150
+ const dropped = [];
151
+ for (const [chainId, ids] of Object.entries(selection ?? {})) {
152
+ const candidate = byId.get(chainId);
153
+ if (candidate === undefined || !candidate.selectable) {
154
+ dropped.push(chainId);
155
+ continue;
156
+ }
157
+ const checked = withSelectionState(candidate, ids);
158
+ if (!checked.selectable) {
159
+ dropped.push(chainId);
160
+ continue;
161
+ }
162
+ next[chainId] = candidate.taskIds.slice();
163
+ }
164
+ return { selection: next, dropped };
165
+ }
166
+
167
+ /**
168
+ * Toggle one chain in a selection.
169
+ * @param {object} selection - chain id -> selected task ids.
170
+ * @param {object} candidate - the chain to toggle.
171
+ * @returns {object} the new selection (a new object; the input is not mutated).
172
+ */
173
+ export function toggleCleanupSelection(selection, candidate) {
174
+ const next = { ...(selection ?? {}) };
175
+ if (candidate === null || typeof candidate !== 'object' || candidate.selectable !== true) return next;
176
+ if (Array.isArray(next[candidate.chainId])) delete next[candidate.chainId];
177
+ else next[candidate.chainId] = candidate.taskIds.slice();
178
+ return next;
179
+ }
180
+
181
+ /**
182
+ * What the confirmation says BEFORE anything is sent.
183
+ *
184
+ * The numbers are computed from the selection and the current tree, so the text can
185
+ * never claim more (or fewer) chains and rounds than the request will name.
186
+ * @param {object} selection - chain id -> selected task ids.
187
+ * @param {object[]} candidates - current candidates.
188
+ * @returns {{ chains: number, rounds: number, empty: boolean, executable: boolean, problems: string[] }} summary.
189
+ */
190
+ export function cleanupSelectionSummary(selection, candidates) {
191
+ const byId = new Map(candidates.map((candidate) => [candidate.chainId, candidate]));
192
+ let chains = 0;
193
+ let rounds = 0;
194
+ const problems = [];
195
+ for (const [chainId, ids] of Object.entries(selection ?? {})) {
196
+ const candidate = byId.get(chainId);
197
+ if (candidate === undefined) {
198
+ problems.push('所选执行链已不在当前任务树中,请重新选择');
199
+ continue;
200
+ }
201
+ if (!candidate.selectable || !withSelectionState(candidate, ids).selectable) {
202
+ problems.push(`执行链「${candidate.title}」已不能清理:${candidate.reason ?? cleanupBlockerLabel('unknown')}`);
203
+ continue;
204
+ }
205
+ chains += 1;
206
+ rounds += candidate.taskIds.length;
207
+ }
208
+ if (chains > MAX_CLEANUP_CHAINS) {
209
+ problems.push(`一次最多清理 ${MAX_CLEANUP_CHAINS} 条执行链,请分批处理`);
210
+ }
211
+ if (chains === 0 && problems.length === 0) problems.push('请先勾选要清理的执行链');
212
+ return { chains, rounds, empty: chains === 0, executable: problems.length === 0, problems };
213
+ }
214
+
215
+ /**
216
+ * Project a selection onto the exact request body the host accepts.
217
+ *
218
+ * One entry per selected chain: its id and that chain's exact task ids, in tree
219
+ * order. Anything that is not a selectable chain of the CURRENT tree is refused
220
+ * here instead of being sent and refused by the host.
221
+ * @param {object} selection - chain id -> selected task ids.
222
+ * @param {object[]} candidates - current candidates.
223
+ * @returns {{ body: { chains: Array<{ chainId: string, taskIds: string[] }> }, problems: string[] }} request.
224
+ */
225
+ export function cleanupRequest(selection, candidates) {
226
+ const byId = new Map(candidates.map((candidate) => [candidate.chainId, candidate]));
227
+ const summary = cleanupSelectionSummary(selection, candidates);
228
+ if (!summary.executable) return { body: { chains: [] }, problems: summary.problems };
229
+ const chains = [];
230
+ for (const [chainId, ids] of Object.entries(selection)) {
231
+ const candidate = byId.get(chainId);
232
+ chains.push({ chainId: candidate.chainId, taskIds: candidate.taskIds.slice() });
233
+ }
234
+ return { body: { chains }, problems: [] };
235
+ }
236
+
237
+ /**
238
+ * Stable key so a selection survives a refresh: `chainId` -> the ids selected.
239
+ * @param {object[]} candidates - current candidates.
240
+ * @param {object} selection - chain id -> selected task ids.
241
+ * @returns {string} a comparable fingerprint of the selection.
242
+ */
243
+ export function cleanupSelectionKey(candidates, selection) {
244
+ const parts = [];
245
+ for (const candidate of candidates) {
246
+ if (Array.isArray(selection?.[candidate.chainId])) parts.push(`${candidate.chainId}:${candidate.taskIds.join(',')}`);
247
+ }
248
+ return parts.join('|');
249
+ }
250
+
251
+ /**
252
+ * The task ids a completed deletion removed, as the host reported them.
253
+ *
254
+ * Read defensively: a host that answers `deleted: []` removed nothing and must not
255
+ * be shown as a success, so the caller decides from `chains`/`tasks` counts.
256
+ * @param {object | null} outcome - `{ chains, tasks, deletedTaskIds }`.
257
+ * @returns {{ chains: number, rounds: number, deletedTaskIds: string[] }} normalized counts.
258
+ */
259
+ export function cleanupOutcome(outcome) {
260
+ const deleted = Array.isArray(outcome?.deletedTaskIds)
261
+ ? outcome.deletedTaskIds.filter((id) => typeof id === 'string' && id !== '')
262
+ : [];
263
+ const chains = Number.isFinite(outcome?.chains) ? outcome.chains : 0;
264
+ return { chains, rounds: deleted.length, deletedTaskIds: deleted };
265
+ }
266
+
267
+ /**
268
+ * Plain-Chinese description of one completed cleanup.
269
+ * @param {object | null} outcome - the host's success body.
270
+ * @returns {string} one sentence; empty when nothing was removed.
271
+ */
272
+ export function describeCleanupOutcome(outcome) {
273
+ const summary = cleanupOutcome(outcome);
274
+ if (summary.chains === 0 && summary.rounds === 0) return '';
275
+ return `已永久删除 ${summary.chains} 条执行链、共 ${summary.rounds} 轮的任务记录与审查意见;`
276
+ + 'DSH 会话、工作区与源文件均未删除。';
277
+ }
278
+
279
+ /**
280
+ * The standing warning shown next to the confirm button.
281
+ * @returns {string} Chinese text.
282
+ */
283
+ export function cleanupWarningText() {
284
+ return '删除任务记录后不可恢复,也无法按这些任务编号查询或续接。';
285
+ }
286
+
287
+ /**
288
+ * A detached copy of the projection, for one caller that must not see bindings change.
289
+ * @returns {object} the seven functions plus the bound.
290
+ */
291
+ export function cleanupProjection() {
292
+ return {
293
+ candidates: cleanupCandidates,
294
+ request: cleanupRequest,
295
+ outcome: cleanupOutcome,
296
+ describe: describeCleanupOutcome,
297
+ selectionSummary: cleanupSelectionSummary,
298
+ prune: pruneCleanupSelection,
299
+ toggle: toggleCleanupSelection,
300
+ blockerLabel: cleanupBlockerLabel,
301
+ warning: cleanupWarningText,
302
+ MAX_CHAINS: MAX_CLEANUP_CHAINS,
303
+ };
304
+ }