diff --git a/skills/seal-use/references/staging-simulation-workflow.md b/skills/seal-use/references/staging-simulation-workflow.md index 99dbfc7..1bcf539 100644 --- a/skills/seal-use/references/staging-simulation-workflow.md +++ b/skills/seal-use/references/staging-simulation-workflow.md @@ -207,6 +207,7 @@ flowchart TD # 底层直接推送出站调试消息(仅限 assisted 模式) seal-use deploy hosecloud test-outbound --flow-id --message-id --corp --yes ``` + 指定 `--rejection-num N` 时只接受第 N 轮出站日志,找不到则报错;按审核记录创建时间推测出站事件时,只接受相差小于 60 秒的记录,不会退回更旧的轮次或日志。 > **专属安全红线契约**: > 1. **双向绑定**:必须在已绑定的生产-测试企业对之间执行;严禁跨越非关联租户(未带 `--force` 时直接报 Exit 14 阻断)。 > 2. **锁死 assisted 模式**:目标测试环境的合思配置必须为 `assisted` 辅助模式,否则直接触发 Exit 14 安全阻断。 diff --git a/src/domains/seal/hose-outbound-replay.test.ts b/src/domains/seal/hose-outbound-replay.test.ts index bb74c22..71a4295 100644 --- a/src/domains/seal/hose-outbound-replay.test.ts +++ b/src/domains/seal/hose-outbound-replay.test.ts @@ -140,6 +140,80 @@ describe("selectMatchingOutboundLog", () => { ]; expect(selectMatchingOutboundLog(emptyLogs)).toBeUndefined(); }); + + test("an unavailable iteration never falls back to another one", () => { + expect(selectMatchingOutboundLog(mockLogs, { rejectionNum: 99 })).toBeUndefined(); + expect(selectMatchingOutboundLog(mockLogs, { rejectionNum: 0 })).toBeUndefined(); + }); + + test("an explicit iteration also constrains message and callback matching", () => { + expect(selectMatchingOutboundLog(mockLogs, { + messageId: "ID01WahQ3gbhMP", rejectionNum: 3 + })).toBeUndefined(); + expect(selectMatchingOutboundLog(mockLogs, { + recordId: "01a08f69-ad6f-76b8-8c81-81190544150d", rejectionNum: 3 + })).toBeUndefined(); + }); + + test("timestamp matching has a strict 60-second window and preserves clock-skew tolerance", () => { + const time = mockLogs[1]!.createTime; + const invoking = [mockLogs[1]!]; + for (const delta of [0, 59_999, -1000]) { + expect(selectMatchingOutboundLog(invoking, { + recordId: "unknown-record", recordCreatedAt: time + delta + })?.id).toBe("log-invoking-3"); + } + for (const delta of [60_000, 864_000_000, -1001]) { + expect(selectMatchingOutboundLog(invoking, { + recordId: "unknown-record", recordCreatedAt: time + delta + })).toBeUndefined(); + } + expect(selectMatchingOutboundLog(invoking, { recordCreatedAt: NaN })).toBeUndefined(); + }); + + test("timestamp matching selects the nearest event within the requested iteration", () => { + const recordCreatedAt = 100_000; + const logs: HoseOutboundLogItem[] = [ + { ...mockLogs[1]!, id: "different-iteration", createTime: 99_999 }, + { ...mockLogs[3]!, id: "near", createTime: 95_000 }, + { ...mockLogs[3]!, id: "far", createTime: 70_000 } + ]; + expect(selectMatchingOutboundLog(logs, { + recordCreatedAt, rejectionNum: 2 + })?.id).toBe("near"); + }); + + test("a requested record without matching evidence is unresolved", () => { + expect(selectMatchingOutboundLog(mockLogs, { recordId: "unknown-record" })).toBeUndefined(); + }); + + test("latest selection does not depend on input order or mutate the log list", () => { + const reversed = [...mockLogs].reverse(); + const ids = reversed.map((item) => item.id); + expect(selectMatchingOutboundLog(reversed)?.id).toBe("log-invoking-3"); + expect(reversed.map((item) => item.id)).toEqual(ids); + }); + + test("callback lookup stays on the callback's document and node", () => { + const callback = mockLogs[2]!; + const unrelated = { + ...mockLogs[3]!, id: "other-node", createTime: callback.createTime - 1, + request: { ...mockLogs[3]!.request, nodeId: "FLOW:other-node" } + }; + expect(selectMatchingOutboundLog([unrelated, ...mockLogs], { + recordId: "01a08f69-ad6f-76b8-8c81-81190544150d" + })?.id).toBe("log-invoking-2"); + }); + + test("a callback for a different iteration cannot be paired with an older event on the same node", () => { + const recordId = "01a0a43c-521b-778c-b11b-238c8496dfec"; + const old = { + ...mockLogs[3]!, request: { ...mockLogs[3]!.request, nodeId: mockLogs[1]!.request!.nodeId } + }; + expect(selectMatchingOutboundLog([mockLogs[0]!, mockLogs[1]!, old], { + recordId, rejectionNum: 2 + })).toBeUndefined(); + }); }); describe("validateHoseOutboundReplaySafety", () => { diff --git a/src/domains/seal/hose-outbound-replay.ts b/src/domains/seal/hose-outbound-replay.ts index 6f3745c..cd5ec67 100644 --- a/src/domains/seal/hose-outbound-replay.ts +++ b/src/domains/seal/hose-outbound-replay.ts @@ -53,7 +53,13 @@ export function selectMatchingOutboundLog( rejectionNum?: number; } = {} ): HoseOutboundLogItem | undefined { - const invokingLogs = logs.filter((item) => item.type === "INVOKING"); + // An explicit iteration is a constraint on every matching strategy, including + // callback and timestamp lookups; it must never fall back to a different one. + const allInvokingLogs = logs.filter((item) => item.type === "INVOKING") + .sort((a, b) => b.createTime - a.createTime); + const invokingLogs = allInvokingLogs.filter((item) => + (options.rejectionNum === undefined || item.request?.rejectionNum === options.rejectionNum) + ); if (invokingLogs.length === 0) { return undefined; } @@ -73,29 +79,36 @@ export function selectMatchingOutboundLog( ); if (callbackWithRecordId) { // Find the invoking event right before this callback - const priorInvoking = invokingLogs.find( - (item) => item.createTime <= callbackWithRecordId.createTime + const priorInvoking = allInvokingLogs.find( + (item) => item.createTime <= callbackWithRecordId.createTime && + (!callbackWithRecordId.request?.flowId || + (item.request?.flowId ?? item.flowId?.id) === callbackWithRecordId.request.flowId) && + (!callbackWithRecordId.request?.nodeId || item.request?.nodeId === callbackWithRecordId.request.nodeId) ); - if (priorInvoking) { - return priorInvoking; - } - } - - // Fallback: match by timestamp delta (< 60s) - if (options.recordCreatedAt) { - const closestByTime = invokingLogs - .filter((item) => item.createTime <= options.recordCreatedAt! + 1000) - .sort( - (a, b) => - Math.abs(options.recordCreatedAt! - a.createTime) - - Math.abs(options.recordCreatedAt! - b.createTime) - )[0]; - if (closestByTime) { - return closestByTime; - } + return options.rejectionNum === undefined || priorInvoking?.request?.rejectionNum === options.rejectionNum + ? priorInvoking + : undefined; } } + // Fallback: match by timestamp delta (< 60s) + if (options.recordCreatedAt !== undefined) { + const recordCreatedAt = options.recordCreatedAt; + if (!Number.isFinite(recordCreatedAt)) return undefined; + const closestByTime = invokingLogs + .filter((item) => item.createTime <= recordCreatedAt + 1000 && + Math.abs(recordCreatedAt - item.createTime) < 60_000) + .sort( + (a, b) => + Math.abs(recordCreatedAt - a.createTime) - + Math.abs(recordCreatedAt - b.createTime) + )[0]; + return closestByTime; + } + + // A requested record with no callback or usable timestamp is unresolved. + if (options.recordId) return undefined; + // 3. Match by rejectionNum if provided if (options.rejectionNum !== undefined) { const matchRejection = invokingLogs.find( @@ -221,7 +234,7 @@ export async function executeHoseOutboundReplay(options: HoseOutboundReplayOptio if (!selectedLog || !selectedLog.request?.messageId) { throw validationError( - `在合思出站日志中未找到单据「${sn}」的有效出站记录 (type: INVOKING)。\n` + + `在合思出站日志中未找到单据「${sn}」${options.rejectionNum !== undefined ? `第 ${options.rejectionNum} 轮` : ""}的匹配出站记录 (type: INVOKING)。\n` + `请确认该单据在合思审批流中是否触发过出站消息(EBot节点),或显式提供 --message-id 参数。` ); }