Skip to content

Commit 441a9e0

Browse files
xlorneclaude
andcommitted
fix: 子流程重置复用创建脚本并支持参数与人员设定 #237
重置时重新执行子流程创建脚本,按旧实例创建顺序映射请求并校验数量,保证多实例数据组装不错位。自动提交携带 operatorSelectMap,发起人/审批人设定与首次发起一致。抽取 SubProcessSessionFactory 统一结果判定与重置的父流程会话构建,并补充测试与能力文档。 Co-Authored-By: Claude Code <noreply@anthropic.com>
1 parent e8daed3 commit 441a9e0

6 files changed

Lines changed: 401 additions & 75 deletions

File tree

‎docs/capabilities/flow-engine-framework/sub-process.md‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ symbols:
1616
- SubProcessResultScript
1717
- FlowSubProcessResultService
1818
- FlowSubProcessResetService
19+
- SubProcessSessionFactory
1920
- FlowSubProcessResetRequest
2021
- FlowSubProcessResetEvent
2122
- FlowSession.findSubProcessRecords
@@ -89,7 +90,9 @@ def run(request){
8990
### 接口与定位方式
9091

9192
- 请求 `FlowSubProcessResetRequest`:`recordId`(当前操作的待办记录)+ `resetInstanceProcessIds`(选中重建的实例流程id)+ `advice`(重置说明)。
92-
- **不需要指定子流程节点**:由选中实例的流程id定位其所属聚合组(全部选中实例须同属一个未取代聚合组),重置始终是整个子流程**从头重走一次**——重建实例经由子流程节点配置的创建脚本与自动提交策略重新发起,不跳转、不迁移、不自动跳过任何历史数据。
93+
- **不需要指定子流程节点**:由选中实例的流程id定位其所属聚合组(全部选中实例须同属一个未取代聚合组),重置始终是整个子流程**从头重走一次**——重建时重新执行子流程节点配置的创建脚本,按旧实例在聚合组中的创建顺序取对应的 `FlowCreateRequest`,再按创建脚本的自动提交策略提交;不跳转、不迁移、不自动跳过任何历史数据。
94+
- **参数与人员设定由脚本决定**:重置接口不接受表单、发起人、流程标题或人员设定参数,这些均以创建脚本重新执行的输出为准(`FlowCreateRequest.formData` / `operatorId` / `operatorSelectMap` / `workTitle` / `actionId`),与首次发起完全一致;「发起人设定 / 审批人设定」(`INITIATOR_SELECT` / `APPROVER_SELECT`)类节点在重置后仍由脚本指定的人员接收待办。
95+
- **数量一致性校验**:创建脚本返回的创建请求数量必须与旧聚合组实例数一致,否则抛出 `state.subProcess.resetScriptMismatch` 并拒绝重置,避免按错位请求重建、打乱多实例的创建顺序与数据组装。
9396
- 前置约束:当前记录为待办且操作人匹配;目标聚合组已放行(PASSED)、未被取代、全部实例已结束;当前流程无等待中聚合组;当前记录位于**锁定合并节点**(见下)。
9497

9598
### 锁定合并节点(复杂下游拓扑支持)
@@ -106,7 +109,8 @@ def run(request){
106109

107110
- **聚合组建模**:旧聚合组标记 `superseded`(聚合状态保持不变,历史记录保留有效可查),新建一条聚合组取代其成为当前有效数据。新组由两类实例组成:
108111
- **继承实例**(未选中,`Instance.inherited=true`):沿用原实例的流程id与最终状态,不重新执行;
109-
- **重建实例**(选中):基于旧实例的启动记录反推创建请求(沿用原子流程定义、表单数据、流程标题与原发起人),创建全新子流程(新流程id),`Instance.sourceProcessId` 记录其替换的旧实例流程id,供订阅方完成旧 → 新映射。
112+
- **重建实例**(选中):重新执行子流程创建脚本,按旧实例在聚合组中的创建顺序取对应的 `FlowCreateRequest`(表单数据、发起人、人员设定、流程标题与开始动作均以脚本输出为准,不再基于旧启动记录反推请求),创建全新子流程(新流程id),`Instance.sourceProcessId` 记录其替换的旧实例流程id,供订阅方完成旧 → 新映射。
113+
- **脚本上下文一致**:重置与结果判定共用 `SubProcessSessionFactory` 以锚点记录重建父流程会话(当前节点为子流程节点、表单/审批意见/动作均取自锚点记录),保证重置时创建脚本拿到的 `request` 与首次触发子流程时一致。
110114
- **锚点不变**:新组沿用旧组的 `parentRecordId`(原触发记录),保证结果判定后主流程恢复位置不变。
111115
- **记录链作废**:触发锚点之后的主流程记录链以撤销语义作废(含执行重置的当前待办,重置说明写入该记录供审计),主流程退回子流程节点重新等待——**原待办从待办列表消失,新聚合组放行后生成全新的下游待办**;作废链路与 `FlowRevokeService` 一致,逐条推送 `FlowRecordRevokeEvent`。
112116
- **结果判定自愈**:重建实例全部结束后,`FlowSubProcessResultService` 按新组判定(结果脚本经上下文可见继承 + 重建实例的完整最终记录集),放行后生成新的下游待办;`complete()` 定位聚合组时跳过已取代组,避免继承实例同属新旧两组时命中旧组。

‎flow-engine-framework/src/main/java/com/codingapi/flow/exception/FlowStateException.java‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,19 @@ public static FlowStateException subProcessResetMergeNodeOnly() {
175175
"Sub process reset only allowed on the todo record of the locked merge node");
176176
}
177177

178+
/**
179+
* Sub process reset script result does not match the original group instance count
180+
*
181+
* @param expected original group instance count
182+
* @param actual create request count returned by the creation script
183+
* @return exception
184+
*/
185+
public static FlowStateException subProcessResetScriptMismatch(int expected, int actual) {
186+
return new FlowStateException("state.subProcess.resetScriptMismatch",
187+
String.format("Sub process reset creation script returned %d create requests, "
188+
+ "but the original group has %d instances", actual, expected));
189+
}
190+
178191
/**
179192
* Workflow script not found when executing a node or action
180193
*

‎flow-engine-framework/src/main/java/com/codingapi/flow/service/impl/FlowSubProcessResetService.java‎

Lines changed: 15 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import com.codingapi.flow.service.FlowRecordService;
2121
import com.codingapi.flow.service.FlowService;
2222
import com.codingapi.flow.service.WorkflowService;
23+
import com.codingapi.flow.session.FlowSession;
2324
import com.codingapi.flow.session.IRepositoryHolder;
2425
import com.codingapi.flow.strategy.node.SubProcessStrategy;
2526
import com.codingapi.flow.workflow.Workflow;
@@ -112,15 +113,27 @@ public void reset() {
112113
throw FlowStateException.subProcessResetMergeNodeOnly();
113114
}
114115

116+
// 重建与首次发起共用同一套创建脚本与数据组装过程:重新执行子流程创建脚本,
117+
// 按旧实例在聚合组中的创建顺序取对应请求,保证参数、发起人、发起人设定的人员
118+
// 与首次发起一致,且不会打乱多实例的创建顺序
119+
FlowSession parentSession = SubProcessSessionFactory.createParentSession(
120+
currentGroup, repositoryHolder, workflowService);
121+
List<FlowCreateRequest> scriptRequests = strategy.getSubProcessScript().execute(parentSession);
122+
List<SubProcessRecord.Instance> oldInstances = currentGroup.getInstances();
123+
if (scriptRequests.size() != oldInstances.size()) {
124+
throw FlowStateException.subProcessResetScriptMismatch(oldInstances.size(), scriptRequests.size());
125+
}
126+
115127
// 先创建重建实例(不触发自动提交),落库新聚合组后再统一提交,
116128
// 避免重建实例在新组落库前结束导致结果判定找不到聚合组
117129
boolean submit = strategy.isSubmit();
118130
FlowService flowService = repositoryHolder.createFlowService();
119131
List<SubProcessRecord.Instance> newInstances = new ArrayList<>();
120132
List<FlowActionRequest> submitRequests = new ArrayList<>();
121-
for (SubProcessRecord.Instance instance : currentGroup.getInstances()) {
133+
for (int index = 0; index < oldInstances.size(); index++) {
134+
SubProcessRecord.Instance instance = oldInstances.get(index);
122135
if (request.getResetInstanceProcessIds().contains(instance.getProcessId())) {
123-
FlowCreateRequest createRequest = buildRebuildRequest(instance, anchorRecord);
136+
FlowCreateRequest createRequest = scriptRequests.get(index);
124137
long newRecordId = flowService.create(createRequest);
125138
FlowRecord newStartRecord = repositoryHolder.getRecordById(newRecordId);
126139
newInstances.add(SubProcessRecord.Instance.rebuiltFrom(newStartRecord, instance.getProcessId()));
@@ -236,29 +249,6 @@ private FlowRecord findLockedMergeRecord(String processId, long anchorId) {
236249
.orElse(null);
237250
}
238251

239-
/**
240-
* 依据旧实例的启动记录反推重建请求:沿用原子流程定义、表单数据、流程标题与原发起人,
241-
* 锚定到原触发记录。
242-
*
243-
* @param oldInstance 被重置的旧实例
244-
* @param anchorRecord 父流程中子流程节点的触发记录
245-
* @return 重建子流程的创建请求
246-
*/
247-
private FlowCreateRequest buildRebuildRequest(SubProcessRecord.Instance oldInstance, FlowRecord anchorRecord) {
248-
FlowRecord oldStartRecord = repositoryHolder.getRecordById(oldInstance.getStartRecordId());
249-
if (oldStartRecord == null) {
250-
throw FlowNotFoundException.record(oldInstance.getStartRecordId());
251-
}
252-
FlowCreateRequest request = new FlowCreateRequest();
253-
request.setWorkCode(oldStartRecord.getWorkCode());
254-
request.setOperatorId(oldStartRecord.getCreateOperatorId());
255-
request.setActionId(oldStartRecord.getActionId());
256-
request.setFormData(oldStartRecord.getFormData());
257-
request.setWorkTitle(oldInstance.getWorkTitle());
258-
request.setParentRecordId(anchorRecord.getId());
259-
return request;
260-
}
261-
262252
/**
263253
* 判断指定记录当前是否可以执行子流程重置(供详情数据计算重置标识)。
264254
*

‎flow-engine-framework/src/main/java/com/codingapi/flow/service/impl/FlowSubProcessResultService.java‎

Lines changed: 2 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,21 @@
11
package com.codingapi.flow.service.impl;
22

33
import com.codingapi.flow.action.BaseAction;
4-
import com.codingapi.flow.action.IFlowAction;
5-
import com.codingapi.flow.action.actions.PassAction;
6-
import com.codingapi.flow.cache.FlowRuntimeScriptLocalCache;
74
import com.codingapi.flow.domain.SubProcessContext;
85
import com.codingapi.flow.domain.SubProcessRecord;
96
import com.codingapi.flow.error.ErrorThrow;
107
import com.codingapi.flow.event.FlowRecordDoneEvent;
118
import com.codingapi.flow.event.FlowRecordTodoEvent;
129
import com.codingapi.flow.event.IFlowEvent;
13-
import com.codingapi.flow.exception.FlowNotFoundException;
1410
import com.codingapi.flow.exception.FlowValidationException;
15-
import com.codingapi.flow.form.FormData;
1611
import com.codingapi.flow.node.BaseFlowNode;
1712
import com.codingapi.flow.node.IFlowNode;
1813
import com.codingapi.flow.node.nodes.SubProcessNode;
19-
import com.codingapi.flow.operator.IFlowOperator;
2014
import com.codingapi.flow.record.FlowRecord;
2115
import com.codingapi.flow.repository.SubProcessRepository;
22-
import com.codingapi.flow.service.WorkflowService;
23-
import com.codingapi.flow.session.FlowAdvice;
2416
import com.codingapi.flow.session.FlowSession;
2517
import com.codingapi.flow.session.IRepositoryHolder;
2618
import com.codingapi.flow.strategy.node.SubProcessStrategy;
27-
import com.codingapi.flow.workflow.Workflow;
28-
import com.codingapi.flow.workflow.runtime.WorkflowRuntime;
2919
import com.codingapi.springboot.framework.event.EventPusher;
3020

3121
import java.util.ArrayList;
@@ -39,13 +29,11 @@ public class FlowSubProcessResultService {
3929
private final FlowRecord childFinalRecord;
4030
private final IRepositoryHolder repositoryHolder;
4131
private final SubProcessRepository subProcessRepository;
42-
private final WorkflowService workflowService;
4332

4433
public FlowSubProcessResultService(FlowRecord childFinalRecord, IRepositoryHolder repositoryHolder) {
4534
this.childFinalRecord = childFinalRecord;
4635
this.repositoryHolder = repositoryHolder;
4736
this.subProcessRepository = repositoryHolder.getSubProcessRepository();
48-
this.workflowService = repositoryHolder.getWorkflowService();
4937
}
5038

5139
public void complete() {
@@ -90,42 +78,8 @@ public void complete() {
9078
}
9179

9280
private FlowSession createParentSession(SubProcessRecord subProcessRecord) {
93-
FlowRecord parentRecord = repositoryHolder.getRecordById(subProcessRecord.getParentRecordId());
94-
if (parentRecord == null) {
95-
throw FlowNotFoundException.record(subProcessRecord.getParentRecordId());
96-
}
97-
WorkflowRuntime workflowRuntime = workflowService.getWorkflowRuntime(subProcessRecord.getParentWorkRuntimeId());
98-
if (workflowRuntime == null) {
99-
throw FlowNotFoundException.workflow(subProcessRecord.getParentWorkRuntimeId() + " not found");
100-
}
101-
FlowRuntimeScriptLocalCache.getInstance().set(workflowRuntime.getScripts());
102-
Workflow workflow = workflowRuntime.toWorkflow();
103-
IFlowNode sourceNode = workflow.getFlowNode(parentRecord.getNodeId());
104-
IFlowAction action = sourceNode.actionManager().getActionById(parentRecord.getActionId());
105-
// 前驱记录可能为相同人员自动通过的留痕记录(无审批动作,actionId=null,issue #226):
106-
// 恢复下游以来源节点的通过动作兜底,避免空动作会话导致 NPE 使主流程永久停滞
107-
if (action == null && parentRecord.isAutoDone()) {
108-
action = sourceNode.actionManager().getAction(PassAction.class);
109-
}
110-
IFlowOperator currentOperator = repositoryHolder.getOperatorById(parentRecord.getCurrentOperatorId());
111-
IFlowOperator createdOperator = repositoryHolder.getOperatorById(parentRecord.getCreateOperatorId());
112-
IFlowOperator submitOperator = repositoryHolder.getOperatorById(parentRecord.getSubmitOperatorId());
113-
FormData formData = new FormData(workflow.getForm());
114-
formData.reset(parentRecord.getFormData());
115-
FlowAdvice advice = parentRecord.toAdvice(workflow);
116-
IFlowNode subProcessNode = workflow.getFlowNode(subProcessRecord.getNodeId());
117-
FlowSession session = new FlowSession(repositoryHolder,
118-
currentOperator,
119-
createdOperator,
120-
submitOperator,
121-
workflow,
122-
subProcessNode,
123-
action,
124-
formData,
125-
parentRecord,
126-
List.of(),
127-
parentRecord.getWorkRuntimeId(),
128-
advice);
81+
FlowSession session = SubProcessSessionFactory.createParentSession(
82+
subProcessRecord, repositoryHolder, repositoryHolder.getWorkflowService());
12983
session.setSubProcessContext(new SubProcessContext(subProcessRecord, childFinalRecord));
13084
return session;
13185
}
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
package com.codingapi.flow.service.impl;
2+
3+
import com.codingapi.flow.action.IFlowAction;
4+
import com.codingapi.flow.action.actions.PassAction;
5+
import com.codingapi.flow.cache.FlowRuntimeScriptLocalCache;
6+
import com.codingapi.flow.domain.SubProcessRecord;
7+
import com.codingapi.flow.exception.FlowNotFoundException;
8+
import com.codingapi.flow.form.FormData;
9+
import com.codingapi.flow.node.IFlowNode;
10+
import com.codingapi.flow.operator.IFlowOperator;
11+
import com.codingapi.flow.record.FlowRecord;
12+
import com.codingapi.flow.service.WorkflowService;
13+
import com.codingapi.flow.session.FlowAdvice;
14+
import com.codingapi.flow.session.FlowSession;
15+
import com.codingapi.flow.session.IRepositoryHolder;
16+
import com.codingapi.flow.workflow.Workflow;
17+
import com.codingapi.flow.workflow.runtime.WorkflowRuntime;
18+
19+
import java.util.List;
20+
21+
/**
22+
* 子流程父流程会话工厂。
23+
*
24+
* <p>以子流程聚合记录的锚点记录为来源重建父流程会话,供子流程结果判定脚本与
25+
* 子流程重置重建脚本复用,保证两处脚本执行时的父流程上下文、表单数据与动作信息一致。</p>
26+
*/
27+
public class SubProcessSessionFactory {
28+
29+
private SubProcessSessionFactory() {
30+
}
31+
32+
/**
33+
* 以子流程聚合记录的锚点记录重建父流程会话。
34+
*
35+
* <p>当前记录为锚点记录、当前节点为子流程节点,表单数据与审批意见均来自锚点记录,
36+
* 因而与首次触发子流程时的脚本上下文保持一致。</p>
37+
*
38+
* @param subProcessRecord 子流程聚合记录
39+
* @param repositoryHolder 仓储持有者
40+
* @param workflowService 流程服务
41+
* @return 父流程会话
42+
*/
43+
public static FlowSession createParentSession(SubProcessRecord subProcessRecord,
44+
IRepositoryHolder repositoryHolder,
45+
WorkflowService workflowService) {
46+
FlowRecord parentRecord = repositoryHolder.getRecordById(subProcessRecord.getParentRecordId());
47+
if (parentRecord == null) {
48+
throw FlowNotFoundException.record(subProcessRecord.getParentRecordId());
49+
}
50+
WorkflowRuntime workflowRuntime = workflowService.getWorkflowRuntime(subProcessRecord.getParentWorkRuntimeId());
51+
if (workflowRuntime == null) {
52+
throw FlowNotFoundException.workflow(subProcessRecord.getParentWorkRuntimeId() + " not found");
53+
}
54+
FlowRuntimeScriptLocalCache.getInstance().set(workflowRuntime.getScripts());
55+
Workflow workflow = workflowRuntime.toWorkflow();
56+
IFlowNode sourceNode = workflow.getFlowNode(parentRecord.getNodeId());
57+
IFlowAction action = sourceNode.actionManager().getActionById(parentRecord.getActionId());
58+
// 前驱记录可能为相同人员自动通过的留痕记录(无审批动作,actionId=null,issue #226):
59+
// 恢复下游以来源节点的通过动作兜底,避免空动作会话导致 NPE 使主流程永久停滞
60+
if (action == null && parentRecord.isAutoDone()) {
61+
action = sourceNode.actionManager().getAction(PassAction.class);
62+
}
63+
IFlowOperator currentOperator = repositoryHolder.getOperatorById(parentRecord.getCurrentOperatorId());
64+
IFlowOperator createdOperator = repositoryHolder.getOperatorById(parentRecord.getCreateOperatorId());
65+
IFlowOperator submitOperator = repositoryHolder.getOperatorById(parentRecord.getSubmitOperatorId());
66+
FormData formData = new FormData(workflow.getForm());
67+
formData.reset(parentRecord.getFormData());
68+
FlowAdvice advice = parentRecord.toAdvice(workflow);
69+
IFlowNode subProcessNode = workflow.getFlowNode(subProcessRecord.getNodeId());
70+
return new FlowSession(repositoryHolder,
71+
currentOperator,
72+
createdOperator,
73+
submitOperator,
74+
workflow,
75+
subProcessNode,
76+
action,
77+
formData,
78+
parentRecord,
79+
List.of(),
80+
parentRecord.getWorkRuntimeId(),
81+
advice);
82+
}
83+
}

0 commit comments

Comments
 (0)