@mldong/jeeflow 1.8.27 → 1.8.29

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/engine.d.ts CHANGED
@@ -94,7 +94,7 @@ export declare class EngineImpl implements Engine {
94
94
  /**
95
95
  * 退回上一步(血缘版,规范 04 · 退回上一步):上一步来源=当前行的 parentTaskId,
96
96
  * 复活那条历史行;不按模型入边拓扑推(拓扑版在分支/回环流会回到本实例没走过的节点,
97
- * 还会静默留下"实例 DOING 却零待办")。错码写在 msg 前缀(出口统一 99999999)。
97
+ * 还会静默留下"实例 DOING 却零待办")。对外 msg 用固定中文文案、不含引擎内部码(出口统一 99999999)。
98
98
  */
99
99
  private rollbackToParent;
100
100
  /**
package/dist/engine.js CHANGED
@@ -395,11 +395,11 @@ export class EngineImpl {
395
395
  /**
396
396
  * 退回上一步(血缘版,规范 04 · 退回上一步):上一步来源=当前行的 parentTaskId,
397
397
  * 复活那条历史行;不按模型入边拓扑推(拓扑版在分支/回环流会回到本实例没走过的节点,
398
- * 还会静默留下"实例 DOING 却零待办")。错码写在 msg 前缀(出口统一 99999999)。
398
+ * 还会静默留下"实例 DOING 却零待办")。对外 msg 用固定中文文案、不含引擎内部码(出口统一 99999999)。
399
399
  */
400
400
  async rollbackToParent(flow, inst, task, operator) {
401
- const NO_LINEAGE = '20010007: 上一步任务ID为空,无法驳回至上一步处理';
402
- const GUARD = '20010008: 无法驳回至上一步处理,请确认上一步骤并非fork、join、suprocess以及会签任务';
401
+ const NO_LINEAGE = '上一步任务ID为空,无法驳回至上一步处理';
402
+ const GUARD = '无法驳回至上一步处理,请确认上一步骤并非fork、join、suprocess以及会签任务';
403
403
  const parentId = task.parentTaskId;
404
404
  if (!parentId || parentId === '0')
405
405
  throw new Error(NO_LINEAGE);
package/dist/facade.js CHANGED
@@ -828,6 +828,9 @@ export class JeeflowFacade {
828
828
  return his.map(t => ({
829
829
  taskName: t.taskName, displayName: t.displayName, taskType: t.taskType ?? null,
830
830
  performType: t.performType ?? null, taskState: t.taskState, operator: t.actorId ?? '',
831
+ // issues/122:可空时间列空值出 '' 不出 null——mldong-nestjs 全局 SmartNullStripping
832
+ // 拦截器会把 null 值整键剥掉(本出口是对象字面量,拿不到 keepNull:fields 元数据),
833
+ // 出 '' 才能让键恒在,形状也与 boot2 的 record 行一致。
831
834
  finishTime: fmtTime(t.finishTime), variable: t.variables ?? {},
832
835
  ext: t.variables ?? {}, // issues/15:前端读 ext.tf_approvalComment
833
836
  }));
@@ -1142,11 +1145,11 @@ export class JeeflowFacade {
1142
1145
  id: t.id, processInstanceId: t.processInstanceId, taskName: t.taskName,
1143
1146
  displayName: t.displayName, taskType: t.taskType ?? null,
1144
1147
  performType: t.performType ?? null, taskState: t.taskState,
1145
- operator: t.actorId ?? '', finishTime: t.finishTime,
1146
- expireTime: t.expireTime, formKey: t.formKey ?? '', taskParentId: t.parentTaskId ?? null,
1148
+ operator: t.actorId ?? '', finishTime: fmtTime(t.finishTime),
1149
+ expireTime: fmtTime(t.expireTime), formKey: t.formKey ?? '', taskParentId: t.parentTaskId ?? null,
1147
1150
  variable: JSON.stringify(t.variables ?? {}),
1148
- createTime: t.createTime, createUser: t.createUser,
1149
- updateTime: t.updateTime, updateUser: t.updateUser,
1151
+ createTime: fmtTime(t.createTime), createUser: t.createUser,
1152
+ updateTime: fmtTime(t.updateTime), updateUser: t.updateUser,
1150
1153
  taskActorIdList: t.actorIds ?? [],
1151
1154
  taskFormData: formDataOf(t.variables, 'tf_'), // issues/15
1152
1155
  };
@@ -1167,7 +1170,7 @@ export class JeeflowFacade {
1167
1170
  businessNo: inst.businessNo, operator: inst.operator,
1168
1171
  variables: inst.variables,
1169
1172
  formData: formDataOf(inst.variables, 'f_'), // issues/15
1170
- createTime: inst.createTime, createUser: inst.createUser,
1173
+ createTime: fmtTime(inst.createTime), createUser: inst.createUser,
1171
1174
  jsonObject: graph, // issues/05
1172
1175
  tasks,
1173
1176
  activeTaskList,
@@ -1416,10 +1419,16 @@ function formDataOf(vars, prefix) {
1416
1419
  }
1417
1420
  return out;
1418
1421
  }
1419
- /** Date → 'yyyy-MM-dd HH:mm:ss'(null/undefined → null) */
1422
+ /**
1423
+ * Date → 'yyyy-MM-dd HH:mm:ss';**空值出 ''、不出 null**。
1424
+ *
1425
+ * 出 null 会被下游 `mldong-nestjs` 的全局剥-null 拦截器(SmartNullStripping)连键一起吞掉,
1426
+ * 13 栈里就只有 nestjs 少这些列(issues/122 的机理)。规范 06 §2.4 的时间列是"必出键、空即空串",
1427
+ * 所以归一放在这一层:所有行投影共用它,逐个补 `?? ''` 迟早漏一处。
1428
+ */
1420
1429
  function fmtTime(v) {
1421
1430
  if (v == null)
1422
- return null;
1431
+ return '';
1423
1432
  const p = (n) => String(n).padStart(2, '0');
1424
1433
  return `${v.getFullYear()}-${p(v.getMonth() + 1)}-${p(v.getDate())} ${p(v.getHours())}:${p(v.getMinutes())}:${p(v.getSeconds())}`;
1425
1434
  }
@@ -26,15 +26,24 @@ export declare class JdbcProcessExtRepository implements ProcessExtRepository {
26
26
  removeSurrogate(id: string): Promise<void>;
27
27
  pageSurrogates(pageNum?: number, pageSize?: number, filters?: Record<string, any>, conditions?: QueryCondition[]): Promise<[ProcessSurrogate[], number]>;
28
28
  /**
29
- * 生效委托查询(issues/116 判据 a~d 基准实现):
30
- * a. processName 先精确命中,未命中再兜底 process_name IS NULL OR = '' (全流程委托)
31
- * b. 时间窗 start_time <= at <= end_time,任一侧 NULL = 该侧不限
32
- * c. 自委托过滤 surrogate <> operator
33
- * d. enabled 只认 1(INT 列,脏值进不来;NULL 也不生效)
34
- * 多条命中取 id 最大(最新)一条 —— 内存仓须给出同结论(MemoryExtRepository.getSurrogate)
29
+ * 生效委托查询(issues/116 判据 a~d 基准实现 + issues/123 的取行/裁决顺序):
30
+ * a. 作用域:先在该流程名作用域内取记录,该作用域**一条都没有**才兜底
31
+ * `process_name IS NULL OR = ''`(全流程委托);精确作用域里只要有记录就由它裁决;
32
+ * 然后由 `surrogateIsEffective` 裁决**这一条**:
33
+ * d. enabled 严格 1('1'/1 同结论,NULL 不生效)、c. 自委托过滤、
34
+ * b. 时间窗 start<=at<=end(任一侧 NULL = 该侧不限;`at` 传 null 则整段跳过窗比较)。
35
+ *
36
+ * ⚠️ 旧形状是 `WHERE operator=? AND enabled=1 AND surrogate<>? AND 窗口… ORDER BY id DESC LIMIT 1`
37
+ * ——先滤生效再取最新,等于"历史上留过一条窗内且 enabled=1 的记录就永久生效",用户随后
38
+ * 新建的窗外 / enabled=0 / 脏值 / 自委托记录全都判不动它(issues/123,13 栈 L2-17/L2-18 全红)。
39
+ * 现在 WHERE 里**不带**任何生效判据,只 `ORDER BY id DESC LIMIT 1` 取最新一条;
40
+ * 最新一条不生效 ⇒ 同层内不回落更旧那条,转看全流程作用域的最新一条;两层都判否才返回 null。
41
+ * 内存仓须给出同结论(MemoryExtRepository.getSurrogate,共用 src/surrogate-rule.ts)。
35
42
  */
36
43
  getSurrogate(operator: string, processName: string, at?: Date): Promise<ProcessSurrogate | null>;
37
- private querySurrogate;
44
+ /** 该授权人在指定流程作用域内**最新的一条**委托:只排序取首行,不滤任何生效判据。
45
+ * processName 为 '' = 全流程委托作用域(process_name IS NULL OR = '')。 */
46
+ private queryNewestSurrogate;
38
47
  private mapDesign;
39
48
  private mapSurrogate;
40
49
  }
package/dist/jdbc/ext.js CHANGED
@@ -3,6 +3,7 @@
3
3
  // 与 shared.ts 同一套 SqlAdapter / 占位符约定;分页为单表简单过滤(filters 字段名 EQ)。
4
4
  import { AsyncLocalStorage } from 'node:async_hooks';
5
5
  import { TsIDGenerator, rowId } from './shared.js';
6
+ import { surrogateIsEffective, surrogateTimeMs } from '../surrogate-rule.js';
6
7
  const txStore = new AsyncLocalStorage();
7
8
  export class JdbcProcessExtRepository {
8
9
  adapter;
@@ -267,25 +268,35 @@ export class JdbcProcessExtRepository {
267
268
  }
268
269
  }
269
270
  /**
270
- * 生效委托查询(issues/116 判据 a~d 基准实现):
271
- * a. processName 先精确命中,未命中再兜底 process_name IS NULL OR = '' (全流程委托)
272
- * b. 时间窗 start_time <= at <= end_time,任一侧 NULL = 该侧不限
273
- * c. 自委托过滤 surrogate <> operator
274
- * d. enabled 只认 1(INT 列,脏值进不来;NULL 也不生效)
275
- * 多条命中取 id 最大(最新)一条 —— 内存仓须给出同结论(MemoryExtRepository.getSurrogate)
271
+ * 生效委托查询(issues/116 判据 a~d 基准实现 + issues/123 的取行/裁决顺序):
272
+ * a. 作用域:先在该流程名作用域内取记录,该作用域**一条都没有**才兜底
273
+ * `process_name IS NULL OR = ''`(全流程委托);精确作用域里只要有记录就由它裁决;
274
+ * 然后由 `surrogateIsEffective` 裁决**这一条**:
275
+ * d. enabled 严格 1('1'/1 同结论,NULL 不生效)、c. 自委托过滤、
276
+ * b. 时间窗 start<=at<=end(任一侧 NULL = 该侧不限;`at` 传 null 则整段跳过窗比较)。
277
+ *
278
+ * ⚠️ 旧形状是 `WHERE operator=? AND enabled=1 AND surrogate<>? AND 窗口… ORDER BY id DESC LIMIT 1`
279
+ * ——先滤生效再取最新,等于"历史上留过一条窗内且 enabled=1 的记录就永久生效",用户随后
280
+ * 新建的窗外 / enabled=0 / 脏值 / 自委托记录全都判不动它(issues/123,13 栈 L2-17/L2-18 全红)。
281
+ * 现在 WHERE 里**不带**任何生效判据,只 `ORDER BY id DESC LIMIT 1` 取最新一条;
282
+ * 最新一条不生效 ⇒ 同层内不回落更旧那条,转看全流程作用域的最新一条;两层都判否才返回 null。
283
+ * 内存仓须给出同结论(MemoryExtRepository.getSurrogate,共用 src/surrogate-rule.ts)。
276
284
  */
277
285
  async getSurrogate(operator, processName, at = new Date()) {
278
286
  const name = processName ?? '';
279
- if (name) {
280
- const hit = await this.querySurrogate(operator, name, at);
281
- if (hit)
282
- return hit;
287
+ const ms = surrogateTimeMs(at);
288
+ let newest = await this.queryNewestSurrogate(operator, name);
289
+ if (!surrogateIsEffective(newest, operator, ms) && name) {
290
+ // 精确作用域判否(含池空)⇒ 仍要看全流程作用域的最新一条(条款 1.4 后半句)
291
+ newest = await this.queryNewestSurrogate(operator, '');
283
292
  }
284
- return this.querySurrogate(operator, '', at);
293
+ return surrogateIsEffective(newest, operator, ms) ? newest : null;
285
294
  }
286
- async querySurrogate(operator, processName, at) {
287
- let sql = `SELECT ${JdbcProcessExtRepository.SURROGATE_COLS} FROM wf_process_surrogate WHERE operator = ? AND enabled = 1 AND surrogate <> ?`;
288
- const args = [operator, operator];
295
+ /** 该授权人在指定流程作用域内**最新的一条**委托:只排序取首行,不滤任何生效判据。
296
+ * processName 为 '' = 全流程委托作用域(process_name IS NULL OR = '')。 */
297
+ async queryNewestSurrogate(operator, processName) {
298
+ let sql = `SELECT ${JdbcProcessExtRepository.SURROGATE_COLS} FROM wf_process_surrogate WHERE operator = ?`;
299
+ const args = [operator];
289
300
  if (!processName) {
290
301
  sql += " AND (process_name IS NULL OR process_name = '')";
291
302
  }
@@ -293,10 +304,6 @@ export class JdbcProcessExtRepository {
293
304
  sql += ' AND process_name = ?';
294
305
  args.push(processName);
295
306
  }
296
- if (at) {
297
- sql += ' AND (start_time IS NULL OR start_time <= ?) AND (end_time IS NULL OR end_time >= ?)';
298
- args.push(at, at);
299
- }
300
307
  sql += ' ORDER BY id DESC LIMIT 1';
301
308
  const conn = await this.c();
302
309
  try {
@@ -18,14 +18,21 @@ export declare class MemoryExtRepository implements ProcessExtRepository {
18
18
  removeSurrogate(id: string): Promise<void>;
19
19
  pageSurrogates(_pageNum?: number, _pageSize?: number, filters?: Record<string, any>, conditions?: QueryCondition[]): Promise<[ProcessSurrogate[], number]>;
20
20
  /**
21
- * 生效委托查询(issues/116 判据 a~d,与 JdbcProcessExtRepository.getSurrogate 同口径,
22
- * 同一份数据两仓必须给出同一结论):
23
- * a. processName 先精确命中,未命中再兜底空 process_name(全流程委托)
24
- * b. 时间窗 start_time <= at <= end_time,任一侧为空 = 该侧不限
25
- * c. 自委托过滤:surrogate <> operator
26
- * d. enabled 只认整数 1,脏值不当启用
27
- * 多条命中取**主键 id 最大**(判据 1.4,对齐 SQL 侧 `ORDER BY id DESC LIMIT 1`;
28
- * 显式比 id,不依赖 Map 插入序,见 surrogateIdLess)
21
+ * 生效委托查询(issues/116 判据 a~d + issues/123 的取行/裁决顺序,
22
+ * 与 JdbcProcessExtRepository.getSurrogate 同口径,同一份数据两仓必须给出同一结论):
23
+ * a. 作用域:先在该流程名作用域内取记录,该作用域**一条都没有**才兜底 process_name 空(全流程委托);
24
+ * 精确作用域里只要有记录就由它裁决,**不跨作用域回落**;
25
+ * b. 时间窗 start_time <= at <= end_time,任一侧为空 = 该侧不限;`at` 解析不出时刻则整段跳过窗比较
26
+ * (保留 `at: Date = new Date()` 显式传 null 的老语义);
27
+ * c. 自委托过滤 surrogate <> operator;d. enabled 只认整数 1。
28
+ *
29
+ * ⚠️ 顺序是「**先**按主键 id 取最新一条 → **再**由四判据裁决这一条」(条款 1.4 + issues/123):
30
+ * 反过来(先滤生效、剩下的才取最新)等于"历史上留过一条窗内委托就永久生效",
31
+ * 用户随后新建的窗外/停用/脏值/自委托记录全都判不动它。最新一条不生效 ⇒ 返回 null,
32
+ * **不回落**到更旧那条。id 显式按数值比(surrogateIdLess),不依赖 Map 插入序。
29
33
  */
30
34
  getSurrogate(operator: string, processName: string, at?: Date): Promise<ProcessSurrogate | null>;
35
+ /** 该授权人在指定流程作用域内 id 最大(最新)的一条委托;processName 为 '' = 全流程委托作用域。
36
+ * **不带**任何生效判据过滤(裁决在 getSurrogate 里对这一条单独做)。 */
37
+ private newestSurrogateInScope;
31
38
  }
@@ -1,5 +1,6 @@
1
1
  // 扩展仓储内存实现(v1.1.0,测试/演示用)
2
2
  import { matchConditions } from './memory.js';
3
+ import { surrogateIsEffective, surrogateTimeMs, newestOf } from './surrogate-rule.js';
3
4
  // ═══ 条件匹配基建(issues/05-5) ═══
4
5
  const DESIGN_FIELDS = {
5
6
  't.id': 'id', 't.name': 'name', 't.display_name': 'displayName', 't.type': 'type',
@@ -19,47 +20,7 @@ function pickFields(row, map) {
19
20
  }
20
21
  return fields;
21
22
  }
22
- // ── 委托查询判据基建(issues/116 判据 b/d,与 SQL 仓同口径)─────────────────
23
- /** 时间归一为毫秒:Date/毫秒数/可解析字符串均可;空值或脏值 → null(该侧不限,对齐 SQL 侧 NULL) */
24
- function surrogateTimeMs(v) {
25
- if (v == null || v === '')
26
- return null;
27
- if (v instanceof Date)
28
- return Number.isNaN(v.getTime()) ? null : v.getTime();
29
- if (typeof v === 'number')
30
- return Number.isNaN(v) ? null : v;
31
- if (typeof v === 'string') {
32
- // 兼容 'yyyy-MM-dd HH:mm:ss'(Safari/Node 对空格分隔不统一,转 T 再解)
33
- const n = Date.parse(v.trim().replace(' ', 'T'));
34
- return Number.isNaN(n) ? null : n;
35
- }
36
- return null;
37
- }
38
- /** enabled 只认整数 1('1' 视作 1,与 SQL 侧 INT 列同结论);脏值('abc'/true/0/2/空)一律不启用 */
39
- function surrogateEnabled(v) {
40
- if (typeof v === 'number')
41
- return v === 1;
42
- if (typeof v === 'string') {
43
- const s = v.trim();
44
- return s !== '' && Number.isInteger(Number(s)) && Number(s) === 1;
45
- }
46
- return false;
47
- }
48
- /**
49
- * 判据 1.4:比较两条委托的主键大小(a 是否比 b 旧)。id 是雪花数字串(19 位,已超
50
- * Number 安全整数),纯数字串一律按 BigInt 比**数值**(与 SQL 侧 `ORDER BY id DESC` 同序),
51
- * 非数字 id 退化为字符串序。
52
- *
53
- * ⚠️ 不能拿 Map 的插入序当 id 序:saveSurrogate/updateSurrogate 都接受调用方显式 id,
54
- * 乱序写入(补录、导入、双仓对拍)时"取遍历到的末条"就会与 SQL 仓给出不同答案。
55
- * Go 内存仓原实现正是"map 随机序取到啥算啥"被抓出来重写的(issues/116 §5)。
56
- */
57
- function surrogateIdLess(a, b) {
58
- const sa = String(a ?? ''), sb = String(b ?? '');
59
- if (/^\d+$/.test(sa) && /^\d+$/.test(sb))
60
- return BigInt(sa) < BigInt(sb);
61
- return sa < sb;
62
- }
23
+ // ── 委托查询判据基建:单一事实源在 ./surrogate-rule.ts(与 SQL 仓共用,issues/116/123)──
63
24
  export class MemoryExtRepository {
64
25
  designs = new Map();
65
26
  designHis = new Map();
@@ -136,47 +97,34 @@ export class MemoryExtRepository {
136
97
  return [rows, rows.length];
137
98
  }
138
99
  /**
139
- * 生效委托查询(issues/116 判据 a~d,与 JdbcProcessExtRepository.getSurrogate 同口径,
140
- * 同一份数据两仓必须给出同一结论):
141
- * a. processName 先精确命中,未命中再兜底空 process_name(全流程委托)
142
- * b. 时间窗 start_time <= at <= end_time,任一侧为空 = 该侧不限
143
- * c. 自委托过滤:surrogate <> operator
144
- * d. enabled 只认整数 1,脏值不当启用
145
- * 多条命中取**主键 id 最大**(判据 1.4,对齐 SQL 侧 `ORDER BY id DESC LIMIT 1`;
146
- * 显式比 id,不依赖 Map 插入序,见 surrogateIdLess)
100
+ * 生效委托查询(issues/116 判据 a~d + issues/123 的取行/裁决顺序,
101
+ * 与 JdbcProcessExtRepository.getSurrogate 同口径,同一份数据两仓必须给出同一结论):
102
+ * a. 作用域:先在该流程名作用域内取记录,该作用域**一条都没有**才兜底 process_name 空(全流程委托);
103
+ * 精确作用域里只要有记录就由它裁决,**不跨作用域回落**;
104
+ * b. 时间窗 start_time <= at <= end_time,任一侧为空 = 该侧不限;`at` 解析不出时刻则整段跳过窗比较
105
+ * (保留 `at: Date = new Date()` 显式传 null 的老语义);
106
+ * c. 自委托过滤 surrogate <> operator;d. enabled 只认整数 1。
107
+ *
108
+ * ⚠️ 顺序是「**先**按主键 id 取最新一条 → **再**由四判据裁决这一条」(条款 1.4 + issues/123):
109
+ * 反过来(先滤生效、剩下的才取最新)等于"历史上留过一条窗内委托就永久生效",
110
+ * 用户随后新建的窗外/停用/脏值/自委托记录全都判不动它。最新一条不生效 ⇒ 返回 null,
111
+ * **不回落**到更旧那条。id 显式按数值比(surrogateIdLess),不依赖 Map 插入序。
147
112
  */
148
113
  async getSurrogate(operator, processName, at = new Date()) {
149
114
  const name = processName ?? '';
150
- const now = surrogateTimeMs(at);
151
- let exact = null;
152
- let global = null;
153
- for (const s of this.surrogates.values()) {
154
- if (s.operator !== operator)
155
- continue;
156
- if (s.surrogate === operator)
157
- continue; // c 自委托过滤
158
- if (!surrogateEnabled(s.enabled))
159
- continue; // d enabled 只认 1
160
- if (now != null) { // b 时间窗(空 = 不限)
161
- const start = surrogateTimeMs(s.startTime);
162
- const end = surrogateTimeMs(s.endTime);
163
- if (start != null && start > now)
164
- continue;
165
- if (end != null && end < now)
166
- continue;
167
- }
168
- const bucket = (name && s.processName === name) ? 'exact' : (!s.processName ? 'global' : '');
169
- if (!bucket)
170
- continue; // a 异流程名的行既非精确也非兜底
171
- const cur = bucket === 'exact' ? exact : global;
172
- if (cur == null || surrogateIdLess(cur.id, s.id)) {
173
- if (bucket === 'exact')
174
- exact = s;
175
- else
176
- global = s;
177
- }
115
+ const ms = surrogateTimeMs(at);
116
+ let hit = this.newestSurrogateInScope(operator, name);
117
+ if (surrogateIsEffective(hit, operator, ms) || !name) {
118
+ return surrogateIsEffective(hit, operator, ms) && hit ? { ...hit } : null;
178
119
  }
179
- const hit = exact ?? global; // a 精确优先,其次全流程兜底
180
- return hit ? { ...hit } : null;
120
+ // 精确作用域判否(含池空)⇒ 仍要看全流程作用域的最新一条(条款 1.4 后半句)
121
+ hit = this.newestSurrogateInScope(operator, '');
122
+ return surrogateIsEffective(hit, operator, ms) && hit ? { ...hit } : null;
123
+ }
124
+ /** 该授权人在指定流程作用域内 id 最大(最新)的一条委托;processName 为 '' = 全流程委托作用域。
125
+ * **不带**任何生效判据过滤(裁决在 getSurrogate 里对这一条单独做)。 */
126
+ newestSurrogateInScope(operator, processName) {
127
+ const inScope = [...this.surrogates.values()].filter(s => s.operator === operator && (processName === '' ? !s.processName : s.processName === processName));
128
+ return newestOf(inScope);
181
129
  }
182
130
  }
@@ -0,0 +1,34 @@
1
+ import type { ProcessSurrogate } from './model.js';
2
+ /** 时间归一为毫秒:Date/毫秒数/可解析字符串均可;空值或脏值 → null(该侧不限,对齐 SQL 侧 NULL) */
3
+ export declare function surrogateTimeMs(v: any): number | null;
4
+ /** enabled 只认整数 1('1' 视作 1,与 SQL 侧 INT 列同结论);脏值('abc'/true/0/2/空)一律不启用 */
5
+ export declare function surrogateEnabled(v: any): boolean;
6
+ /**
7
+ * 判据 1.4:比较两条委托的主键大小(a 是否比 b 旧)。id 是雪花数字串(19 位,已超
8
+ * Number 安全整数),纯数字串一律按 BigInt 比**数值**(与 SQL 侧 `ORDER BY id DESC` 同序),
9
+ * 非数字 id 退化为字符串序。
10
+ *
11
+ * ⚠️ 不能拿 Map 的插入序当 id 序:saveSurrogate/updateSurrogate 都接受调用方显式 id,
12
+ * 乱序写入(补录、导入、双仓对拍)时"取遍历到的末条"就会与 SQL 仓给出不同答案。
13
+ * Go 内存仓原实现正是"map 随机序取到啥算啥"被抓出来重写的(issues/116 §5)。
14
+ */
15
+ export declare function surrogateIdLess(a: any, b: any): boolean;
16
+ /**
17
+ * 单条裁决(规范 06 §4.5 条款 5 四判据 + issues/123):这一行委托此刻对该授权人生效吗。
18
+ *
19
+ * 调用方**必须**先按 id 选出「该授权人在该流程作用域内的最新一条」再问本函数——
20
+ * 本函数只裁决单条,不做多条择优(择优规则见文件头的 ①②③)。
21
+ *
22
+ * @param s 待裁决的委托行(null = 该作用域内没有记录)
23
+ * @param operator 授权人(判自委托:被委托人等于授权人 ⇒ 不新增、不重复)
24
+ * @param atMs 判定时刻(毫秒);传 null 表示不做窗口比较
25
+ * (保留 `getSurrogate(..., at)` 显式传 null 时整段跳过窗比较的入参语义)
26
+ */
27
+ export declare function surrogateIsEffective(s: ProcessSurrogate | null | undefined, operator: string, atMs: number | null): boolean;
28
+ /**
29
+ * 判据 1.4 的取行:候选里主键 id 最大(最新)的那一条,**不带**任何生效判据过滤。
30
+ * 空池返回 null。SQL 侧对应 `ORDER BY id DESC LIMIT 1`。
31
+ */
32
+ export declare function newestOf<T extends {
33
+ id: any;
34
+ }>(rows: Iterable<T>): T | null;
@@ -0,0 +1,97 @@
1
+ // 委托查询判据的**单一事实源**(issues/116 判据 a~d + issues/123 的取行/裁决顺序)。
2
+ //
3
+ // 契约依据:jeeflow-doc spec `06-facade.md` §4.5 条款 1.4/5/6 + `08-compliance.md` 用例 27。
4
+ // 条款 6 要求**内存仓与 SQL 仓两条路径都要满足判据**,同栈两仓给出不同结论即缺陷,
5
+ // 故两侧共用本文件的同一份谓词(姿势对齐 PHP 的 `SurrogateRule`、Python 的 `surrogate.py`)。
6
+ //
7
+ // ⚠️ issues/123:取行与裁决的**顺序不可颠倒**——
8
+ // ① 先在流程作用域内按主键 id 取**最新一条**({@link newestOf} / SQL `ORDER BY id DESC LIMIT 1`,
9
+ // 不带任何生效判据过滤);
10
+ // ② 再由 {@link surrogateIsEffective} 裁决**这一条**;
11
+ // ③ 同层内不生效即"未命中",不回落到更旧那条;但精确作用域判否(含池空)后仍要看
12
+ // 全流程作用域(processName 为空)的最新一条——条款 1.4 后半句。Java 参考实现的既有
13
+ // 测试 JdbcProcessExtRepositoryTest#testSurrogateCrudAndGet 钉的是「精确已过期 → 兜底全流程」。
14
+ // 反过来写(先用 ② 的判据把记录滤掉、剩下的才取最新)等价于"同一授权人历史上留过一条
15
+ // 窗内 enabled=1 的记录就永久生效",用户随后新建的窗外 / enabled=0 / 脏值 / 自委托记录
16
+ // 全都判不动它——这正是 issues/123 里 13 栈 L2-17/L2-18 全红的病灶。
17
+ /** 时间归一为毫秒:Date/毫秒数/可解析字符串均可;空值或脏值 → null(该侧不限,对齐 SQL 侧 NULL) */
18
+ export function surrogateTimeMs(v) {
19
+ if (v == null || v === '')
20
+ return null;
21
+ if (v instanceof Date)
22
+ return Number.isNaN(v.getTime()) ? null : v.getTime();
23
+ if (typeof v === 'number')
24
+ return Number.isNaN(v) ? null : v;
25
+ if (typeof v === 'string') {
26
+ // 兼容 'yyyy-MM-dd HH:mm:ss'(Safari/Node 对空格分隔不统一,转 T 再解)
27
+ const n = Date.parse(v.trim().replace(' ', 'T'));
28
+ return Number.isNaN(n) ? null : n;
29
+ }
30
+ return null;
31
+ }
32
+ /** enabled 只认整数 1('1' 视作 1,与 SQL 侧 INT 列同结论);脏值('abc'/true/0/2/空)一律不启用 */
33
+ export function surrogateEnabled(v) {
34
+ if (typeof v === 'number')
35
+ return v === 1;
36
+ if (typeof v === 'string') {
37
+ const s = v.trim();
38
+ return s !== '' && Number.isInteger(Number(s)) && Number(s) === 1;
39
+ }
40
+ return false;
41
+ }
42
+ /**
43
+ * 判据 1.4:比较两条委托的主键大小(a 是否比 b 旧)。id 是雪花数字串(19 位,已超
44
+ * Number 安全整数),纯数字串一律按 BigInt 比**数值**(与 SQL 侧 `ORDER BY id DESC` 同序),
45
+ * 非数字 id 退化为字符串序。
46
+ *
47
+ * ⚠️ 不能拿 Map 的插入序当 id 序:saveSurrogate/updateSurrogate 都接受调用方显式 id,
48
+ * 乱序写入(补录、导入、双仓对拍)时"取遍历到的末条"就会与 SQL 仓给出不同答案。
49
+ * Go 内存仓原实现正是"map 随机序取到啥算啥"被抓出来重写的(issues/116 §5)。
50
+ */
51
+ export function surrogateIdLess(a, b) {
52
+ const sa = String(a ?? ''), sb = String(b ?? '');
53
+ if (/^\d+$/.test(sa) && /^\d+$/.test(sb))
54
+ return BigInt(sa) < BigInt(sb);
55
+ return sa < sb;
56
+ }
57
+ /**
58
+ * 单条裁决(规范 06 §4.5 条款 5 四判据 + issues/123):这一行委托此刻对该授权人生效吗。
59
+ *
60
+ * 调用方**必须**先按 id 选出「该授权人在该流程作用域内的最新一条」再问本函数——
61
+ * 本函数只裁决单条,不做多条择优(择优规则见文件头的 ①②③)。
62
+ *
63
+ * @param s 待裁决的委托行(null = 该作用域内没有记录)
64
+ * @param operator 授权人(判自委托:被委托人等于授权人 ⇒ 不新增、不重复)
65
+ * @param atMs 判定时刻(毫秒);传 null 表示不做窗口比较
66
+ * (保留 `getSurrogate(..., at)` 显式传 null 时整段跳过窗比较的入参语义)
67
+ */
68
+ export function surrogateIsEffective(s, operator, atMs) {
69
+ if (!s)
70
+ return false;
71
+ if (!surrogateEnabled(s.enabled))
72
+ return false; // d 只认整数 1
73
+ const agent = typeof s.surrogate === 'string' ? s.surrogate.trim() : String(s.surrogate ?? '');
74
+ if (agent === '' || agent === operator)
75
+ return false; // c 自委托过滤(含代理人为空)
76
+ if (atMs == null)
77
+ return true; // 调用方没给时刻 ⇒ 不比较窗口
78
+ const start = surrogateTimeMs(s.startTime);
79
+ const end = surrogateTimeMs(s.endTime);
80
+ if (start != null && start > atMs)
81
+ return false; // b 一侧为空 = 该侧不限
82
+ if (end != null && end < atMs)
83
+ return false;
84
+ return true;
85
+ }
86
+ /**
87
+ * 判据 1.4 的取行:候选里主键 id 最大(最新)的那一条,**不带**任何生效判据过滤。
88
+ * 空池返回 null。SQL 侧对应 `ORDER BY id DESC LIMIT 1`。
89
+ */
90
+ export function newestOf(rows) {
91
+ let hit = null;
92
+ for (const r of rows) {
93
+ if (hit == null || surrogateIdLess(hit.id, r.id))
94
+ hit = r;
95
+ }
96
+ return hit;
97
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mldong/jeeflow",
3
- "version": "1.8.27",
3
+ "version": "1.8.29",
4
4
  "description": "jeeflow workflow engine — Node.js / TypeScript implementation",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",