M1: 底座 —— @contracts 首版 + event / ledger / org / llm

@contracts 首版(§8.1 九个文件):
- id 品牌 ID / terminal 终态五名与归侧四标签 / envelope 信封六字段与四类事件
- card 工单两模式 + 对话卡族(形状对齐 2026-07-26 原型定稿)
- contract7 契约七件 / ledger 三级寻址与写三分 / process-package 流程包 DSL 与触发器三类
- manifest 活动包清单六区(含 CMMN·DMN·步序声明的资产形状)/ org / api 路由契约

模块(L1):
- event   信封存储、归属解析三级短路、投递五诺、监听注册表、三级消费裁定
- ledger  schema 自生长(最大化合并/最宽类型/只增不破坏)、任务视图叠加、写三分、冲突检查
- org     岗位授权(客体三类×动作三种)、停用前置检查、会话、种子播种
- llm     四元绑定、ModelGateway 三模式(live/record/replay)—— 两缝之一

app 装配:Hono 服务 + 鉴权中间件 + 治理台/工作台路由;drizzle 迁移;AES-256-GCM 凭证密封

ADR-007 工具链与门禁 · ADR-008 投递队列 · ADR-009 凭证加密
(ADR 编号自 007 续:编码规范 §14 已用到 006,README 写「从 005 续」是笔误)

