@dsh-workbench/shared 1.5.5-rc.0 → 1.5.6

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 (93) hide show
  1. package/dist/api/dsh.d.ts +9 -1
  2. package/dist/components/card/ApprovalPlan.css +3 -0
  3. package/dist/components/card/ApprovalPlan.js +7 -2
  4. package/dist/components/card/LongTaskRow.css +32 -0
  5. package/dist/components/card/LongTaskRow.d.ts +8 -2
  6. package/dist/components/card/LongTaskRow.js +26 -5
  7. package/dist/components/card/board-module.d.ts +35 -4
  8. package/dist/components/card/use-text-marquee.d.ts +15 -0
  9. package/dist/components/card/use-text-marquee.js +66 -0
  10. package/dist/components/feedback/ProgressBar.css +56 -0
  11. package/dist/components/feedback/ProgressBar.d.ts +25 -0
  12. package/dist/components/feedback/ProgressBar.js +22 -0
  13. package/dist/config/constants.d.ts +2 -0
  14. package/dist/config/constants.js +2 -0
  15. package/dist/contract/guards.d.ts +1 -0
  16. package/dist/contract/guards.js +3 -0
  17. package/dist/contract/long-task.d.ts +56 -0
  18. package/dist/contract/long-task.js +41 -0
  19. package/dist/contract/protocol/long-tasks.d.ts +8 -2
  20. package/dist/contract/unit.d.ts +1 -0
  21. package/dist/host/diy/diy-preset.d.ts +1 -1
  22. package/dist/host/diy/diy-preset.js +3 -3
  23. package/dist/host/flow/create.d.ts +2 -1
  24. package/dist/host/flow/create.js +6 -0
  25. package/dist/host/flow/dsh-journal.d.ts +3 -0
  26. package/dist/host/flow/dsh-journal.js +55 -0
  27. package/dist/host/flow/journal.d.ts +20 -0
  28. package/dist/host/flow/journal.js +30 -0
  29. package/dist/host/flow/open.js +43 -26
  30. package/dist/host/flow/task.js +12 -1
  31. package/dist/host/flow/types.d.ts +5 -0
  32. package/dist/host/platform/integration/browser-launch.d.ts +1 -0
  33. package/dist/host/platform/integration/browser-launch.js +9 -2
  34. package/dist/host/profile/bundle-manager.js +4 -6
  35. package/dist/host/unit/long-task-host.d.ts +18 -0
  36. package/dist/host/unit/long-task-host.js +44 -0
  37. package/dist/host/unit/long-task-progress.d.ts +3 -0
  38. package/dist/host/unit/long-task-progress.js +32 -0
  39. package/dist/host/unit/long-task.d.ts +14 -38
  40. package/dist/host/unit/long-task.js +95 -94
  41. package/dist/host/unit/unit-face.d.ts +2 -1
  42. package/dist/host/unit/with-intervention-retry.d.ts +20 -0
  43. package/dist/host/unit/with-intervention-retry.js +45 -0
  44. package/dist/host.d.ts +30 -19
  45. package/dist/host.js +30 -19
  46. package/dist/i18n/en.js +33 -7
  47. package/dist/i18n/index.d.ts +33 -7
  48. package/dist/i18n/zh.d.ts +32 -6
  49. package/dist/i18n/zh.js +32 -6
  50. package/dist/index.d.ts +4 -2
  51. package/dist/index.js +4 -2
  52. package/package.json +2 -1
  53. package/src/api/dsh.ts +12 -1
  54. package/src/components/card/ApprovalPlan.css +3 -0
  55. package/src/components/card/ApprovalPlan.tsx +9 -2
  56. package/src/components/card/LongTaskRow.css +32 -0
  57. package/src/components/card/LongTaskRow.tsx +51 -4
  58. package/src/components/card/__tests__/ApprovalPlan.test.tsx +50 -0
  59. package/src/components/card/__tests__/long-task-row.test.ts +64 -2
  60. package/src/components/card/board-module.ts +36 -4
  61. package/src/components/card/use-text-marquee.ts +79 -0
  62. package/src/components/feedback/ProgressBar.css +56 -0
  63. package/src/components/feedback/ProgressBar.tsx +60 -0
  64. package/src/config/constants.ts +3 -0
  65. package/src/contract/__tests__/long-task.test.ts +43 -1
  66. package/src/contract/guards.ts +4 -0
  67. package/src/contract/long-task.ts +75 -0
  68. package/src/contract/protocol/long-tasks.ts +9 -1
  69. package/src/contract/unit.ts +1 -0
  70. package/src/host/diy/__tests__/diy-preset.test.ts +2 -0
  71. package/src/host/diy/diy-preset.ts +3 -2
  72. package/src/host/flow/__tests__/dsh-journal.test.ts +56 -0
  73. package/src/host/flow/__tests__/flow-create.test.ts +41 -1
  74. package/src/host/flow/__tests__/flow-open.test.ts +32 -0
  75. package/src/host/flow/create.ts +8 -5
  76. package/src/host/flow/dsh-journal.ts +74 -0
  77. package/src/host/flow/journal.ts +46 -0
  78. package/src/host/flow/open.ts +41 -26
  79. package/src/host/flow/task.ts +11 -1
  80. package/src/host/flow/types.ts +6 -0
  81. package/src/host/platform/integration/__tests__/browser-launch.test.ts +27 -0
  82. package/src/host/platform/integration/browser-launch.ts +15 -3
  83. package/src/host/profile/bundle-manager.ts +4 -7
  84. package/src/host/unit/__tests__/long-task.test.ts +203 -2
  85. package/src/host/unit/long-task-host.ts +64 -0
  86. package/src/host/unit/long-task-progress.ts +37 -0
  87. package/src/host/unit/long-task.ts +112 -126
  88. package/src/host/unit/unit-face.ts +2 -1
  89. package/src/host/unit/with-intervention-retry.ts +55 -0
  90. package/src/host.ts +31 -20
  91. package/src/i18n/en.ts +33 -7
  92. package/src/i18n/zh.ts +32 -6
  93. package/src/index.ts +4 -2
