@lokvis/runtime 0.1.0 → 0.2.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 (80) hide show
  1. package/LICENSE +21 -0
  2. package/dist/asset-store.d.ts +5 -2
  3. package/dist/asset-store.d.ts.map +1 -1
  4. package/dist/asset-store.js +93 -3
  5. package/dist/asset-store.js.map +1 -1
  6. package/dist/batch-processor.d.ts +200 -0
  7. package/dist/batch-processor.d.ts.map +1 -0
  8. package/dist/batch-processor.js +505 -0
  9. package/dist/batch-processor.js.map +1 -0
  10. package/dist/capability-registry.d.ts +4 -0
  11. package/dist/capability-registry.d.ts.map +1 -1
  12. package/dist/capability-registry.js +19 -2
  13. package/dist/capability-registry.js.map +1 -1
  14. package/dist/degradation.d.ts +109 -0
  15. package/dist/degradation.d.ts.map +1 -0
  16. package/dist/degradation.js +184 -0
  17. package/dist/degradation.js.map +1 -0
  18. package/dist/event-bus.d.ts.map +1 -1
  19. package/dist/event-bus.js +7 -2
  20. package/dist/event-bus.js.map +1 -1
  21. package/dist/executor.d.ts.map +1 -1
  22. package/dist/executor.js +37 -1
  23. package/dist/executor.js.map +1 -1
  24. package/dist/history-store.d.ts +76 -0
  25. package/dist/history-store.d.ts.map +1 -0
  26. package/dist/history-store.js +109 -0
  27. package/dist/history-store.js.map +1 -0
  28. package/dist/history.d.ts +1 -1
  29. package/dist/history.d.ts.map +1 -1
  30. package/dist/history.js +1 -1
  31. package/dist/history.js.map +1 -1
  32. package/dist/idb-asset-store.js +1 -1
  33. package/dist/idb-asset-store.js.map +1 -1
  34. package/dist/index.d.ts +5 -0
  35. package/dist/index.d.ts.map +1 -1
  36. package/dist/index.js +5 -0
  37. package/dist/index.js.map +1 -1
  38. package/dist/memory-guard.d.ts +109 -0
  39. package/dist/memory-guard.d.ts.map +1 -0
  40. package/dist/memory-guard.js +161 -0
  41. package/dist/memory-guard.js.map +1 -0
  42. package/dist/opfs-asset-store.d.ts +35 -4
  43. package/dist/opfs-asset-store.d.ts.map +1 -1
  44. package/dist/opfs-asset-store.js +94 -10
  45. package/dist/opfs-asset-store.js.map +1 -1
  46. package/dist/runtime.d.ts +139 -4
  47. package/dist/runtime.d.ts.map +1 -1
  48. package/dist/runtime.js +443 -25
  49. package/dist/runtime.js.map +1 -1
  50. package/dist/types.d.ts +101 -2
  51. package/dist/types.d.ts.map +1 -1
  52. package/dist/worker-host.d.ts +13 -0
  53. package/dist/worker-host.d.ts.map +1 -1
  54. package/dist/worker-host.js +47 -15
  55. package/dist/worker-host.js.map +1 -1
  56. package/dist/worker-protocol.d.ts +10 -1
  57. package/dist/worker-protocol.d.ts.map +1 -1
  58. package/dist/worker-protocol.js.map +1 -1
  59. package/dist/workflow-builder.d.ts +124 -0
  60. package/dist/workflow-builder.d.ts.map +1 -0
  61. package/dist/workflow-builder.js +240 -0
  62. package/dist/workflow-builder.js.map +1 -0
  63. package/package.json +3 -2
  64. package/src/asset-store.ts +103 -3
  65. package/src/batch-processor.ts +699 -0
  66. package/src/capability-registry.ts +19 -2
  67. package/src/degradation.ts +241 -0
  68. package/src/event-bus.ts +6 -2
  69. package/src/executor.ts +46 -1
  70. package/src/history-store.ts +159 -0
  71. package/src/history.ts +2 -2
  72. package/src/idb-asset-store.ts +1 -1
  73. package/src/index.ts +5 -0
  74. package/src/memory-guard.ts +202 -0
  75. package/src/opfs-asset-store.ts +118 -10
  76. package/src/runtime.ts +482 -21
  77. package/src/types.ts +98 -1
  78. package/src/worker-host.ts +56 -16
  79. package/src/worker-protocol.ts +11 -1
  80. package/src/workflow-builder.ts +303 -0