门禁五道全绿;96 个测试通过,含对真实 PG 的 ledger 集成测试

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01KeDBx2PHLavbURXB8LJJ7m
This commit is contained in:
JCAI Main Agent
2026-07-27 00:59:14 +08:00
co-authored by Claude Opus 5
parent 7cd664af3a
commit 14d4fa046a
120 changed files with 15236 additions and 78 deletions
+279
View File
@@ -0,0 +1,279 @@
/**
* 流程包 DSL 与触发器配置(编码规范 §8.1;语义出处 OS 03)。
*
* 流程包 = 调用接口 + 两种活动(声明)+ 审批点 + 两种网关;工作流 = 其运行时实例。
* 触发器与流程包**平级**:不入流程包结构、不随流程包版本,配置独立存放。
*
* 编排铁律:**连线即映射,画图即校验,不闭合不得发布** —— 存在未完成边、未满足必填
* 或类型冲突的流程包可存草稿、不得发布。校验算法在 `process` 的 domain,本文件只定形状。
*/
import { z } from 'zod';
import {
activityIdSchema,
nodeIdSchema,
processPackageIdSchema,
staffIdSchema,
triggerIdSchema,
} from './id.ts';
import { jsonSchemaSchema } from './contract7.ts';
/* ── 负责人四策略(OS 03) ──────────────────────────────────────────── */
export const OWNER_STRATEGIES = ['assigned', 'sole', 'rotation', 'follow-source'] as const;
export const ownerStrategySchema = z.enum(OWNER_STRATEGIES);
export type OwnerStrategy = z.infer<typeof ownerStrategySchema>;
export const OWNER_STRATEGY_LABEL: Readonly<Record<OwnerStrategy, string>> = {
assigned: '指定人',
sole: '岗位唯一人',
rotation: '轮值',
'follow-source': '来源跟随',
};
export const ownerSpecSchema = z.object({
/** 编排声明岗位,起单时确定性解析 */
position: z.string().min(1),
strategy: ownerStrategySchema.default('sole'),
/** strategy = assigned 时直接绑定员工(慎用) */
staffId: staffIdSchema.optional(),
});
export type OwnerSpec = z.infer<typeof ownerSpecSchema>;
/* ── 边与载荷映射 ────────────────────────────────────────────────────── */
/**
* 映射来源四类:上游输出口载荷字段 / 流程变量 / 起单载荷 / 常量;
* 允许 S-FEEL 表达式做转换(与触发器字段映射同一形式)。
*/
export const mappingSourceSchema = z.discriminatedUnion('from', [
z.object({ from: z.literal('output-port'), port: z.string().min(1), path: z.string().min(1) }),
z.object({ from: z.literal('workflow-var'), name: z.string().min(1) }),
z.object({ from: z.literal('initiation'), path: z.string().min(1) }),
z.object({ from: z.literal('constant'), value: z.unknown() }),
z.object({ from: z.literal('expression'), sfeel: z.string().min(1) }),
]);
export type MappingSource = z.infer<typeof mappingSourceSchema>;
export const fieldMappingSchema = z.object({
/** 下游活动契约 data 的字段路径 */
target: z.string().min(1),
source: mappingSourceSchema,
});
export type FieldMapping = z.infer<typeof fieldMappingSchema>;
export const edgeSchema = z.object({
id: z.string().min(1),
from: nodeIdSchema,
to: nodeIdSchema,
/** XOR 网关出边的条件(S-FEEL);AND 出边留空 */
condition: z.string().optional(),
/** 连一条边即须完成该边映射 —— 必填未映射 = 边未完成 */
mapping: z.array(fieldMappingSchema).default([]),
/** 边标签:画布上的出口名(如「TYPE-A」「通过」) */
label: z.string().optional(),
/** 回边标记:人裁决后重开新任务实例(同节点、新任务 ID、留痕关联) */
reopen: z.boolean().default(false),
});
export type Edge = z.infer<typeof edgeSchema>;
/* ── 失败走向四选一(OS 03) ────────────────────────────────────────── */
export const failureDispositionSchema = z.discriminatedUnion('kind', [
z.object({ kind: z.literal('terminate') }),
z.object({ kind: z.literal('reroute'), edgeId: z.string().min(1) }),
z.object({ kind: z.literal('escalate'), approvalNodeId: nodeIdSchema }),
/** 仅限 AND 分支;静态校验其下游映射不得依赖该分支的必填字段 */
z.object({ kind: z.literal('tolerate') }),
]);
export type FailureDisposition = z.infer<typeof failureDispositionSchema>;
export const FAILURE_DISPOSITION_LABEL: Readonly<Record<FailureDisposition['kind'], string>> = {
terminate: '终止流',
reroute: '改道',
escalate: '升审批',
tolerate: '容忍',
};
/* ── 催办阶梯(时限机制复用) ────────────────────────────────────────── */
export const nudgeLadderSchema = z.object({
/** ISO-8601 duration */
deadline: z.string().min(2),
steps: z.array(z.object({ after: z.string().min(2), action: z.enum(['nudge', 'escalate']) })).default([]),
});
export type NudgeLadder = z.infer<typeof nudgeLadderSchema>;
/* ── 节点 ─────────────────────────────────────────────────────────────── */
export const GATEWAY_KINDS = ['parallel', 'exclusive'] as const;
export const gatewayKindSchema = z.enum(GATEWAY_KINDS);
export type GatewayKind = z.infer<typeof gatewayKindSchema>;
export const GATEWAY_KIND_LABEL: Readonly<Record<GatewayKind, string>> = {
parallel: '并行网关',
exclusive: '异或网关',
};
const nodeBase = {
id: nodeIdSchema,
label: z.string().min(1),
/** 画布坐标 —— 布局引擎的输入;不影响语义 */
layout: z.object({ row: z.number().int(), col: z.number().int() }).optional(),
};
export const processNodeSchema = z.discriminatedUnion('kind', [
z.object({ ...nodeBase, kind: z.literal('start') }),
z.object({
...nodeBase,
kind: z.literal('end'),
/** 流程包显式终点的输出声明 */
output: jsonSchemaSchema.optional(),
}),
z.object({
...nodeBase,
kind: z.literal('activity'),
/** 以声明入流:引用活动库中已安装的活动 */
activityId: activityIdSchema,
/** 版本约束:留空 = 装配时取最新生效版 */
activityVersion: z.string().optional(),
owner: ownerSpecSchema,
failure: failureDispositionSchema.default({ kind: 'terminate' }),
/** 委托消息模板,可插值载荷字段;缺省为空 */
message: z.string().optional(),
}),
z.object({
...nodeBase,
kind: z.literal('approval'),
/** 审批点功能属性四件:审批岗位 / 输入装配映射 / 驳回走向 / 催办阶梯 */
approver: ownerSpecSchema,
/** 驳回走向:声明式,不由临场决定 */
reject: z.discriminatedUnion('kind', [
z.object({ kind: z.literal('reopen'), nodeId: nodeIdSchema, mapping: z.array(fieldMappingSchema).default([]) }),
z.object({ kind: z.literal('terminate') }),
]),
nudge: nudgeLadderSchema.optional(),
message: z.string().optional(),
}),
z.object({
...nodeBase,
kind: z.literal('gateway'),
gateway: gatewayKindSchema,
/** 分叉 / 汇合 */
mode: z.enum(['fork', 'join']),
}),
]);
export type ProcessNode = z.infer<typeof processNodeSchema>;
/* ── 调用接口:流程包的唯一出入口 ───────────────────────────────────── */
export const callInterfaceSchema = z.object({
input: jsonSchemaSchema,
/** 显式终点的输出声明 —— 末端输出载荷按此交付、按来源回执 */
output: jsonSchemaSchema,
});
export type CallInterface = z.infer<typeof callInterfaceSchema>;
/* ── 流程包 ───────────────────────────────────────────────────────────── */
export const PROCESS_PACKAGE_STATES = ['draft', 'published', 'retired'] as const;
export const processPackageStateSchema = z.enum(PROCESS_PACKAGE_STATES);
export type ProcessPackageState = z.infer<typeof processPackageStateSchema>;
export const processPackageSchema = z.object({
id: processPackageIdSchema,
name: z.string().min(1),
version: z.number().int().min(1),
state: processPackageStateSchema,
callInterface: callInterfaceSchema,
nodes: z.array(processNodeSchema).min(2),
edges: z.array(edgeSchema),
/** 流程变量声明:边映射的 workflow-var 来源 */
variables: z.array(z.object({ name: z.string().min(1), schema: jsonSchemaSchema })).default([]),
description: z.string().optional(),
});
export type ProcessPackage = z.infer<typeof processPackageSchema>;
/* ── 编排校验的报告形状(前端画布即时预览与后端裁决同源) ───────────── */
export const CLOSURE_VIOLATION_KINDS = [
'edge-unmapped',
'required-unmapped',
'type-conflict',
'dangling-node',
'gateway-unbalanced',
'join-ambiguous-source',
'tolerate-required-dependency',
'unknown-activity',
'missing-owner-position',
] as const;
export const closureViolationKindSchema = z.enum(CLOSURE_VIOLATION_KINDS);
export type ClosureViolationKind = z.infer<typeof closureViolationKindSchema>;
export const closureViolationSchema = z.object({
kind: closureViolationKindSchema,
nodeId: nodeIdSchema.optional(),
edgeId: z.string().optional(),
field: z.string().optional(),
message: z.string(),
});
export type ClosureViolation = z.infer<typeof closureViolationSchema>;
export const closureReportSchema = z.object({
publishable: z.boolean(),
violations: z.array(closureViolationSchema),
});
export type ClosureReport = z.infer<typeof closureReportSchema>;
/* ── 触发器三类(OS 3.1) ──────────────────────────────────────────── */
export const TRIGGER_KINDS = ['periodic', 'event', 'registration'] as const;
export const triggerKindSchema = z.enum(TRIGGER_KINDS);
export type TriggerKind = z.infer<typeof triggerKindSchema>;
export const TRIGGER_KIND_LABEL: Readonly<Record<TriggerKind, string>> = {
periodic: '周期触发',
event: '事件触发',
registration: '登记触发',
};
export const triggerConfigSchema = z.object({
id: triggerIdSchema,
name: z.string().min(1),
enabled: z.boolean().default(true),
/** 触发器只知道调用接口,不知流程内部 */
processPackageId: processPackageIdSchema,
/** 起单载荷装配:事件信封与载荷字段 → 调用接口的 data 字段;缺必填即起单失败入死信 */
mapping: z.array(fieldMappingSchema).default([]),
spec: z.discriminatedUnion('kind', [
z.object({
kind: z.literal('periodic'),
/** 日历型:纯日历规则 */
mode: z.literal('calendar'),
/** cron 风格的日历表达式 */
calendar: z.string().min(1),
}),
z.object({
kind: z.literal('periodic'),
/** 数据日历型:按台账日期字段 + 偏移 + 过滤条件逐行起单 */
mode: z.literal('data-calendar'),
domain: z.string().min(1),
object: z.string().min(1),
dateField: z.string().min(1),
/** 偏移:如 -P3D 表示日期字段前 3 天 */
offset: z.string().min(2),
filter: z.string().optional(),
}),
z.object({
kind: z.literal('event'),
/** 命中规则:S-FEEL,对事件信封与载荷字段求值 */
rule: z.string().min(1),
eventType: z.enum(['data', 'time', 'human', 'control']).default('data'),
}),
z.object({
kind: z.literal('registration'),
/** 登记表单由调用接口 schema 自动渲染 */
formHint: z.string().optional(),
}),
]),
});
export type TriggerConfig = z.infer<typeof triggerConfigSchema>;