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
+343
View File
@@ -0,0 +1,343 @@
/**
* 统一事件标准(编码规范 §8.1;语义出处 OS 06)。
*
* 系统向活动只以统一信封抛四类事件;一条事件流,三级消费(任务监听 / 触发器 / 消息)。
* 投递五诺(先持久后投递 / 同任务按序 / 至少一次 + 幂等 / 处理有回执 / 扇出与幂等)
* 是 `event` 模块的实现承诺,不在本文件表达 —— 本文件只定信封的形状。
*/
import { z } from 'zod';
export declare const EVENT_TYPES: readonly ["data", "time", "human", "control"];
export declare const eventTypeSchema: z.ZodEnum<{
human: "human";
data: "data";
time: "time";
control: "control";
}>;
export type EventType = z.infer<typeof eventTypeSchema>;
export declare const EVENT_TYPE_LABEL: Readonly<Record<EventType, string>>;
/** 消费目标四类(OS 6.1):任务 / 触发器 / 工作流 / 消息收件岗位 */
export declare const eventTargetSchema: z.ZodDiscriminatedUnion<[z.ZodObject<{
kind: z.ZodLiteral<"task">;
id: z.core.$ZodBranded<z.ZodString, "TaskId", "out">;
}, z.core.$strip>, z.ZodObject<{
kind: z.ZodLiteral<"trigger">;
id: z.core.$ZodBranded<z.ZodString, "TriggerId", "out">;
}, z.core.$strip>, z.ZodObject<{
kind: z.ZodLiteral<"workflow">;
id: z.core.$ZodBranded<z.ZodString, "WorkflowId", "out">;
}, z.core.$strip>, z.ZodObject<{
kind: z.ZodLiteral<"message-position">;
id: z.core.$ZodBranded<z.ZodString, "PositionId", "out">;
}, z.core.$strip>], "kind">;
export type EventTarget = z.infer<typeof eventTargetSchema>;
/** 起因:数据对象 / 卡 ID / 定时器 ID —— 「这条事件是被什么激起的」 */
export declare const eventCauseSchema: z.ZodObject<{
kind: z.ZodEnum<{
"ledger-write": "ledger-write";
"card-reply": "card-reply";
timer: "timer";
inbound: "inbound";
governance: "governance";
engine: "engine";
}>;
ref: z.ZodString;
}, z.core.$strip>;
export type EventCause = z.infer<typeof eventCauseSchema>;
export declare const EVENT_SOURCES: readonly ["channel", "engine", "desk", "governance"];
export declare const eventSourceKindSchema: z.ZodEnum<{
governance: "governance";
engine: "engine";
channel: "channel";
desk: "desk";
}>;
export type EventSourceKind = z.infer<typeof eventSourceKindSchema>;
export declare const eventSourceSchema: z.ZodObject<{
kind: z.ZodEnum<{
governance: "governance";
engine: "engine";
channel: "channel";
desk: "desk";
}>;
channelId: z.ZodOptional<z.core.$ZodBranded<z.ZodString, "ChannelId", "out">>;
detail: z.ZodOptional<z.ZodString>;
}, z.core.$strip>;
export type EventSource = z.infer<typeof eventSourceSchema>;
/** 载荷:schema 化内容 + 原件指针(如邮件原文的存档地址) */
export declare const eventPayloadSchema: z.ZodObject<{
data: z.ZodRecord<z.ZodString, z.ZodUnknown>;
originRef: z.ZodOptional<z.ZodString>;
}, z.core.$strip>;
export type EventPayload = z.infer<typeof eventPayloadSchema>;
export declare const envelopeSchema: z.ZodObject<{
id: z.core.$ZodBranded<z.ZodString, "EventId", "out">;
type: z.ZodEnum<{
human: "human";
data: "data";
time: "time";
control: "control";
}>;
correlation: z.ZodObject<{
target: z.ZodOptional<z.ZodDiscriminatedUnion<[z.ZodObject<{
kind: z.ZodLiteral<"task">;
id: z.core.$ZodBranded<z.ZodString, "TaskId", "out">;
}, z.core.$strip>, z.ZodObject<{
kind: z.ZodLiteral<"trigger">;
id: z.core.$ZodBranded<z.ZodString, "TriggerId", "out">;
}, z.core.$strip>, z.ZodObject<{
kind: z.ZodLiteral<"workflow">;
id: z.core.$ZodBranded<z.ZodString, "WorkflowId", "out">;
}, z.core.$strip>, z.ZodObject<{
kind: z.ZodLiteral<"message-position">;
id: z.core.$ZodBranded<z.ZodString, "PositionId", "out">;
}, z.core.$strip>], "kind">>;
cause: z.ZodObject<{
kind: z.ZodEnum<{
"ledger-write": "ledger-write";
"card-reply": "card-reply";
timer: "timer";
inbound: "inbound";
governance: "governance";
engine: "engine";
}>;
ref: z.ZodString;
}, z.core.$strip>;
keys: z.ZodDefault<z.ZodRecord<z.ZodString, z.ZodString>>;
}, z.core.$strip>;
payload: z.ZodObject<{
data: z.ZodRecord<z.ZodString, z.ZodUnknown>;
originRef: z.ZodOptional<z.ZodString>;
}, z.core.$strip>;
occurredAt: z.ZodISODateTime;
source: z.ZodObject<{
kind: z.ZodEnum<{
governance: "governance";
engine: "engine";
channel: "channel";
desk: "desk";
}>;
channelId: z.ZodOptional<z.core.$ZodBranded<z.ZodString, "ChannelId", "out">>;
detail: z.ZodOptional<z.ZodString>;
}, z.core.$strip>;
trace: z.ZodObject<{
storeRef: z.ZodString;
taskMemoryRef: z.ZodOptional<z.ZodString>;
}, z.core.$strip>;
}, z.core.$strip>;
export type Envelope = z.infer<typeof envelopeSchema>;
/**
* 尚未落存储的信封草稿 —— `publish` 的入参。
*
* `target` 在此**可空**:原始事件未必知道自己该被谁消费。归属解析(6.2)与三级消费判定
* (任务监听 / 触发器 / 消息兜底,6.3)由 `event` 模块完成,并为每个命中目标生成一份
* 独立投递 —— 投递出去的信封「关联 = 单目标」,这正是扇出与幂等承诺的形态。
* `storeRef` 由 event adapter 落库时补。
*/
export declare const envelopeDraftSchema: z.ZodObject<{
type: z.ZodEnum<{
human: "human";
data: "data";
time: "time";
control: "control";
}>;
payload: z.ZodObject<{
data: z.ZodRecord<z.ZodString, z.ZodUnknown>;
originRef: z.ZodOptional<z.ZodString>;
}, z.core.$strip>;
source: z.ZodObject<{
kind: z.ZodEnum<{
governance: "governance";
engine: "engine";
channel: "channel";
desk: "desk";
}>;
channelId: z.ZodOptional<z.core.$ZodBranded<z.ZodString, "ChannelId", "out">>;
detail: z.ZodOptional<z.ZodString>;
}, z.core.$strip>;
correlation: z.ZodObject<{
target: z.ZodOptional<z.ZodDiscriminatedUnion<[z.ZodObject<{
kind: z.ZodLiteral<"task">;
id: z.core.$ZodBranded<z.ZodString, "TaskId", "out">;
}, z.core.$strip>, z.ZodObject<{
kind: z.ZodLiteral<"trigger">;
id: z.core.$ZodBranded<z.ZodString, "TriggerId", "out">;
}, z.core.$strip>, z.ZodObject<{
kind: z.ZodLiteral<"workflow">;
id: z.core.$ZodBranded<z.ZodString, "WorkflowId", "out">;
}, z.core.$strip>, z.ZodObject<{
kind: z.ZodLiteral<"message-position">;
id: z.core.$ZodBranded<z.ZodString, "PositionId", "out">;
}, z.core.$strip>], "kind">>;
cause: z.ZodObject<{
kind: z.ZodEnum<{
"ledger-write": "ledger-write";
"card-reply": "card-reply";
timer: "timer";
inbound: "inbound";
governance: "governance";
engine: "engine";
}>;
ref: z.ZodString;
}, z.core.$strip>;
keys: z.ZodDefault<z.ZodRecord<z.ZodString, z.ZodString>>;
}, z.core.$strip>;
occurredAt: z.ZodISODateTime;
id: z.ZodOptional<z.core.$ZodBranded<z.ZodString, "EventId", "out">>;
}, z.core.$strip>;
export type EnvelopeDraft = z.infer<typeof envelopeDraftSchema>;
/** 控制事件载荷:三语义 + 发起人 */
export declare const controlPayloadSchema: z.ZodObject<{
semantic: z.ZodEnum<{
pause: "pause";
halt: "halt";
terminate: "terminate";
}>;
reason: z.ZodOptional<z.ZodString>;
issuedBy: z.ZodString;
}, z.core.$strip>;
export type ControlPayload = z.infer<typeof controlPayloadSchema>;
/** 时间事件载荷:哪个监听 / 定时器到点 */
export declare const timePayloadSchema: z.ZodObject<{
listenerId: z.ZodOptional<z.core.$ZodBranded<z.ZodString, "ListenerId", "out">>;
timerRef: z.ZodString;
escalation: z.ZodOptional<z.ZodEnum<{
nudge: "nudge";
reroute: "reroute";
escalate: "escalate";
}>>;
}, z.core.$strip>;
export type TimePayload = z.infer<typeof timePayloadSchema>;
/** 数据事件载荷:域.对象.主键 的正式版写入 —— 草稿写不抛事件(OS 09) */
export declare const dataPayloadSchema: z.ZodObject<{
domain: z.ZodString;
object: z.ZodString;
primaryKey: z.ZodRecord<z.ZodString, z.ZodUnion<readonly [z.ZodString, z.ZodNumber]>>;
changed: z.ZodDefault<z.ZodArray<z.ZodString>>;
operation: z.ZodEnum<{
insert: "insert";
update: "update";
delete: "delete";
}>;
}, z.core.$strip>;
export type DataPayload = z.infer<typeof dataPayloadSchema>;
/**
* 处理语义:loop 事件即观察 / flow 事件即步的完成条件。
* 这是**引擎层**的区分,交互层不可见(编码规范 §9 双层边界)。
*/
export declare const LISTENER_SEMANTICS: readonly ["observe", "complete-step"];
export declare const listenerSemanticSchema: z.ZodEnum<{
observe: "observe";
"complete-step": "complete-step";
}>;
export type ListenerSemantic = z.infer<typeof listenerSemanticSchema>;
export declare const listenerSpecSchema: z.ZodObject<{
origin: z.ZodEnum<{
static: "static";
dynamic: "dynamic";
}>;
eventType: z.ZodEnum<{
human: "human";
data: "data";
time: "time";
control: "control";
}>;
keyFilter: z.ZodDefault<z.ZodRecord<z.ZodString, z.ZodString>>;
condition: z.ZodOptional<z.ZodString>;
semantic: z.ZodEnum<{
observe: "observe";
"complete-step": "complete-step";
}>;
deadline: z.ZodString;
escalation: z.ZodDefault<z.ZodArray<z.ZodEnum<{
nudge: "nudge";
reroute: "reroute";
escalate: "escalate";
}>>>;
}, z.core.$strip>;
export type ListenerSpec = z.infer<typeof listenerSpecSchema>;
export declare const registeredListenerSchema: z.ZodObject<{
origin: z.ZodEnum<{
static: "static";
dynamic: "dynamic";
}>;
eventType: z.ZodEnum<{
human: "human";
data: "data";
time: "time";
control: "control";
}>;
keyFilter: z.ZodDefault<z.ZodRecord<z.ZodString, z.ZodString>>;
condition: z.ZodOptional<z.ZodString>;
semantic: z.ZodEnum<{
observe: "observe";
"complete-step": "complete-step";
}>;
deadline: z.ZodString;
escalation: z.ZodDefault<z.ZodArray<z.ZodEnum<{
nudge: "nudge";
reroute: "reroute";
escalate: "escalate";
}>>>;
id: z.core.$ZodBranded<z.ZodString, "ListenerId", "out">;
taskId: z.core.$ZodBranded<z.ZodString, "TaskId", "out">;
registeredAt: z.ZodISODateTime;
expiresAt: z.ZodISODateTime;
state: z.ZodEnum<{
armed: "armed";
fired: "fired";
expired: "expired";
cancelled: "cancelled";
}>;
}, z.core.$strip>;
export type RegisteredListener = z.infer<typeof registeredListenerSchema>;
export declare const deliverySchema: z.ZodObject<{
id: z.core.$ZodBranded<z.ZodString, "DeliveryId", "out">;
eventId: z.core.$ZodBranded<z.ZodString, "EventId", "out">;
target: z.ZodDiscriminatedUnion<[z.ZodObject<{
kind: z.ZodLiteral<"task">;
id: z.core.$ZodBranded<z.ZodString, "TaskId", "out">;
}, z.core.$strip>, z.ZodObject<{
kind: z.ZodLiteral<"trigger">;
id: z.core.$ZodBranded<z.ZodString, "TriggerId", "out">;
}, z.core.$strip>, z.ZodObject<{
kind: z.ZodLiteral<"workflow">;
id: z.core.$ZodBranded<z.ZodString, "WorkflowId", "out">;
}, z.core.$strip>, z.ZodObject<{
kind: z.ZodLiteral<"message-position">;
id: z.core.$ZodBranded<z.ZodString, "PositionId", "out">;
}, z.core.$strip>], "kind">;
attempt: z.ZodNumber;
state: z.ZodEnum<{
pending: "pending";
delivered: "delivered";
failed: "failed";
"dead-letter": "dead-letter";
}>;
lastError: z.ZodOptional<z.ZodString>;
}, z.core.$strip>;
export type Delivery = z.infer<typeof deliverySchema>;
/** 匹配次序确定、逐级短路:① 线程精确 ② 关联键命中监听 ③ 地址 + 时间窗 */
export declare const OWNERSHIP_RULES: readonly ["thread", "listener-key", "address-window"];
export declare const ownershipRuleSchema: z.ZodEnum<{
thread: "thread";
"listener-key": "listener-key";
"address-window": "address-window";
}>;
export type OwnershipRule = z.infer<typeof ownershipRuleSchema>;
export declare const ownershipResolutionSchema: z.ZodDiscriminatedUnion<[z.ZodObject<{
outcome: z.ZodLiteral<"owned">;
rule: z.ZodEnum<{
thread: "thread";
"listener-key": "listener-key";
"address-window": "address-window";
}>;
taskId: z.core.$ZodBranded<z.ZodString, "TaskId", "out">;
}, z.core.$strip>, z.ZodObject<{
outcome: z.ZodLiteral<"unowned">;
reason: z.ZodEnum<{
"no-match": "no-match";
ambiguous: "ambiguous";
"extract-failed": "extract-failed";
}>;
fallbackPosition: z.ZodOptional<z.core.$ZodBranded<z.ZodString, "PositionId", "out">>;
}, z.core.$strip>], "outcome">;
export type OwnershipResolution = z.infer<typeof ownershipResolutionSchema>;