dynamic-workflow-run-retune.ts 3.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. // ============================================================
  2. // Dynamic Workflow Run Service:就地改一个在飞 run 的并发上界
  3. // ============================================================
  4. // 这是 `DynamicWorkflowRunPort.retuneConcurrency` 的实现体,与三条启动入口(dynamic-workflow-run-submit.ts)刻意分居:那三条
  5. // 都会**铸一个 run**,这一条一个 run 都不铸——它只对着已经在飞的那个发一条命令。
  6. //
  7. // 两种拒绝在**不同的地方**判定,这正是让它们分得开的原因:
  8. // - `unchanged` 是本模块自己的答案,读的是服务此刻持有的上界,在句柄被调之前就判完;
  9. // - 于是引擎的 setter 只剩一个说 false 的理由——run 在这两步之间结算了——端口把它报成 `not_live`。
  10. //
  11. // 顺序也是载荷性的:先句柄(引擎换 caps + 写行 + 记事件 + 闸门换上界,一个同步片),成了才动
  12. // 服务自己的内存副本。反过来就会在 run 恰好结算的那一瞬留下一个与 `dwf_run` 行不符的内存上界,
  13. // 而快照与详情读的正是那一份。
  14. import type {
  15. DynamicWorkflowRunRetuneRequest,
  16. DynamicWorkflowRunRetuneResult,
  17. } from "@zcode/contracts";
  18. import type { JournalStorePort } from "@zcode/dynamic-workflow";
  19. import type { RunRegistryEntry } from "./dynamic-workflow-run-observation.js";
  20. import { clampRunConcurrency } from "./workflow-concurrency-ceiling.js";
  21. /** 本模块借用的 service 内部状态;全是引用,本文件不持有任何自己的状态。 */
  22. export interface DynamicWorkflowRunRetuneContext {
  23. runs: Map<string, RunRegistryEntry>;
  24. journal: JournalStorePort;
  25. /** 与 caps 起点、两条读面判据同一个函数(见 run service 的 `concurrencyCeiling`)。 */
  26. concurrencyCeiling: () => number;
  27. }
  28. export function retuneRunConcurrency(
  29. ctx: DynamicWorkflowRunRetuneContext,
  30. request: DynamicWorkflowRunRetuneRequest,
  31. ): DynamicWorkflowRunRetuneResult {
  32. const ceiling = ctx.concurrencyCeiling();
  33. // `null` = 天花板 = 解除本 run 自己的限制。与提交时**同一条**钳制(工具层已经钳过一次是为了让
  34. // 确认窗显示将要生效的值;端口再钳是端口自己的契约,两次必然同值)。
  35. const next = clampRunConcurrency(request.maxConcurrency ?? undefined, ceiling);
  36. const entry = ctx.runs.get(request.runId);
  37. if (entry === undefined || entry.terminal !== undefined || entry.control === undefined) {
  38. // 本 service 手上没有这个活条目:从来不是本进程的 run、早已结算的 run,或者升级前留下的
  39. // 无控制面条目。三者对调用方是同一个下一步——走一次真正的修订。
  40. return { ok: false, reason: "not_live" };
  41. }
  42. // 此刻生效的上界:**有条目就读条目**(三条建条目的路都落值),冷行才回退到 journal。
  43. // 两者都没有只可能是 submit → createRun 之间那几个微任务里的接线异常,按「跑在天花板上」读。
  44. const current =
  45. entry.maxConcurrency ?? ctx.journal.getRun(request.runId)?.caps.maxConcurrency ?? ceiling;
  46. if (current === next) return { ok: false, reason: "unchanged", current };
  47. if (!entry.control.setMaxConcurrency(next)) {
  48. // 条目还在、引擎却说没改:run 在存活判定与这一行之间结算了(竞态),或者引擎还没接上
  49. // (launch 之前的那几个微任务)。两者都是「这一刻不可就地改」。
  50. return { ok: false, reason: "not_live", current };
  51. }
  52. // 内存副本一并挪动:快照读的是 `entry.maxConcurrency ?? 行`,条目优先——不挪就会在 retune
  53. // 之后继续报提交时那个数,而 `AmendWorkflow` 的 resolveInput 正是读这张快照判「沿用什么」。
  54. entry.maxConcurrency = next;
  55. return { ok: true, maxConcurrency: next, previous: current, ceiling };
  56. }