Files
hose-use/src/domains/platform/workflow/watch/helpers.ts
T
gitea 71aa9b54a7 refactor(workflow): 收敛 Job 状态语义与监听事件过滤
同一份逻辑在两个地方各写一遍,必然在某次修改后漂移:

- Job 状态判定:watch/helpers.ts 与 execution/debug.ts 各有一份终态集合,
  且对数字 state 的解释相反——watch 把 state:0 视为 COMPLETED,
  debug 把它当成未知继续轮询到超时,同一个 Job 在两条路径上结论相反。
  统一到 execution/job-status.ts(watch 已依赖 execution,方向单向),
  helpers 保留转发导出,debug 删除本地实现;新增两条用例固定该语义。
- --failed-only 事件过滤:pipeline-watcher.ts 与 single-job.ts 逐字重复,
  抽为 watch/helpers.ts 的 shouldEmitWatchEvent(),两处调用点同步瘦身。

同步更新 execution 模块 README,说明状态语义的两个入口必须一起考虑。
2026-11-01 22:02:06 +08:00

48 lines
1.7 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import type { WorkflowTaskExecution } from "../types.js";
import type { WatchWorkflowOptions, WorkflowWatchEvent } from "./types.js";
// Job 状态判定统一来自 execution/job-status.ts,watch 与 debug 共用同一套语义。
export { normalizeJobStatus, isJobTerminal, TERMINAL_JOB_STATES } from "../execution/job-status.js";
/**
* `--failed-only` 的事件过滤。此前的实现在两个监听入口各写了一份且逐字相同,
* 任一侧漏改都会让两种监听模式给出不同的结果。
*/
export function shouldEmitWatchEvent(
event: WorkflowWatchEvent,
options: Pick<WatchWorkflowOptions, "failedOnly">
): boolean {
if (!options.failedOnly) return true;
return (
event.type === "task_failed" ||
(event.type === "task_skipped" && Boolean(event.error)) ||
event.type === "job_completed" ||
event.type === "timeout" ||
event.type === "cancelled"
);
}
/**
* 提取节点执行失败或跳过时的具体错误原因
*/
export function extractTaskErrorMessage(node: WorkflowTaskExecution): string | undefined {
if (!node) return undefined;
const anyNode = node as any;
if (node.error?.message && String(node.error.message).trim()) {
return String(node.error.message).trim();
}
if (anyNode.errorMessage && String(anyNode.errorMessage).trim()) {
return String(anyNode.errorMessage).trim();
}
const outputs = anyNode.outputs ?? anyNode.output;
if (outputs && typeof outputs === "object") {
if (outputs.error) {
return typeof outputs.error === "string" ? outputs.error.trim() : JSON.stringify(outputs.error);
}
if (outputs.message && typeof outputs.message === "string") {
return outputs.message.trim();
}
}
return undefined;
}