@@ -0,0 +1,699 @@
1
+ /**
2
+ * BatchProcessor - 批量作业处理器(W6.1 + W6.2 + W6.3)
3
+ *
4
+ * 目标:把 W5 在 playground BatchQueue.tsx 里手写的"并发 4 池 + 进度 + 错误"
5
+ * 逻辑下沉到 runtime,统一暴露给 SDK / MCP / UI 消费。
6
+ *
7
+ * 核心能力:
8
+ * - 并发控制:基于槽位池,默认 4(Pro=16),按 MemoryGuard 压力动态收缩
9
+ * - 失败重试:maxRetries(默认 0,与 W5 行为兼容);达到上限才发 batch:item:failed
10
+ * - 批量上限:免费 10 文件(W6.2)、Pro 无限(超额抛 BatchLimitExceededError)
11
+ * - 进度事件:batch:started / batch:item:* / batch:progress / batch:completed / batch:cancelled
12
+ * - 取消:批量级 cancel,逐个取消 in-flight 项,已 pending 的标记 cancelled
13
+ * - 暂停/恢复:挂起 schedule 循环(不取消 in-flight,完成后不再补满)
14
+ *
15
+ * 设计取舍:
16
+ * - 不持久化作业状态(刷新即丢):W6 主要保证 Asset 持久化,批量队列是临时调度
17
+ * 结构,刷新后由 UI 重新发起即可。持久化批量进度属 Phase 2。
18
+ * - 不持有 output Blob:每项执行完即返回 AssetId,Blob 由 AssetStore(OPFS/IDB)托管,
19
+ * 避免 50+ 图批量把 output Blob 全部留在内存导致 OOM。
20
+ * - 单 Job 串行 schedule:同一 BatchProcessor 实例可并发 enqueue 多个 Job,但内部
21
+ * schedule 逻辑用 per-job lock 防重入(参考 W5 BatchQueue 的 scheduleLock 修复)。
22
+ *
23
+ * @example
24
+ * ```ts
25
+ * const job = runtime.batch.enqueue({
26
+ * items: files.map((f) => ({ source: { kind: 'file', file: f }, workflow: wf })),
27
+ * maxRetries: 1,
28
+ * });
29
+ * runtime.batch.onProgress(job.id, (p) => console.log(p.completed, '/', p.total));
30
+ * await runtime.batch.waitForCompletion(job.id);
31
+ * ```
32
+ */
33
+ import type {
34
+ AssetId,
35
+ AssetSource,
36
+ BatchItemStatus,
37
+ BatchJobStatus,
38
+ LokvisEvent,
39
+ Workflow,
40
+ WorkflowResult,
41
+ } from '@lokvis/schema';
42
+ import type { EventBus } from '@lokvis/schema';
43
+ import type { LokvisRuntime } from './types.js';
44
+ import { MemoryGuard, type MemoryPressure } from './memory-guard.js';
45
+
46
+ /** 批量项输入(每个文件一个) */
47
+ export interface BatchItemInput {
48
+ /**
49
+ * 资产来源:通常为 { kind: 'file', file }(用户拖入),也可为 blob/url。
50
+ * BatchProcessor 在执行前调 runtime.importAsset(source) 导入为 AssetId。
51
+ */
52
+ source: AssetSource;
53
+ /** 该项要执行的工作流(每个文件独立 workflow 实例,便于独立 cancel) */
54
+ workflow: Workflow;
55
+ /** 该项最大重试次数(覆盖 job 级 maxRetries) */
56
+ maxRetries?: number;
57
+ }
58
+
59
+ /** 批量项运行时状态(对外只读视图) */
60
+ export interface BatchItem {
61
+ /** 项 ID(自动生成,用于事件追踪与 UI 定位) */
62
+ id: string;
63
+ /** 在 job 中的序号(0-based,用于事件中的 index 字段) */
64
+ index: number;
65
+ status: BatchItemStatus;
66
+ /** 输入 source(便于失败时 UI 展示文件名) */
67
+ source: AssetSource;
68
+ /** 该项执行的工作流 */
69
+ workflow: Workflow;
70
+ /** 输出 AssetId(completed 时填) */
71
+ outputAssetId?: AssetId;
72
+ /** 失败原因(failed 时填) */
73
+ error?: Error;
74
+ /** 已重试次数 */
75
+ attempts: number;
76
+ /** 该项最大重试次数 */
77
+ maxRetries: number;
78
+ /** 单项执行耗时(ms,completed 时填) */
79
+ duration?: number;
80
+ }
81
+
82
+ /** 批量作业运行时状态(对外只读视图) */
83
+ export interface BatchJob {
84
+ id: string;
85
+ status: BatchJobStatus;
86
+ items: BatchItem[];
87
+ /** 已完成数(completed) */
88
+ completed: number;
89
+ /** 失败数(failed) */
90
+ failed: number;
91
+ /** 总项数 */
92
+ total: number;
93
+ /** job 起始时间(ms) */
94
+ startedAt: number;
95
+ /** job 结束时间(ms,完成后填) */
96
+ endedAt?: number;
97
+ }
98
+
99
+ /** enqueue 选项 */
100
+ export interface EnqueueOptions {
101
+ /** 作业项 */
102
+ items: BatchItemInput[];
103
+ /** job 级最大重试次数(默认 0,单项可 BatchItemInput.maxRetries 覆盖) */
104
+ maxRetries?: number;
105
+ /**
106
+ * 并发槽位上限(默认按 isPro:free=4,pro=16)。
107
+ * 实际并发会按 MemoryGuard 压力动态收缩。
108
+ */
109
+ concurrency?: number;
110
+ }
111
+
112
+ /** 进度回调载荷 */
113
+ export interface BatchProgress {
114
+ jobId: string;
115
+ completed: number;
116
+ failed: number;
117
+ total: number;
118
+ }
119
+
120
+ /** 批量上限超限错误(W6.2) */
121
+ export class BatchLimitExceededError extends Error {
122
+ readonly limit: number;
123
+ readonly requested: number;
124
+ constructor(limit: number, requested: number) {
125
+ super(
126
+ `Batch limit exceeded: free tier allows up to ${limit} items, got ${requested}. ` +
127
+ 'Upgrade to Pro for unlimited batch processing.'
128
+ );
129
+ this.name = 'BatchLimitExceededError';
130
+ this.limit = limit;
131
+ this.requested = requested;
132
+ }
133
+ }
134
+
135
+ /** 免费版批量上限(W6.2) */
136
+ export const FREE_BATCH_LIMIT = 10;
137
+
138
+ /** 默认并发槽位 */
139
+ export const FREE_CONCURRENCY = 4;
140
+ export const PRO_CONCURRENCY = 16;
141
+
142
+ /** 按 MemoryPressure 收缩并发槽位 */
143
+ function shrinkConcurrencyByPressure(
144
+ base: number,
145
+ pressure: MemoryPressure
146
+ ): number {
147
+ switch (pressure) {
148
+ case 'critical':
149
+ return 1;
150
+ case 'high':
151
+ return Math.max(1, Math.floor(base / 2));
152
+ case 'elevated':
153
+ return Math.max(2, Math.floor((base * 3) / 4));
154
+ default:
155
+ return base;
156
+ }
157
+ }
158
+
159
+ /** 生成项 ID */
160
+ function generateItemId(): string {
161
+ if (typeof crypto !== 'undefined' && crypto.randomUUID) {
162
+ return `bi_${crypto.randomUUID()}`;
163
+ }
164
+ return `bi_${Date.now()}_${Math.random().toString(36).slice(2, 10)}`;
165
+ }
166
+
167
+ /** 生成 job ID */
168
+ function generateJobId(): string {
169
+ if (typeof crypto !== 'undefined' && crypto.randomUUID) {
170
+ return `batch_${crypto.randomUUID()}`;
171
+ }
172
+ return `batch_${Date.now()}_${Math.random().toString(36).slice(2, 10)}`;
173
+ }
174
+
175
+ /**
176
+ * BatchProcessor 实现。
177
+ *
178
+ * 不直接依赖 LokvisRuntimeImpl(避免循环引用),通过构造注入 LokvisRuntime 接口。
179
+ * 内部用纯 JS Map 管理作业,无 React state(可被 SDK / MCP / Node 复用)。
180
+ */
181
+ export class BatchProcessor {
182
+ private readonly runtime: LokvisRuntime;
183
+ private readonly eventBus: EventBus;
184
+ private readonly isPro: boolean;
185
+ private readonly memoryGuard?: MemoryGuard;
186
+ /** jobId → BatchJob 内部可变状态(含 private 字段) */
187
+ private readonly jobs = new Map<string, BatchJobInternal>();
188
+ /** 进度回调订阅 */
189
+ private readonly progressSubs = new Map<string, Set<(p: BatchProgress) => void>>();
190
+
191
+ constructor(opts: {
192
+ runtime: LokvisRuntime;
193
+ eventBus: EventBus;
194
+ isPro: boolean;
195
+ memoryGuard?: MemoryGuard;
196
+ }) {
197
+ this.runtime = opts.runtime;
198
+ this.eventBus = opts.eventBus;
199
+ this.isPro = opts.isPro;
200
+ this.memoryGuard = opts.memoryGuard;
201
+ }
202
+
203
+ /**
204
+ * 入队批量作业。返回 job 视图(只读)。
205
+ * 超过免费上限时同步抛 BatchLimitExceededError,不创建 job。
206
+ * 空 items 抛错(避免静默产出 0 项 completed job,遮蔽调用方逻辑错误)。
207
+ */
208
+ enqueue(options: EnqueueOptions): BatchJob {
209
+ const { items, maxRetries = 0, concurrency } = options;
210
+
211
+ // m3: 空数组视为调用方逻辑错误,显式抛错而非静默完成
212
+ if (!Array.isArray(items) || items.length === 0) {
213
+ throw new Error('BatchProcessor.enqueue: items cannot be empty');
214
+ }
215
+
216
+ // W6.2 批量上限:免费 10,Pro 无限
217
+ if (!this.isPro && items.length > FREE_BATCH_LIMIT) {
218
+ throw new BatchLimitExceededError(FREE_BATCH_LIMIT, items.length);
219
+ }
220
+
221
+ const jobId = generateJobId();
222
+ const baseConcurrency =
223
+ concurrency ?? (this.isPro ? PRO_CONCURRENCY : FREE_CONCURRENCY);
224
+
225
+ const internal: BatchJobInternal = {
226
+ id: jobId,
227
+ status: 'queued',
228
+ baseConcurrency,
229
+ maxRetries,
230
+ items: items.map((input, index) => ({
231
+ id: generateItemId(),
232
+ index,
233
+ status: 'pending' as BatchItemStatus,
234
+ source: input.source,
235
+ workflow: input.workflow,
236
+ attempts: 0,
237
+ maxRetries: input.maxRetries ?? maxRetries,
238
+ })),
239
+ completed: 0,
240
+ failed: 0,
241
+ startedAt: Date.now(),
242
+ /** schedule 重入锁(参考 W5 BatchQueue scheduleLock 修复) */
243
+ scheduleLock: false,
244
+ /** pause 时 schedule 不再补满新项 */
245
+ paused: false,
246
+ /** cancel 标记:已发起 cancel 后所有 pending → cancelled,schedule 终止 */
247
+ cancelled: false,
248
+ };
249
+ this.jobs.set(jobId, internal);
250
+
251
+ // 异步启动(不阻塞 enqueue 调用方)
252
+ void this.runJob(internal);
253
+
254
+ return this.toJobView(internal);
255
+ }
256
+
257
+ /** 列出所有作业(只读视图) */
258
+ list(): BatchJob[] {
259
+ return Array.from(this.jobs.values()).map((j) => this.toJobView(j));
260
+ }
261
+
262
+ /** 获取指定 job */
263
+ get(jobId: string): BatchJob | undefined {
264
+ const internal = this.jobs.get(jobId);
265
+ return internal ? this.toJobView(internal) : undefined;
266
+ }
267
+
268
+ /** 取消整个 job:逐个取消 in-flight 项,pending 项标记 cancelled */
269
+ async cancel(jobId: string): Promise<void> {
270
+ const job = this.jobs.get(jobId);
271
+ if (!job) return;
272
+ // m2: failed 也是终态,任何终态 job 不应被改写
273
+ if (this.isTerminal(job.status)) return;
274
+
275
+ // B1 修复:同步先标记 cancelled,使 in-flight processItem 续跑时读到
276
+ // cancelled=true 后短路,避免覆盖 cancelled 状态或把被取消的项误标 failed。
277
+ job.cancelled = true;
278
+ job.paused = false;
279
+ let cancelledCount = 0;
280
+
281
+ // 同步把所有 processing/pending 项标记 cancelled(processItem 续跑会读到此状态)
282
+ for (const item of job.items) {
283
+ if (item.status === 'processing' || item.status === 'pending') {
284
+ item.status = 'cancelled';
285
+ cancelledCount++;
286
+ }
287
+ }
288
+
289
+ // 异步取消所有 in-flight workflow(此时项状态已是 cancelled,
290
+ // processItem 续跑会因 job.cancelled 短路,不会回写其他状态)
291
+ for (const item of job.items) {
292
+ if (item.status === 'cancelled') {
293
+ const wfId = this.itemWorkflowId(job.id, item.id);
294
+ try {
295
+ await this.runtime.cancel(wfId);
296
+ } catch {
297
+ // 取消失败不阻断,继续取消其他项
298
+ }
299
+ }
300
+ }
301
+
302
+ job.status = 'cancelled';
303
+ job.endedAt = Date.now();
304
+ this.emit({ type: 'batch:cancelled', jobId, cancelled: cancelledCount });
305
+ this.cleanupJobSubs(jobId);
306
+ }
307
+
308
+ /** 暂停 job:schedule 不再补满,已 in-flight 项跑完即止 */
309
+ async pause(jobId: string): Promise<void> {
310
+ const job = this.jobs.get(jobId);
311
+ if (!job) return;
312
+ if (job.status !== 'running' && job.status !== 'queued') return;
313
+ job.paused = true;
314
+ job.status = 'paused';
315
+ // m1: 发 pause 事件,UI 可订阅无需轮询
316
+ this.emit({ type: 'batch:paused', jobId });
317
+ }
318
+
319
+ /** 恢复 job:解除 pause,schedule 继续补满 */
320
+ async resume(jobId: string): Promise<void> {
321
+ const job = this.jobs.get(jobId);
322
+ if (!job) return;
323
+ if (job.status !== 'paused') return;
324
+ job.paused = false;
325
+ job.status = 'running';
326
+ // m1: 发 resumed 事件
327
+ this.emit({ type: 'batch:resumed', jobId });
328
+ void this.schedule(job);
329
+ }
330
+
331
+ /**
332
+ * 重试 job 中所有 failed 项(仅当 job 已结束)。
333
+ * 重置 attempts/error/status,pending 重新等待 schedule。
334
+ */
335
+ async retryFailed(jobId: string): Promise<void> {
336
+ const job = this.jobs.get(jobId);
337
+ if (!job) return;
338
+ if (job.status !== 'failed' && job.status !== 'completed') return;
339
+
340
+ for (const item of job.items) {
341
+ if (item.status === 'failed') {
342
+ item.status = 'pending';
343
+ item.attempts = 0;
344
+ item.error = undefined;
345
+ }
346
+ }
347
+ job.failed = 0;
348
+ job.cancelled = false;
349
+ job.status = 'running';
350
+ void this.schedule(job);
351
+ }
352
+
353
+ /** 订阅 job 进度(返回取消订阅函数) */
354
+ onProgress(jobId: string, handler: (p: BatchProgress) => void): () => void {
355
+ let subs = this.progressSubs.get(jobId);
356
+ if (!subs) {
357
+ subs = new Set();
358
+ this.progressSubs.set(jobId, subs);
359
+ }
360
+ subs.add(handler);
361
+ return () => {
362
+ subs!.delete(handler);
363
+ if (subs!.size === 0) this.progressSubs.delete(jobId);
364
+ };
365
+ }
366
+
367
+ /**
368
+ * 等待 job 完成(completed/failed/cancelled 任一)。
369
+ *
370
+ * M5 修复:
371
+ * - 增加 timeoutMs(默认 5min),超时 reject,避免坏 job 永久挂起泄漏订阅
372
+ * - batch:completed 事件已涵盖 failed(maybeComplete 对 failed>0 的 job
373
+ * 同样发 batch:completed),故只需监听 completed + cancelled
374
+ * - resolve 后立即解绑所有订阅,避免调用方丢弃 promise 时泄漏
375
+ * - cancel/maybeComplete 进入终态时也会 cleanupJobSubs 兜底
376
+ */
377
+ async waitForCompletion(
378
+ jobId: string,
379
+ timeoutMs = 5 * 60 * 1000
380
+ ): Promise<BatchJob> {
381
+ const job = this.jobs.get(jobId);
382
+ if (!job) throw new Error(`Batch job not found: ${jobId}`);
383
+ if (this.isTerminal(job.status)) return this.toJobView(job);
384
+
385
+ return new Promise<BatchJob>((resolve, reject) => {
386
+ let settled = false;
387
+ // 声明在前,finish 与各 handler 互相引用
388
+ let offCompleted: () => void;
389
+ let offCancelled: () => void;
390
+ let timer: ReturnType<typeof setTimeout>;
391
+ const finish = (fn: () => void) => {
392
+ if (settled) return;
393
+ settled = true;
394
+ clearTimeout(timer);
395
+ offCompleted();
396
+ offCancelled();
397
+ fn();
398
+ };
399
+ offCompleted = this.eventBus.on('batch:completed', (e) => {
400
+ if (e.jobId !== jobId) return;
401
+ finish(() => resolve(this.toJobView(this.jobs.get(jobId) ?? job)));
402
+ });
403
+ offCancelled = this.eventBus.on('batch:cancelled', (e) => {
404
+ if (e.jobId !== jobId) return;
405
+ finish(() => resolve(this.toJobView(this.jobs.get(jobId) ?? job)));
406
+ });
407
+ timer = setTimeout(() => {
408
+ finish(() =>
409
+ reject(
410
+ new Error(
411
+ `BatchProcessor.waitForCompletion timed out after ${timeoutMs}ms (jobId=${jobId})`
412
+ )
413
+ )
414
+ );
415
+ }, timeoutMs);
416
+ });
417
+ }
418
+
419
+ /** 清理 job 的所有进度订阅(终态时调用,避免泄漏) */
420
+ private cleanupJobSubs(jobId: string): void {
421
+ this.progressSubs.delete(jobId);
422
+ }
423
+
424
+ // ─── 内部实现 ────────────────────────────────────────
425
+
426
+ /** 启动 job:发 batch:started,进入 running,触发首次 schedule */
427
+ private async runJob(job: BatchJobInternal): Promise<void> {
428
+ if (job.cancelled) return;
429
+ job.status = 'running';
430
+ this.emit({ type: 'batch:started', jobId: job.id, total: job.items.length });
431
+ await this.schedule(job);
432
+ }
433
+
434
+ /**
435
+ * 调度循环:把 pending 项填入空闲槽位,直到无可用槽位或无 pending 项。
436
+ *
437
+ * 重入保护:用 scheduleLock 防止多个 schedule 调用并发导致超限(参考 W5
438
+ * BatchQueue.tsx 的 scheduleLock 修复:并发完成时多个 processItem 同时触发
439
+ * schedule 会读到相同 running 快照,导致并发超限)。
440
+ */
441
+ private async schedule(job: BatchJobInternal): Promise<void> {
442
+ if (job.scheduleLock) return;
443
+ if (job.cancelled || job.paused) return;
444
+ if (this.isTerminal(job.status)) return;
445
+
446
+ job.scheduleLock = true;
447
+ try {
448
+ while (!job.cancelled && !job.paused) {
449
+ const running = job.items.filter((i) => i.status === 'processing').length;
450
+ const cap = this.currentConcurrency(job);
451
+ if (running >= cap) break;
452
+
453
+ const next = job.items.find((i) => i.status === 'pending');
454
+ if (!next) break;
455
+
456
+ // 标记 processing 并启动(不 await,让循环继续补满)
457
+ next.status = 'processing';
458
+ void this.processItem(job, next);
459
+ }
460
+
461
+ // 循环结束:检查是否所有项都已终结
462
+ this.maybeComplete(job);
463
+ } finally {
464
+ job.scheduleLock = false;
465
+ }
466
+ }
467
+
468
+ /** 处理单个项:导入 → run workflow → 导出 output AssetId */
469
+ private async processItem(
470
+ job: BatchJobInternal,
471
+ item: BatchItemInternal
472
+ ): Promise<void> {
473
+ const start = Date.now();
474
+ const wfId = this.itemWorkflowId(job.id, item.id);
475
+
476
+ this.emit({
477
+ type: 'batch:item:started',
478
+ jobId: job.id,
479
+ itemId: item.id,
480
+ index: item.index,
481
+ total: job.items.length,
482
+ });
483
+
484
+ // B1 修复:cancel 已同步把 item 标为 cancelled,此处若发现 cancelled
485
+ // 直接短路,不发起 import/run,避免与 cancel 竞态
486
+ if (job.cancelled || item.status === 'cancelled') {
487
+ return;
488
+ }
489
+
490
+ let inputAssetId: AssetId | undefined;
491
+ let result: WorkflowResult | undefined;
492
+ try {
493
+ // 1. 导入输入资产
494
+ inputAssetId = await this.runtime.importAsset(item.source);
495
+
496
+ // B1 守卫:import 期间可能被 cancel
497
+ if (job.cancelled) {
498
+ item.status = 'cancelled';
499
+ // 清理刚导入的 input,避免孤儿资产(cancel 期间产出的 input 不应残留)
500
+ void this.runtime.removeAsset(inputAssetId).catch(() => {});
501
+ return;
502
+ }
503
+
504
+ // 2. 执行工作流(用独立 workflow id,便于 cancel)
505
+ const wf: Workflow = { ...item.workflow, id: wfId };
506
+ result = await this.runtime.run(wf, [inputAssetId]);
507
+
508
+ // B1 守卫:run 期间可能被 cancel(cancel 已同步标 item=cancelled)
509
+ if (job.cancelled) {
510
+ item.status = 'cancelled';
511
+ return;
512
+ }
513
+
514
+ if (result.status !== 'completed' || result.outputs.length === 0) {
515
+ throw new Error(result.error || `Workflow ${result.status}`);
516
+ }
517
+
518
+ const outputAssetId = result.outputs[0]!;
519
+ const duration = Date.now() - start;
520
+
521
+ // 成功
522
+ item.status = 'completed';
523
+ item.outputAssetId = outputAssetId;
524
+ item.duration = duration;
525
+ job.completed++;
526
+
527
+ this.emit({
528
+ type: 'batch:item:finished',
529
+ jobId: job.id,
530
+ itemId: item.id,
531
+ index: item.index,
532
+ total: job.items.length,
533
+ outputAssetId,
534
+ duration,
535
+ });
536
+ this.emitProgress(job);
537
+ } catch (err) {
538
+ // B1 守卫:被 cancel 的 workflow 抛错不计入 failed(item 已是 cancelled)
539
+ if (job.cancelled) {
540
+ item.status = 'cancelled';
541
+ return;
542
+ }
543
+ // 失败:判断是否还可重试
544
+ item.attempts++;
545
+ const maxRetries = item.maxRetries;
546
+ if (item.attempts <= maxRetries) {
547
+ // 重试:回 pending,schedule 会再次拉起
548
+ item.status = 'pending';
549
+ // 注意:不重置 attempts,保留累计重试次数
550
+ // 清理本次导入的 input asset,避免重试重新 import 时旧 input 成为孤儿
551
+ // (Blocker 修复:maxRetries=3 全失败原本会累积 3 个孤儿 input)
552
+ if (inputAssetId !== undefined) {
553
+ void this.runtime.removeAsset(inputAssetId).catch(() => {});
554
+ }
555
+ } else {
556
+ item.status = 'failed';
557
+ item.error = err instanceof Error ? err : new Error(String(err));
558
+ job.failed++;
559
+ // M4:最终失败时清理已导入的 input asset,避免批量失败累积孤儿资产
560
+ // 占用 OPFS/IDB 空间并污染 listAssets / StatusBar 配额
561
+ if (inputAssetId !== undefined) {
562
+ void this.runtime.removeAsset(inputAssetId).catch(() => {});
563
+ }
564
+ this.emit({
565
+ type: 'batch:item:failed',
566
+ jobId: job.id,
567
+ itemId: item.id,
568
+ index: item.index,
569
+ total: job.items.length,
570
+ error: item.error,
571
+ attempts: item.attempts,
572
+ });
573
+ this.emitProgress(job);
574
+ }
575
+ } finally {
576
+ // 触发 schedule 补满(用 ref 打破循环依赖)
577
+ if (!job.cancelled && !job.paused && !this.isTerminal(job.status)) {
578
+ void this.schedule(job);
579
+ }
580
+ }
581
+ }
582
+
583
+ /** 当前并发槽位(按内存压力动态收缩) */
584
+ private currentConcurrency(job: BatchJobInternal): number {
585
+ const pressure = this.memoryGuard?.getPressure() ?? 'low';
586
+ return shrinkConcurrencyByPressure(job.baseConcurrency, pressure);
587
+ }
588
+
589
+ /** 发出 progress 事件 + 通知订阅者 */
590
+ private emitProgress(job: BatchJobInternal): void {
591
+ const payload: BatchProgress = {
592
+ jobId: job.id,
593
+ completed: job.completed,
594
+ failed: job.failed,
595
+ total: job.items.length,
596
+ };
597
+ this.emit({
598
+ type: 'batch:progress',
599
+ ...payload,
600
+ });
601
+ const subs = this.progressSubs.get(job.id);
602
+ if (subs) {
603
+ // M1 修复:遍历副本,防止 handler 内 unsubscribe/subscribe mutate 正在迭代的 Set
604
+ for (const fn of [...subs]) {
605
+ try {
606
+ fn(payload);
607
+ } catch {
608
+ // 单个订阅者异常不阻断其他订阅者
609
+ }
610
+ }
611
+ }
612
+ }
613
+
614
+ /** 检查 job 是否全部完成,如是则发 batch:completed */
615
+ private maybeComplete(job: BatchJobInternal): void {
616
+ if (this.isTerminal(job.status)) return;
617
+ const pending = job.items.some(
618
+ (i) => i.status === 'pending' || i.status === 'processing'
619
+ );
620
+ if (pending) return;
621
+
622
+ job.endedAt = Date.now();
623
+ job.status = job.failed > 0 ? 'failed' : 'completed';
624
+ // 注意:无论 job.status 是 'completed' 还是 'failed',都发 batch:completed 事件。
625
+ // 事件名表示"所有项已终结(含部分失败)",而非"全部成功"。
626
+ // 消费方应通过 payload.failed 区分;waitForCompletion 依赖此行为(只监听
627
+ // batch:completed + batch:cancelled 两种终态事件)。
628
+ this.emit({
629
+ type: 'batch:completed',
630
+ jobId: job.id,
631
+ total: job.items.length,
632
+ completed: job.completed,
633
+ failed: job.failed,
634
+ duration: job.endedAt - job.startedAt,
635
+ });
636
+ // M5:进入终态清理进度订阅,避免 waitForCompletion 调用方丢弃 promise 后泄漏
637
+ this.cleanupJobSubs(job.id);
638
+ }
639
+
640
+ private isTerminal(status: BatchJobStatus): boolean {
641
+ return (
642
+ status === 'completed' ||
643
+ status === 'cancelled' ||
644
+ status === 'failed'
645
+ );
646
+ }
647
+
648
+ /** 派生每项的 workflow id(独立于其他项,便于 cancel) */
649
+ private itemWorkflowId(jobId: string, itemId: string): string {
650
+ return `${jobId}__${itemId}`;
651
+ }
652
+
653
+ /** 安全 emit(无 EventBus 时降级为 no-op,便于纯单元测试) */
654
+ private emit(event: LokvisEvent): void {
655
+ try {
656
+ this.eventBus.emit(event);
657
+ } catch {
658
+ // EventBus 异常不阻断批量逻辑
659
+ }
660
+ }
661
+
662
+ /** 内部可变状态 → 对外只读视图(浅拷贝 items 数组与每项) */
663
+ private toJobView(job: BatchJobInternal): BatchJob {
664
+ return {
665
+ id: job.id,
666
+ status: job.status,
667
+ items: job.items.map((i) => ({ ...i })),
668
+ completed: job.completed,
669
+ failed: job.failed,
670
+ total: job.items.length,
671
+ startedAt: job.startedAt,
672
+ endedAt: job.endedAt,
673
+ };
674
+ }
675
+ }
676
+
677
+ /** BatchJob 内部可变状态(BatchProcessor 私有) */
678
+ interface BatchJobInternal {
679
+ id: string;
680
+ status: BatchJobStatus;
681
+ baseConcurrency: number;
682
+ maxRetries: number;
683
+ items: BatchItemInternal[];
684
+ completed: number;
685
+ failed: number;
686
+ startedAt: number;
687
+ endedAt?: number;
688
+ /** schedule 重入锁 */
689
+ scheduleLock: boolean;
690
+ paused: boolean;
691
+ cancelled: boolean;
692
+ }
693
+
694
+ /** BatchItem 内部可变状态 */
695
+ interface BatchItemInternal extends Omit<BatchItem, 'outputAssetId' | 'error' | 'duration'> {
696
+ outputAssetId?: AssetId;
697
+ error?: Error;
698
+ duration?: number;
699
+ }