workflow-runs-artifacts.ts 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. // ============================================================
  2. // workflowRuns 归约里的用户面产物部分
  3. // ============================================================
  4. // 从 workflow-runs-reducer.ts 拆出(max-lines 门),与 workflow-runs-concurrency.ts 同一先例:
  5. // 主归约只剩 switch 的分派,产物的三条规则住在这里。同一条纪律:纯函数、无时钟、无 I/O。
  6. //
  7. // ⚠ 术语:本模块的 artifact 是脚本经
  8. // `artifact.*` 发布给**用户**看的产出。主归约里 `resultPreview` 调用的
  9. // `serializeWorkflowArtifact`(workflow-artifact.ts,单数)里那个 artifact 是**另一个意思**
  10. // ——引擎内部对「脚本顶层返回值」的叫法,给模型看的。两者无关。
  11. //
  12. // 三条规则各有各的键,这是本模块最要紧的一句话:
  13. // artifact-published ⇒ 按**产物 id** upsert。同 id 再发布是新版本,必须覆盖同一张卡;
  14. // 而同一个 id 的两个版本来自两个不同的**站点实例**,所以这里不能
  15. // 用 nodes/actors/reports 那把 (siteId, ordinal) 钥匙。
  16. // artifact-failed ⇒ 不动状态(在主归约里,只有一行 return)。失败行不认领 id / 种类 /
  17. // 版本,没有可 upsert 的东西。
  18. // report(artifactId) ⇒ 该 id 的 itemCount 加一。去重键回到 (siteId, ordinal)——它是一条
  19. // report,身份仍是 report 的身份。
  20. import {
  21. WORKFLOW_ARTIFACT_LIMITS,
  22. type WorkflowRunArtifactKind,
  23. type WorkflowRunArtifactSummary,
  24. } from "./workflow-artifacts.js";
  25. /** 产物的六个成员种类。归约不校验 schema,所以这里自带一张闭集表判读。 */
  26. const ARTIFACT_KINDS: ReadonlySet<string> = new Set<WorkflowRunArtifactKind>([
  27. "file",
  28. "markdown",
  29. "chart",
  30. "table",
  31. "metrics",
  32. "board",
  33. ]);
  34. /**
  35. * `artifact-published` 载荷里的 `ArtifactVersionRecord` → 快照里的**最新版元数据**。
  36. *
  37. * 裁剪掉 `versions` / `spec` / `description` / `uri` / `sourcePath` / `publishedAt`:快照是
  38. * 高频状态键,它的读者只需要「有哪些产物、现在第几版、变了没有」。全量元数据走
  39. * `workflowRunArtifacts` 查询,字节走 `workflowRunArtifactRead`——权威始终在 journal。
  40. *
  41. * `id` / `kind` / `version` 缺任一即无从展示(返回 undefined,只抬水位):一张认不出种类的卡
  42. * 既选不出图标也分派不了渲染器。其余字段有则带、无则整键缺席。
  43. */
  44. export function workflowArtifactSummary(value: unknown): WorkflowRunArtifactSummary | undefined {
  45. if (!isPlainRecord(value)) return undefined;
  46. const id = nonEmptyString(value.id);
  47. const kind = nonEmptyString(value.kind);
  48. const version = value.version;
  49. if (id === undefined || kind === undefined || !ARTIFACT_KINDS.has(kind)) return undefined;
  50. if (typeof version !== "number" || !Number.isInteger(version) || version < 1) return undefined;
  51. const title = nonEmptyString(value.title);
  52. const contentType = nonEmptyString(value.contentType);
  53. const bytes = value.bytes;
  54. return {
  55. id: id.slice(0, WORKFLOW_ARTIFACT_LIMITS.maxIdLength),
  56. kind: kind as WorkflowRunArtifactKind,
  57. ...(title === undefined
  58. ? {}
  59. : { title: title.slice(0, WORKFLOW_ARTIFACT_LIMITS.maxTitleLength) }),
  60. version: Math.min(version, WORKFLOW_ARTIFACT_LIMITS.maxVersions),
  61. ...(contentType === undefined ? {} : { contentType: contentType.slice(0, 128) }),
  62. ...(typeof bytes === "number" && Number.isInteger(bytes) && bytes >= 0 ? { bytes } : {}),
  63. ...(value.primary === true ? { primary: true as const } : {}),
  64. };
  65. }
  66. /**
  67. * 按**产物 id** upsert 进有界的产物表,并**保留已有条目的 `itemCount`**。
  68. *
  69. * 保留计数是这个函数与 `upsertBoundedByInstance` 唯一的实质差别,也是它存在的理由:新版本
  70. * 的记录里没有 `itemCount`(那是标签 report 数出来的,不是发布时刻的事实),整条替换会把
  71. * 一块正在被喂数据的看板的计数清零——刷新信号归零,看板从此不再增量取数。
  72. *
  73. * 触界语义与其他三张表同族:拒绝新条目、已有条目照常更新。
  74. */
  75. export function upsertBoundedByArtifactId(
  76. list: readonly WorkflowRunArtifactSummary[],
  77. entry: WorkflowRunArtifactSummary,
  78. limit: number,
  79. ): { list: WorkflowRunArtifactSummary[]; truncated: boolean } {
  80. const index = list.findIndex((item) => item.id === entry.id);
  81. if (index >= 0) {
  82. const previous = list[index]!;
  83. const next = [...list];
  84. next[index] = {
  85. ...entry,
  86. ...(previous.itemCount === undefined ? {} : { itemCount: previous.itemCount }),
  87. };
  88. return { list: next, truncated: false };
  89. }
  90. if (list.length >= limit) return { list: [...list], truncated: true };
  91. return { list: [...list, entry], truncated: false };
  92. }
  93. /**
  94. * 一条打了标签的 `report` 落在某个产物上:把该 id 的 `itemCount` 加一。
  95. *
  96. * 表里没有这个 id 时**忽略计数**(返回原表,条目仍照常进 `reports`)。运行期这是不可能的
  97. * ——`report(item, tag)` 的 tag 必须先被声明为预置产物,否则引擎 failRun——所以这里纯粹是
  98. * 防御:事件乱序或载荷残缺时,宁可少一个计数,也不要凭空造出一张没有 spec、渲染不了的卡。
  99. *
  100. * **计数的失准方向是有意的。** 去重键 (siteId, ordinal) 来自 `reports` 表,而那张表在 64 条
  101. * 之后拒收新条目——超界之后一条被重放的标签 report 认不出自己是重放,会多数一次。选择这个
  102. * 方向是因为 `itemCount` 的职责只是**刷新信号**:多数一次让看板多发一次带 `afterSequence`
  103. * 的增量查询、拿回零行,无害;少数一次(例如改成"超界就不再计数")会让折线在第 64 个点上
  104. * 永远冻住,而那正是这个特性存在的理由。触界时 `truncated` 已置位,读者据此知道 `reports`
  105. * 与 `itemCount` 都已是近似值;条目的**权威**计数在 journal,经 `workflowRunArtifactData` 翻页得到。
  106. */
  107. export function countTaggedReport(
  108. list: readonly WorkflowRunArtifactSummary[] | undefined,
  109. artifactId: string,
  110. ): WorkflowRunArtifactSummary[] | undefined {
  111. if (list === undefined) return undefined;
  112. const index = list.findIndex((item) => item.id === artifactId);
  113. // 返回 undefined = **这个键不用动**:调用方整个不写 artifacts,于是幂等重放得到
  114. // 逐字节相同的 run 对象,顶层的 JSON 比对随即返回 null。
  115. if (index < 0) return undefined;
  116. const previous = list[index]!;
  117. const next = [...list];
  118. next[index] = { ...previous, itemCount: (previous.itemCount ?? 0) + 1 };
  119. return next;
  120. }
  121. function isPlainRecord(value: unknown): value is Record<string, unknown> {
  122. return typeof value === "object" && value !== null && !Array.isArray(value);
  123. }
  124. function nonEmptyString(value: unknown): string | undefined {
  125. return typeof value === "string" && value.length > 0 ? value : undefined;
  126. }