diff --git a/src/domains/hose/flow.ts b/src/domains/hose/flow.ts index 90ccfa6..70e71c8 100644 --- a/src/domains/hose/flow.ts +++ b/src/domains/hose/flow.ts @@ -189,8 +189,16 @@ export function parseHoseTimeline( .map(extractFlowPerson) .filter((p): p is FlowPerson => Boolean(p)); + const nodeId = + typeof attributes.nodeId === "string" && attributes.nodeId + ? attributes.nodeId + : typeof log.nodeId === "string" && log.nodeId + ? log.nodeId + : undefined; + return { id: typeof log.id === "string" ? log.id : undefined, + nodeId, time: currentTime, durationMs, state, @@ -964,6 +972,32 @@ export function alignApprovalRunsWithHoseFlow( const run = sortedRuns[i]; if (!run) continue; const runTime = Number(run.createdAt) || 0; + + const doc = (run.document as Record | undefined) || {}; + const mar = + ((run as Record).manualApprovalRecord as Record | undefined) || + ((run.result as Record | undefined)?.manualApprovalRecord as Record | undefined) || + ((doc as Record).manualApprovalRecord as Record | undefined) || + {}; + + // 1. 强特征 Node ID 锚定:优先从 sourceExtendData.nodeId 或 manualApprovalRecord.nodeId 反查合思真实节点 + const sourceNodeId = + typeof (run.sourceExtendData as Record | undefined)?.nodeId === "string" + ? String((run.sourceExtendData as Record).nodeId) + : undefined; + const rawMarNodeId = typeof mar.nodeId === "string" ? String(mar.nodeId) : undefined; + const decodedMarNodeId = rawMarNodeId + ? decodeURIComponent(rawMarNodeId).replace(/^hcnode_/, "").replace(/_r\d+$/, "") + : undefined; + const targetNodeId = sourceNodeId || decodedMarNodeId; + + const matchedNodeEvent = targetNodeId + ? history.find((e) => e.nodeId && (e.nodeId === targetNodeId || targetNodeId.includes(e.nodeId))) + : undefined; + + // 锚定基准时间:优先采用合思对应节点的真实执行时刻;若无对应节点,放宽多系统排队容差至 60 秒 + const effectiveRunTime = matchedNodeEvent?.time ?? runTime; + const nextRun = sortedRuns[i + 1]; let nextRunTime = nextRun ? Number(nextRun.createdAt) || Infinity : Infinity; let isLatestRun = i === sortedRuns.length - 1; @@ -974,7 +1008,7 @@ export function alignApprovalRunsWithHoseFlow( // 则直接证明当前 Run 已被后续更新的 AI 轮次所替代,绝非最新终态轮次! const subsequentEbot = history.find( (e) => - (e.time ?? 0) > runTime + 5000 && + (e.time ?? 0) > effectiveRunTime + 5000 && e.isEbot && (e.rawAction === "freeflow.agree" || e.action === "智能审批" || e.nodeName?.includes("智能")) ); @@ -1010,12 +1044,6 @@ export function alignApprovalRunsWithHoseFlow( else if (isReject) aiAction = isAutomatic ? "驳回" : "添加评论"; else if (normDecision === "REVIEW" || normDecision === "转交复核") aiAction = "转交复核"; - const doc = (run.document as Record | undefined) || {}; - const mar = - ((run as Record).manualApprovalRecord as Record | undefined) || - (res.manualApprovalRecord as Record | undefined) || - ((doc as Record).manualApprovalRecord as Record | undefined) || - {}; const marStatus = typeof mar.status === "object" && mar.status !== null ? String((mar.status as Record).type || (mar.status as Record).label || "").toUpperCase() @@ -1060,10 +1088,11 @@ export function alignApprovalRunsWithHoseFlow( marStatus === "已驳回" || marStatus === "被驳回"); - // 5-second clock skew tolerance window [runTime - 5000, nextRunTime) + // 容差窗口:若已通过 nodeId 准确锚定,前后仅需 1 秒同步边沿;若未锚定,提供 60 秒跨系统排队与处理缓冲 + const skewToleranceMs = matchedNodeEvent ? 1000 : 60000; const windowEvents = history.filter((e) => { const t = e.time ?? 0; - return t >= runTime - 5000 && t < nextRunTime; + return t >= effectiveRunTime - skewToleranceMs && t < nextRunTime; }); let eHumanAction = "-"; @@ -1074,7 +1103,7 @@ export function alignApprovalRunsWithHoseFlow( // Check for Post-AI Mutation / Audit Drift const isAiApproved = isApprove || isExemptApprove; - const postAiEvents = history.filter((e) => (e.time ?? 0) > runTime + 5000); + const postAiEvents = history.filter((e) => (e.time ?? 0) > effectiveRunTime + 5000); const hasPostAiRetract = postAiEvents.some( (e) => e.rawAction === "freeflow.retract" || e.action === "单据撤回" ); diff --git a/src/domains/hose/types.ts b/src/domains/hose/types.ts index 233bab1..35f5185 100644 --- a/src/domains/hose/types.ts +++ b/src/domains/hose/types.ts @@ -42,6 +42,7 @@ export type FlowPerson = z.infer; export const HoseFlowTimelineEntrySchema = z.object({ id: z.string().optional(), + nodeId: z.string().optional(), time: z.number().optional(), durationMs: z.number().optional(), state: z.string().optional(),