diff --git a/src/domains/hose/flow.test.ts b/src/domains/hose/flow.test.ts index 6e3f1f7..1d74bca 100644 --- a/src/domains/hose/flow.test.ts +++ b/src/domains/hose/flow.test.ts @@ -1215,5 +1215,47 @@ describe("Hose Flow Domain", () => { "1. 费用类型是研发费(宁艳楠): 同意\n2. 财务经理(周晚容): 同意\n3. 高思琦: 支付成功" ); }); + + test("cross-period partial query: identifies historical rejected run even if queried in isolation without subsequent runs in batch", () => { + // User only queried June (so Seal batch only contains Run 1). + // But Hose document later had Run 2 in July and was paid in August. + const singleIsolatedRun = [ + { + id: "run-june-round1", + sourceDocumentSN: "B26009999", + taskMode: "assisted", + createdAt: 100000, + status: "completed", + manualApprovalRecord: { + status: { type: "TERMINATED", label: "已驳回" }, + rejectComment: "发票模糊" + }, + result: { decision: "REJECT" } + } + ] as any[]; + + const fullLifetimeHoseFlow = { + state: "已支付", + rawState: "paid", + history: [ + // June round (Run 1) + { time: 90000, action: "提交送审", rawAction: "freeflow.submit", operator: { name: "张三" } }, + { time: 101000, action: "审批同意", rawAction: "freeflow.agree", isEbot: true, operator: { name: "Ebot" } }, + { time: 110000, action: "审批驳回", rawAction: "freeflow.reject", nodeName: "财务审批", comment: "发票模糊", operator: { name: "李四" } }, + // July round (Run 2, not in current query batch) + { time: 200000, action: "提交送审", rawAction: "freeflow.submit", operator: { name: "张三" } }, + { time: 201000, action: "审批同意", rawAction: "freeflow.agree", isEbot: true, operator: { name: "Ebot" } }, + { time: 210000, action: "审批同意", rawAction: "freeflow.agree", nodeName: "财务审批", comment: "同意", operator: { name: "李四" } }, + { time: 220000, action: "支付成功", rawAction: "freeflow.pay", operator: { name: "王五" } } + ] + } as any; + + const aligned = alignApprovalRunsWithHoseFlow(singleIsolatedRun, fullLifetimeHoseFlow); + const r = aligned.get("run-june-round1")!; + // Must NOT be falsely labeled as "已支付" just because Hose is currently paid! + expect(r.eHumanAction).toBe("审批驳回"); + expect(r.eHumanComment).toBe("财务审批(李四): 发票模糊"); + expect(r.auditDrift).toBe(false); + }); }); }); diff --git a/src/domains/hose/flow.ts b/src/domains/hose/flow.ts index a1bb8a0..90ccfa6 100644 --- a/src/domains/hose/flow.ts +++ b/src/domains/hose/flow.ts @@ -965,8 +965,26 @@ export function alignApprovalRunsWithHoseFlow( if (!run) continue; const runTime = Number(run.createdAt) || 0; const nextRun = sortedRuns[i + 1]; - const nextRunTime = nextRun ? Number(nextRun.createdAt) || Infinity : Infinity; - const isLatestRun = i === sortedRuns.length - 1; + let nextRunTime = nextRun ? Number(nextRun.createdAt) || Infinity : Infinity; + let isLatestRun = i === sortedRuns.length - 1; + + // 跨期/分段切片安全防线: + // 若导出时只圈选了局部时间范围(例如横跨3个月的单据只导出了中间某个月),Seal 内存中可能只有单条历史 Run。 + // 我们利用合思全生命周期流水 (flow.history) 反向校验:如果在当前 Run 之后存在后续新的 AI 审批 (isEbot), + // 则直接证明当前 Run 已被后续更新的 AI 轮次所替代,绝非最新终态轮次! + const subsequentEbot = history.find( + (e) => + (e.time ?? 0) > runTime + 5000 && + e.isEbot && + (e.rawAction === "freeflow.agree" || e.action === "智能审批" || e.nodeName?.includes("智能")) + ); + + if (subsequentEbot) { + isLatestRun = false; + if ((subsequentEbot.time ?? Infinity) < nextRunTime) { + nextRunTime = subsequentEbot.time!; + } + } const res = (run.result as Record | undefined) || {}; const rawAiDecision = (res.decision as string | undefined) || (run as Record).aiDecision || "-";