@@ -1,10 +1,15 @@
1
1
  import { epochOf, now as clockNow, nowIso } from '../runtime/time.js';
2
+ import { derivedLongTaskProgress, nextLongTaskProgress } from './long-task-progress.js';
2
3
  import {
3
4
  LONG_TASK_HISTORY_MAX,
4
5
  LONG_TASK_LINK_MAX,
5
- runsRouteOf,
6
+ longTaskPlanTotalOf,
7
+ longTaskPlanStepsOf,
6
8
  type LongTaskIntervention,
7
9
  type LongTaskLink,
10
+ type LongTaskPlan,
11
+ type LongTaskPlanStep,
12
+ type LongTaskProgress,
8
13
  type LongTaskStatus,
9
14
  type LongTaskStep,
10
15
  type LongTaskStepRow,
@@ -44,6 +49,9 @@ export interface LongTaskBase {
44
49
  readonly startedAt: string;
45
50
  readonly at: string;
46
51
  readonly session?: string | undefined;
52
+ readonly seen?: boolean | undefined;
53
+ readonly progress?: LongTaskProgress | undefined;
54
+ readonly plan?: readonly LongTaskPlanStep[] | undefined;
47
55
  }
48
56
 
49
57
  export interface LongTaskStart {
@@ -54,7 +62,7 @@ export interface LongTaskStart {
54
62
  }
55
63
 
56
64
  export interface LongTaskChange<S extends LongTaskBase> {
57
- readonly kind: 'start' | 'step' | 'settle';
65
+ readonly kind: 'start' | 'step' | 'progress' | 'settle' | 'seen';
58
66
  readonly snapshot: S;
59
67
  readonly step?: LongTaskStep | undefined;
60
68
  readonly by?: string | undefined;
@@ -62,10 +70,13 @@ export interface LongTaskChange<S extends LongTaskBase> {
62
70
 
63
71
  /** 长任务怎么跑、怎么投影成面板那一行。 */
64
72
  export interface LongTaskDeps<S extends LongTaskBase> {
65
- /** 真正执行任务:payload 来自 start,onStep 报步骤,signal 被 abort 时尽快退出。 */
73
+ /** 这一趟打算走几步;给了就有进度条(外壳从步骤事件自己数),没给就不画。 */
74
+ readonly plan?: LongTaskPlan | undefined;
75
+ /** 真正执行任务:payload 来自 start,onStep 报步骤,onProgress 报走到哪了,signal 被 abort 时尽快退出。 */
66
76
  readonly run: (options: {
67
77
  readonly payload: unknown;
68
78
  readonly onStep: (step: LongTaskStep) => void;
79
+ readonly onProgress: (progress: LongTaskProgress) => void;
69
80
  readonly signal: AbortSignal;
70
81
  }) => Promise<unknown>;
71
82
  readonly step?: ((step: LongTaskStep, current: S) => Partial<S>) | undefined;
@@ -102,8 +113,12 @@ export interface LongTaskRegistry<S extends LongTaskBase> {
102
113
  current(workspace: string): readonly Readonly<Record<string, unknown>>[];
103
114
  /** 现在活着的每一趟,不分工作区 / 目标;给侧栏状态点用。 */
104
115
  live(): readonly S[];
116
+ /** 现在活着的,加上最近 `recentMs` 内刚收尾的每一趟;给左栏那张全局长任务列表用。 */
117
+ recent(): readonly S[];
105
118
  /** 请求停止:把中断信号转给生产方;没有这趟或已经结束回 false。 */
106
119
  stop(id: string, by?: string): boolean;
120
+ /** 把已收尾的一趟标成「看过」;活着 / 不认识回 false。不刷新 `at`。 */
121
+ seen(id: string): boolean;
107
122
  recheck(id: string): boolean;
108
123
  /** 交回执:只认 intervention.fields 里的 key,必填缺失回 false。 */
109
124
  answer(id: string, values: Readonly<Record<string, string>>): boolean;
@@ -148,6 +163,9 @@ export function longTaskRowBase(snapshot: LongTaskBase): LongTaskRowBase {
148
163
  'loginUrl',
149
164
  'why',
150
165
  'session',
166
+ 'seen',
167
+ 'progress',
168
+ 'plan',
151
169
  ]),
152
170
  };
153
171
  }
@@ -157,6 +175,9 @@ export function createLongTaskRegistry<S extends LongTaskBase>(deps: LongTaskDep
157
175
 
158
176
  const runs = new Map<string, LongTaskBase>();
159
177
  const controls = new Map<string, AbortController>();
178
+ const reported = new Set<string>();
179
+ const counted = new Set<string>();
180
+ const warned = new Set<string>();
160
181
  const now = (): string => (bound.now ?? nowIso)();
161
182
  let counter = 0;
162
183
  const nextId = (): string => (bound.nextId ?? (() => `run-${clockNow()}-${(counter += 1)}`))();
@@ -181,11 +202,39 @@ export function createLongTaskRegistry<S extends LongTaskBase>(deps: LongTaskDep
181
202
  const patch = (id: string, next: Partial<LongTaskBase>, change: Omit<LongTaskChange<S>, 'snapshot'>): void => {
182
203
  const current = runs.get(id);
183
204
  if (current === undefined) return;
184
- const patched = { ...current, ...next, at: now() };
205
+ const resolved =
206
+ next.progress === undefined
207
+ ? current.progress
208
+ : (nextLongTaskProgress(next.progress, current.progress) ?? undefined);
209
+ const patched: { -readonly [K in keyof LongTaskBase]: LongTaskBase[K] } = {
210
+ ...current,
211
+ ...next,
212
+ at: now(),
213
+ };
214
+ if (resolved === undefined) delete patched.progress;
215
+ else patched.progress = resolved;
185
216
  runs.set(id, patched);
186
217
  notify({ ...change, snapshot: patched as S });
187
218
  };
188
219
 
220
+ const setProgress = (id: string, next: LongTaskProgress | undefined): void => {
221
+ const current = runs.get(id);
222
+ if (current === undefined) return;
223
+ const resolved = next === undefined ? null : nextLongTaskProgress(next, current.progress);
224
+ if (resolved === null) {
225
+ if (current.progress === undefined) return;
226
+ const cleared: { -readonly [K in keyof LongTaskBase]: LongTaskBase[K] } = { ...current };
227
+ delete cleared.progress;
228
+ runs.set(id, cleared);
229
+ notify({ kind: 'progress', snapshot: cleared as S });
230
+ return;
231
+ }
232
+ if (current.progress?.done === resolved.done && current.progress.total === resolved.total) return;
233
+ const patched = { ...current, progress: resolved };
234
+ runs.set(id, patched);
235
+ notify({ kind: 'progress', snapshot: patched as S });
236
+ };
237
+
189
238
  return {
190
239
  get resumable() {
191
240
  return bound.recheck !== undefined;
@@ -228,11 +277,20 @@ export function createLongTaskRegistry<S extends LongTaskBase>(deps: LongTaskDep
228
277
  return [...runs.values()].filter((snapshot) => isLiveStatus(snapshot.status)) as S[];
229
278
  },
230
279
 
280
+ recent() {
281
+ const floor = epochOf(now()) - recentMs();
282
+ return [...runs.values()].filter(
283
+ (snapshot) => isLiveStatus(snapshot.status) || epochOf(snapshot.at) >= floor,
284
+ ) as S[];
285
+ },
286
+
231
287
  start(options) {
232
288
  const running = this.active(options.workspace, options.target);
233
289
  if (running !== null) return running;
234
290
  const id = nextId();
235
291
  const startedAt = now();
292
+ const planTotal = bound.plan === undefined ? 0 : longTaskPlanTotalOf(bound.plan);
293
+ const planSteps = bound.plan === undefined ? [] : longTaskPlanStepsOf(bound.plan);
236
294
  runs.set(id, {
237
295
  id,
238
296
  workspace: options.workspace,
@@ -243,6 +301,9 @@ export function createLongTaskRegistry<S extends LongTaskBase>(deps: LongTaskDep
243
301
  history: [],
244
302
  startedAt,
245
303
  at: startedAt,
304
+ seen: false,
305
+ ...(planTotal > 0 ? { progress: { done: 0, total: planTotal } } : {}),
306
+ ...(planSteps.length === 0 ? {} : { plan: planSteps }),
246
307
  ...(options.session === undefined ? {} : { session: options.session }),
247
308
  });
248
309
  notify({ kind: 'start', snapshot: runs.get(id) as S });
@@ -272,12 +333,28 @@ export function createLongTaskRegistry<S extends LongTaskBase>(deps: LongTaskDep
272
333
  const steps = history.map(
273
334
  (row) => `${row.kind === 'fail' ? '✗ ' : ''}${row.text}`,
274
335
  );
275
- const extra = bound.step?.(step, current) ?? {};
336
+ const extra: Partial<S> = bound.step?.(step, current) ?? {};
337
+ if (extra.progress !== undefined) reported.add(id);
338
+ const derived = reported.has(id)
339
+ ? undefined
340
+ : derivedLongTaskProgress(history, planTotal, planSteps);
341
+ if (derived !== undefined) counted.add(id);
342
+ if (planTotal === 0 && !reported.has(id) && !warned.has(id)) {
343
+ const ids = new Set(history.map((row) => row.stepId));
344
+ if (ids.size >= 3) {
345
+ warned.add(id);
346
+ console.warn(
347
+ `[dsh-workbench] 长任务(${current.target})已经报了 ${String(ids.size)} 个步骤却没有计划 —— ` +
348
+ '行上没有进度条:在 longTasks.create 里给一次 plan({ steps } 或 { total }),步数由外壳自己数',
349
+ );
350
+ }
351
+ }
352
+ const next = derived === undefined ? extra : { ...extra, progress: derived };
276
353
  if (step.intervention !== undefined) {
277
354
  patch(
278
355
  id,
279
356
  {
280
- ...extra,
357
+ ...next,
281
358
  status: 'needs-human',
282
359
  step: step.text,
283
360
  steps,
@@ -293,7 +370,7 @@ export function createLongTaskRegistry<S extends LongTaskBase>(deps: LongTaskDep
293
370
  patch(
294
371
  id,
295
372
  {
296
- ...extra,
373
+ ...next,
297
374
  status: 'needs-login',
298
375
  step: step.text,
299
376
  steps,
@@ -307,7 +384,7 @@ export function createLongTaskRegistry<S extends LongTaskBase>(deps: LongTaskDep
307
384
  patch(
308
385
  id,
309
386
  {
310
- ...extra,
387
+ ...next,
311
388
  status: 'running',
312
389
  step: step.text,
313
390
  steps,
@@ -322,17 +399,31 @@ export function createLongTaskRegistry<S extends LongTaskBase>(deps: LongTaskDep
322
399
  const control = new AbortController();
323
400
  controls.set(id, control);
324
401
 
402
+ const onProgress = (next: LongTaskProgress): void => {
403
+ if (live(id) === null) return;
404
+ reported.add(id);
405
+ counted.add(id);
406
+ setProgress(id, next);
407
+ };
408
+
325
409
  void bound
326
- .run({ payload: options.payload, onStep, signal: control.signal })
410
+ .run({ payload: options.payload, onStep, onProgress, signal: control.signal })
327
411
  .then((result) => {
328
412
  const current = live(id);
329
413
  if (current === null) return;
330
414
  controls.delete(id);
415
+ const outcome: Partial<S> = bound.outcome?.(result, current) ?? {};
416
+ const filled =
417
+ outcome.progress ??
418
+ (current.progress === undefined
419
+ ? undefined
420
+ : { done: current.progress.total, total: current.progress.total });
331
421
  patch(
332
422
  id,
333
423
  {
424
+ ...outcome,
334
425
  status: 'ok',
335
- ...(bound.outcome?.(result, current) ?? {}),
426
+ ...(filled === undefined ? {} : { progress: filled }),
336
427
  intervention: undefined,
337
428
  loginUrl: undefined,
338
429
  },
@@ -401,6 +492,16 @@ export function createLongTaskRegistry<S extends LongTaskBase>(deps: LongTaskDep
401
492
  return true;
402
493
  },
403
494
 
495
+ seen(id) {
496
+ const current = runs.get(id);
497
+ if (current === undefined || isLiveStatus(current.status)) return false;
498
+ if (current.seen === true) return true;
499
+ const patched = { ...current, seen: true };
500
+ runs.set(id, patched);
501
+ notify({ kind: 'seen', snapshot: patched as S });
502
+ return true;
503
+ },
504
+
404
505
  snapshot(id) {
405
506
  return (runs.get(id) as S | undefined) ?? null;
406
507
  },
@@ -414,121 +515,6 @@ export function createLongTaskRegistry<S extends LongTaskBase>(deps: LongTaskDep
414
515
  },
415
516
  };
416
517
  }
518
+ export { withInterventionRetry, type InterventionRetryOptions } from './with-intervention-retry.js';
417
519
 
418
- /**
419
- * `withInterventionRetry` 的口径:最多试几次、每轮先等多久(也可按轮次算)、怎么等。
420
- */
421
- export interface InterventionRetryOptions {
422
- readonly attempts?: number | undefined;
423
- readonly delayMs?: number | ((attempt: number) => number) | undefined;
424
- readonly sleep?: ((ms: number) => Promise<void>) | undefined;
425
- }
426
-
427
- const INTERVENTION_RETRY_ATTEMPTS = 5;
428
-
429
- const INTERVENTION_RETRY_DELAY_MS = 1500;
430
520
 
431
- function defaultInterventionSleep(ms: number): Promise<void> {
432
- return new Promise((resolve) => setTimeout(resolve, Math.max(0, ms)));
433
- }
434
-
435
- /**
436
- * 把「看一眼接上没有」包成能交给 `longTasks.create({ recheck })` 的 handler:
437
- * 用户在人工干预节点上按「我处理完了」时先立刻试一次,没接上就有界地再试(第 2 轮起每轮先等)。
438
- * `check` 返回 true = 已经接上,不用再试;抛错算没接上、继续下一轮。
439
- * 同一 id 上一轮还在飞时再按直接忽略;成功 / 用尽都清单飞;**永不抛错**(fire-and-forget)。
440
- * @param check 看一眼接上没有,可同步可异步。
441
- * @param options 轮次上限(默认 5)、第几次等待等多久(默认 1500ms,可给函数,从第 1 次等待起算)、怎么等(默认真等,测试可注入假的)。
442
- * @returns 一条同签名的 handler,直接交给 `const longTasks = create({ recheck })`。
443
- */
444
- export function withInterventionRetry<S extends LongTaskBase>(
445
- check: (current: S) => boolean | Promise<boolean>,
446
- options: InterventionRetryOptions = {},
447
- ): (current: S) => void {
448
- const attempts = options.attempts ?? INTERVENTION_RETRY_ATTEMPTS;
449
- const delayMs = options.delayMs ?? INTERVENTION_RETRY_DELAY_MS;
450
- const sleep = options.sleep ?? defaultInterventionSleep;
451
- const inFlight = new Set<string>();
452
- return (current: S): void => {
453
- if (inFlight.has(current.id)) return;
454
- inFlight.add(current.id);
455
- void (async () => {
456
- try {
457
- for (let attempt = 1; attempt <= attempts; attempt += 1) {
458
- if (attempt > 1) {
459
- await sleep(typeof delayMs === 'function' ? delayMs(attempt - 1) : delayMs);
460
- }
461
- try {
462
- if (await check(current)) return;
463
- } catch {
464
- }
465
- }
466
- } catch {
467
- } finally {
468
- inFlight.delete(current.id);
469
- }
470
- })();
471
- };
472
- }
473
-
474
- function isRegistry(value: unknown): value is LongTaskRegistry<LongTaskBase> {
475
- if (typeof value !== 'object' || value === null) return false;
476
- const candidate = value as Partial<LongTaskRegistry<LongTaskBase>>;
477
- return (
478
- typeof candidate.start === 'function' &&
479
- typeof candidate.current === 'function' &&
480
- typeof candidate.snapshot === 'function' &&
481
- typeof candidate.onChange === 'function'
482
- );
483
- }
484
-
485
- export function sharedLongTaskRegistry<S extends LongTaskBase>(
486
- scope: string,
487
- key: string,
488
- deps: LongTaskDeps<S>,
489
- ): LongTaskRegistry<S> {
490
- const store = globalThis as unknown as Record<symbol, LongTaskRegistry<LongTaskBase> | undefined>;
491
- const slot = Symbol.for(`dsh-workbench:long-task:${scope}:${key}`);
492
- const existing = store[slot];
493
- if (existing !== undefined && isRegistry(existing)) {
494
- existing.rebind?.(deps as LongTaskDeps<LongTaskBase>);
495
- return existing as unknown as LongTaskRegistry<S>;
496
- }
497
- const created = createLongTaskRegistry(deps);
498
- store[slot] = created as unknown as LongTaskRegistry<LongTaskBase>;
499
- return created;
500
- }
501
-
502
- /** 一个单元的长任务入口:按 key 建注册表。 */
503
- export interface LongTaskHost {
504
- /** key 是模块内稳定的标识;run / view 必给,recheck / answer 决定行上有没有恢复与回执。 */
505
- create<S extends LongTaskBase>(options: { readonly key: string } & LongTaskDeps<S>): LongTaskRegistry<S>;
506
- }
507
-
508
- export function createLongTaskHost(options: {
509
- readonly scope: string;
510
- readonly unit: string;
511
- readonly bump: (facts: readonly string[]) => void;
512
- readonly notify?: ((change: LongTaskChange<LongTaskBase>) => void) | undefined;
513
- }): { readonly host: LongTaskHost; dispose(): void } {
514
- const offs: Array<() => void> = [];
515
- return {
516
- host: {
517
- create(createOptions) {
518
- const { key, facts, ...deps } = createOptions;
519
- const registry = sharedLongTaskRegistry(options.scope, `${options.unit}:${key}`, deps);
520
- offs.push(
521
- registry.onChange((change) => {
522
- const named = [runsRouteOf(options.unit), ...(facts?.(change) ?? [])];
523
- options.bump([...new Set(named)]);
524
- options.notify?.(change);
525
- }),
526
- );
527
- return registry;
528
- },
529
- },
530
- dispose() {
531
- for (const off of offs.splice(0)) off();
532
- },
533
- };
534
- }
@@ -2,7 +2,8 @@ import type { HostContext } from '../../contract/module-context.js';
2
2
  import type { UnitManifest } from '../../contract/unit.js';
3
3
  import type { Need } from '../../contract/needs.js';
4
4
  import type { RouteTable } from '../runtime/http.js';
5
- import type { LongTaskBase, LongTaskHost, LongTaskRegistry } from './long-task.js';
5
+ import type { LongTaskBase, LongTaskRegistry } from './long-task.js';
6
+ import type { LongTaskHost } from './long-task-host.js';
6
7
  import type { ProvisionHandler } from '../supply/provision.js';
7
8
  import type { UnitCapability } from '../../contract/unit-capability.js';
8
9
  import type { ApprovalCall, ApprovalFacts, ApprovalPolicy } from './tool-approval.js';
@@ -0,0 +1,55 @@
1
+ /**
2
+ * `withInterventionRetry` 的口径:最多试几次、每轮先等多久(也可按轮次算)、怎么等。
3
+ */
4
+ export interface InterventionRetryOptions {
5
+ readonly attempts?: number | undefined;
6
+ readonly delayMs?: number | ((attempt: number) => number) | undefined;
7
+ readonly sleep?: ((ms: number) => Promise<void>) | undefined;
8
+ }
9
+
10
+ const INTERVENTION_RETRY_ATTEMPTS = 5;
11
+
12
+ const INTERVENTION_RETRY_DELAY_MS = 1500;
13
+
14
+ function defaultInterventionSleep(ms: number): Promise<void> {
15
+ return new Promise((resolve) => setTimeout(resolve, Math.max(0, ms)));
16
+ }
17
+
18
+ /**
19
+ * 把「看一眼接上没有」包成能交给 `longTasks.create({ recheck })` 的 handler:
20
+ * 用户在人工干预节点上按「我处理完了」时先立刻试一次,没接上就有界地再试(第 2 轮起每轮先等)。
21
+ * `check` 返回 true = 已经接上,不用再试;抛错算没接上、继续下一轮。
22
+ * 同一 id 上一轮还在飞时再按直接忽略;成功 / 用尽都清单飞;**永不抛错**(fire-and-forget)。
23
+ * @param check 看一眼接上没有,可同步可异步。
24
+ * @param options 轮次上限(默认 5)、第几次等待等多久(默认 1500ms,可给函数,从第 1 次等待起算)、怎么等(默认真等,测试可注入假的)。
25
+ * @returns 一条同签名的 handler,直接交给 `const longTasks = create({ recheck })`。
26
+ */
27
+ export function withInterventionRetry<S extends { readonly id: string }>(
28
+ check: (current: S) => boolean | Promise<boolean>,
29
+ options: InterventionRetryOptions = {},
30
+ ): (current: S) => void {
31
+ const attempts = options.attempts ?? INTERVENTION_RETRY_ATTEMPTS;
32
+ const delayMs = options.delayMs ?? INTERVENTION_RETRY_DELAY_MS;
33
+ const sleep = options.sleep ?? defaultInterventionSleep;
34
+ const inFlight = new Set<string>();
35
+ return (current: S): void => {
36
+ if (inFlight.has(current.id)) return;
37
+ inFlight.add(current.id);
38
+ void (async () => {
39
+ try {
40
+ for (let attempt = 1; attempt <= attempts; attempt += 1) {
41
+ if (attempt > 1) {
42
+ await sleep(typeof delayMs === 'function' ? delayMs(attempt - 1) : delayMs);
43
+ }
44
+ try {
45
+ if (await check(current)) return;
46
+ } catch {
47
+ }
48
+ }
49
+ } catch {
50
+ } finally {
51
+ inFlight.delete(current.id);
52
+ }
53
+ })();
54
+ };
55
+ }
package/src/host.ts CHANGED
@@ -30,7 +30,6 @@ export * from './host/adapters/adapter-contract.js';
30
30
  export * from './host/adapters/adapter-set.js';
31
31
  export * from './host/adapters/adapter-types.js';
32
32
  export * from './host/adapters/adapters.js';
33
- export * from './host/platform/integration/host-surface.js';
34
33
  export * from './host/adapters/port-types.js';
35
34
  export * from './host/adapters/supply-port.js';
36
35
  export * from './host/adapters/toolbox-port.js';
@@ -51,16 +50,18 @@ export * from './host/data/recipe-rules.js';
51
50
  export * from './host/data/recipe.js';
52
51
  export * from './host/data/site.js';
53
52
  export * from './host/data/spec.js';
54
- export * from './host/drafts/draft-store.js';
55
53
  export * from './host/diy/diy-debug.js';
56
54
  export * from './host/diy/diy-preset.js';
57
55
  export * from './host/diy/diy-record.js';
58
56
  export * from './host/diy/hold.js';
57
+ export * from './host/drafts/draft-store.js';
59
58
  export * from './host/flow/agent.js';
60
59
  export * from './host/flow/check.js';
61
60
  export * from './host/flow/create.js';
62
61
  export * from './host/flow/data.js';
62
+ export * from './host/flow/dsh-journal.js';
63
63
  export * from './host/flow/expr.js';
64
+ export * from './host/flow/journal.js';
64
65
  export * from './host/flow/kit.js';
65
66
  export * from './host/flow/login-retry.js';
66
67
  export * from './host/flow/open.js';
@@ -75,12 +76,27 @@ export * from './host/git/auth-loss.js';
75
76
  export * from './host/git/git.js';
76
77
  export * from './host/patch/patch-blocks.js';
77
78
  export * from './host/patch/patch.js';
79
+ export * from './host/platform/facts.js';
80
+ export * from './host/platform/integration/bridge.js';
81
+ export * from './host/platform/integration/browser-launch.js';
82
+ export * from './host/platform/integration/browser-process.js';
83
+ export * from './host/platform/integration/browser-read.js';
84
+ export * from './host/platform/integration/browsers.js';
85
+ export * from './host/platform/integration/data.js';
86
+ export * from './host/platform/integration/env-policy.js';
87
+ export * from './host/platform/integration/front-app.js';
88
+ export * from './host/platform/integration/host-surface.js';
89
+ export * from './host/platform/integration/process.js';
90
+ export * from './host/platform/integration/tool-lookup.js';
91
+ export * from './host/platform/ops/shell-render.js';
92
+ export * from './host/platform/ops/shell-text.js';
93
+ export * from './host/platform/ops/shell.js';
94
+ export * from './host/profile/build-approval.js';
78
95
  export * from './host/profile/bundle-manager.js';
79
96
  export * from './host/profile/config-sync.js';
80
97
  export * from './host/profile/dsh-plugins.js';
81
- export * from './host/profile/build-approval.js';
82
- export * from './host/profile/module-rows.js';
83
98
  export * from './host/profile/hmr-reload.js';
99
+ export * from './host/profile/module-rows.js';
84
100
  export * from './host/profile/pnpm-command.js';
85
101
  export * from './host/profile/profile-bundles.js';
86
102
  export * from './host/profile/profile-create.js';
@@ -90,20 +106,12 @@ export * from './host/runtime/boot-record.js';
90
106
  export * from './host/runtime/changelog.js';
91
107
  export * from './host/runtime/cli-error.js';
92
108
  export * from './host/runtime/cli.js';
93
- export * from './host/runtime/dsh-compat.js';
94
- export * from './host/platform/integration/env-policy.js';
95
- export * from './host/platform/integration/browser-read.js';
96
- export * from './host/platform/integration/data.js';
97
- export * from './host/platform/integration/front-app.js';
98
- export * from './host/platform/integration/tool-lookup.js';
99
- export * from './host/platform/integration/browsers.js';
100
- export * from './host/platform/integration/bridge.js';
101
- export * from './host/platform/integration/browser-process.js';
102
- export * from './host/platform/integration/browser-launch.js';
109
+ export * from './host/runtime/diag-bundle.js';
103
110
  export * from './host/runtime/diag-records.js';
104
111
  export * from './host/runtime/diag-sink.js';
105
- export * from './host/runtime/diag-bundle.js';
106
112
  export * from './host/runtime/diag-summary.js';
113
+ export * from './host/runtime/dsh-compat.js';
114
+ export * from './host/runtime/env-policy.js';
107
115
  export * from './host/runtime/fs-write.js';
108
116
  export * from './host/runtime/http.js';
109
117
  export * from './host/runtime/lease.js';
@@ -111,8 +119,8 @@ export * from './host/runtime/paths.js';
111
119
  export * from './host/runtime/process-alive.js';
112
120
  export * from './host/runtime/read-json.js';
113
121
  export * from './host/runtime/time.js';
114
- export * from './host/runtime/version-range.js';
115
122
  export * from './host/runtime/verified-dsh.js';
123
+ export * from './host/runtime/version-range.js';
116
124
  export * from './host/runtime/version.js';
117
125
  export * from './host/runtime/workspace-store.js';
118
126
  export * from './host/runtime/write-result.js';
@@ -127,17 +135,17 @@ export * from './host/seam/process.js';
127
135
  export * from './host/seam/run.js';
128
136
  export * from './host/seam/service.js';
129
137
  export * from './host/seam/unit-seam.js';
130
- export * from './host/platform/ops/shell-render.js';
131
- export * from './host/platform/ops/shell-text.js';
138
+ export * from './host/shell/shell-render.js';
139
+ export * from './host/shell/shell-text.js';
132
140
  export * from './host/shell/shell.js';
133
141
  export * from './host/shell/tokens.js';
142
+ export * from './host/supply/builtin-catalog.js';
134
143
  export * from './host/supply/install-failure.js';
135
144
  export * from './host/supply/installed-module.js';
136
145
  export * from './host/supply/installed-sources.js';
137
146
  export * from './host/supply/lib-capabilities.js';
138
147
  export * from './host/supply/lib-install.js';
139
148
  export * from './host/supply/lib-runners.js';
140
- export * from './host/supply/builtin-catalog.js';
141
149
  export * from './host/supply/market-index-claim.js';
142
150
  export * from './host/supply/market-index-doc.js';
143
151
  export * from './host/supply/market-index-parse.js';
@@ -172,6 +180,8 @@ export * from './host/unit/facts.js';
172
180
  export * from './host/unit/host-actions.js';
173
181
  export * from './host/unit/host-kit.js';
174
182
  export * from './host/unit/local-declaration.js';
183
+ export * from './host/unit/long-task-host.js';
184
+ export * from './host/unit/long-task-progress.js';
175
185
  export * from './host/unit/long-task.js';
176
186
  export * from './host/unit/needs.js';
177
187
  export * from './host/unit/panel-state.js';
@@ -181,4 +191,5 @@ export * from './host/unit/unit-config.js';
181
191
  export * from './host/unit/unit-data.js';
182
192
  export * from './host/unit/unit-face.js';
183
193
  export * from './host/unit/unit-plugin.js';
184
- export * from './host/unit/value-handoff.js';
194
+ export * from './host/unit/value-handoff.js';
195
+ export * from './host/unit/with-intervention-retry.js';
package/src/i18n/en.ts CHANGED
@@ -64,6 +64,7 @@ export const en: Record<keyof WorkbenchMessages, string> = {
64
64
  'status.running': 'Running',
65
65
  'status.longTask': 'Long task running',
66
66
  'status.longTaskWaiting': 'Long task waiting for you',
67
+ 'status.longTaskDone': 'Long task completed',
67
68
  'status.waitingApproval': 'Waiting for approval',
68
69
  'status.planReview': 'Plan awaiting review',
69
70
  'status.waitingAnswer': 'Waiting for your answer',
@@ -81,10 +82,13 @@ export const en: Record<keyof WorkbenchMessages, string> = {
81
82
  'project.attention.longTask.other': '{n} long tasks running',
82
83
  'project.attention.longTaskWaiting.one': '1 long task waiting for you',
83
84
  'project.attention.longTaskWaiting.other': '{n} long tasks waiting for you',
85
+ 'project.attention.longTaskDone.one': '1 long task completed',
86
+ 'project.attention.longTaskDone.other': '{n} long tasks completed',
84
87
  'project.attention.done.one': '1 session completed, unread',
85
88
  'project.attention.done.other': '{n} sessions completed, unread',
86
89
  'project.attention.state.warning': 'Approval',
87
90
  'project.attention.state.longTaskWaiting': 'To handle',
91
+ 'project.attention.state.longTaskDone': 'Completed',
88
92
  'project.attention.state.running': 'Running',
89
93
  'project.attention.state.done': 'New',
90
94
 
@@ -734,14 +738,29 @@ export const en: Record<keyof WorkbenchMessages, string> = {
734
738
  'env.runtime.ok': 'Ready',
735
739
  'env.bridge.ok': 'Connected',
736
740
  'env.bridge.short': 'Not connected',
737
- 'env.bridge.why': 'Panel data needs the sign-in state in your browser. When the connection is not established, modules cannot fetch data. You can fix it and continue, or enter the workbench first.',
738
- 'env.bridge.extensionOffline': 'The extension is not connected. Choose either install method: (1) click the link below to install it from the Chrome Web Store; (2) load a local copy: open chrome://extensions, turn on Developer mode in the top right, click Load unpacked, and choose the extension folder. When done, come back and click Retry.',
739
- 'env.bridge.extensionLocal': 'The extension is not connected. Choose either install method: (1) use the copy shipped with the workbench: open chrome://extensions, turn on Developer mode in the top right, click Load unpacked, and choose {dir}; (2) click the link below to install it from the Chrome Web Store. When done, come back and click Retry.',
740
- 'env.bridge.extensionIncompatible': 'The browser extension and the background tools are on incompatible versions, so panels cannot read data. Update the extension to v{version}, then come back and click Retry.',
741
- 'env.bridge.extensionIncompatibleAny': 'The browser extension and the background tools are on incompatible versions, so panels cannot read data. Switch to a compatible extension version, then come back and click Retry.',
741
+ 'env.bridge.reason.extension': 'The extension is not connected, so panels that need your sign-in cannot fetch data.',
742
+ 'env.bridge.extension.local': 'Use the copy that ships with the workbench',
743
+ 'env.bridge.extension.devMode': 'Turn on Developer mode in the top right',
744
+ 'env.bridge.extension.loadUnpacked': 'Click Load unpacked',
745
+ 'env.bridge.extension.chooseDir': 'Choose this folder',
746
+ 'env.bridge.extension.enable': 'Make sure the extension is enabled',
747
+ 'env.bridge.incompatible.why': 'The browser extension and the background tools are on incompatible versions, so panels cannot read data.',
748
+ 'env.bridge.incompatible.reload': 'Find the browser extension entry and click Reload; if you installed it from the store, click Update',
749
+ 'env.bridge.incompatible.confirm': 'Check that the version is now v{version}',
750
+ 'env.bridge.incompatible.confirmAny': 'Check that the version has changed',
751
+ 'env.bridge.opened': 'Opened the extensions page in your browser.',
752
+ 'env.bridge.openFailed': 'Could not open the browser. Open chrome://extensions manually.',
742
753
  'env.bridge.hold': 'The browser is not connected yet. Fix it with the steps below and click Retry, or enter the workbench first.',
743
- 'env.bridge.unknown': 'The browser connection reported a problem this workbench cannot classify. You can expand the original output, or let the agent diagnose.',
754
+ 'env.bridge.unknown': 'The browser connection reported a problem this workbench cannot classify.',
744
755
  'env.fix.title': 'How to fix this',
756
+ 'env.fix.reason': 'Cause',
757
+ 'env.fix.situation': 'Symptom',
758
+ 'env.fix.methods': 'What to do',
759
+ 'env.fix.pickMethod': 'Either method works.',
760
+ 'env.fix.note': 'When done, come back and click Retry.',
761
+ 'env.fix.open': 'Open',
762
+ 'env.fix.openExtensions': 'Open the Chrome extensions page',
763
+ 'env.fix.runCommand': 'Run this command in a terminal',
745
764
  'env.fix.why': 'The workbench uses its own bundled background tool, separate from the one installed in your terminal. It is not installed yet on this machine.',
746
765
  'env.fix.whyPath': 'The background tool is installed, but this environment still cannot find it.',
747
766
  'env.fix.whyShell': 'The workbench cannot reach this machine’s command line, so it cannot fetch data. This step cannot be completed here; an administrator needs to fix the background environment.',
@@ -913,6 +932,10 @@ export const en: Record<keyof WorkbenchMessages, string> = {
913
932
  'approval.plan.do': 'What you can do',
914
933
  'approval.plan.agent': 'What the AI can do',
915
934
  'approval.plan.data': 'Data source',
935
+ 'approval.plan.output': 'Output',
936
+ 'approval.plan.trigger': 'Trigger',
937
+ 'approval.plan.approval': 'Approval',
938
+ 'approval.plan.headlessData': 'Data',
916
939
  'approval.fact.object': 'Item',
917
940
  'approval.fact.change': 'After',
918
941
  'approval.fact.target': 'Destination',
@@ -1034,13 +1057,16 @@ export const en: Record<keyof WorkbenchMessages, string> = {
1034
1057
  'longTask.stopPending': 'Stop requested; waiting for the module',
1035
1058
  'longTask.attempt': 'Attempt {attempt}',
1036
1059
  'longTask.list.entry': 'Workbench tasks {n}',
1060
+ 'longTask.list.openWorkspace': 'Open workspace {name}',
1037
1061
  'longTask.list.count': '{n}',
1038
1062
  'longTask.list.title': 'Workbench tasks',
1039
1063
  'longTask.state.running': 'Running',
1040
1064
  'longTask.state.needsHuman': 'Waiting for you',
1041
1065
  'longTask.state.needsLogin': 'Needs sign-in',
1042
1066
  'longTask.state.stopped': 'Stopped',
1043
- 'longTask.state.ok': 'Done',
1067
+ 'longTask.progress.text': '{done}/{total}',
1068
+ 'longTask.progress.label': 'Progress',
1069
+ 'longTask.state.ok': 'Completed',
1044
1070
  'longTask.state.failed': 'Failed',
1045
1071
  'longTask.duration.sec': '{s}s',
1046
1072
  'longTask.duration.minSec': '{m}m {s}s',